How to write a crm_objects query to match a substring?

jplew
Member

Hello,

I am writing a custom module to display specific products on a page. I want to filter all my products based on a tag, in this case the `ip__shopify__tags` property.

 

I am trying to write a query like this:

 

{% set products = crm_objects(dynamic_page_crm_object_type_fqn, "ip__shopify__tags__contains=215","ip__shopify__tags") %}

 

But it does not filter them, it returns all 81 of my products and I'm expecting 25.
  

I tried modifying the query to use "in", like this: `ip__shopify__tags__in=215`

 

But it returned 0 products.

I should note that when I remove the query param and simply request all my products I get 81 results.
I know this string querying should work in principle, because it is working fine via the REST api using the CONTAINS_TOKEN operator:

 

```
POST https://api.hubapi.com/crm/v3/objects/products/search


body:

{
  "filterGroups": [
    {
      "filters": [
        {
          "value": "215",
          "propertyName": "ip__shopify__tags",
          "operator": "CONTAINS_TOKEN"
        }
      ]
    }
  ],
  "limit": 100,
  "after": 0
}
```
The above call produces 25 results, which is what I'm expecting. For example the results look like this:
 
{
    "total": 25,
    "results": [
        {
            "id": "943773526",
            "properties": {
                "createdate": "2021-01-12T18:44:52Z",
                "hs_lastmodifieddate": "2021-12-08T01:43:25.487Z",
                "hs_object_id": "943773526",
                "ip__shopify__handle": "215-dry-trimmer",
                "ip__shopify__published_at": "2021-04-19T07:21:51Z",
                "ip__shopify__tags": "215 Series, 215 trimmer, best dry trimmer, bud, bud sorter, bud trimmer, cannabis, cannabis sorter, dry trimmer, Dry Trimmers, greenbroz 215 dry trimmer, m trimmer, number one dry trimmer, top cannabis trimmer, trimmer",
                "name": "215 Dry Trimmer",
                "shopify_id": "5991330021539",
                "shopify_product_type": null,
                "shopify_slug": "215-dry-trimmer"
            },
            "createdAt": "2021-01-12T18:44:52Z",
            "updatedAt": "2021-12-08T01:43:25.487Z",
            "archived": false
        },
        ....
```
 
Alternatively, is there a way to query the REST API directly from a Hubspot module, and avoid using the `crm_objects` method altogether?
0 Upvotes
8 Replies 8
dennisedson
Community Manager

@miljkovicmisa , any chance you could help out here?

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes
miljkovicmisa
Top Contributor | Gold Partner

Hello @jplew , 👋@dennisedson !
Thank you for writing in the forum, it seems that the contains query operator is only applicable for multi-valued properties, as per the documentation. Have you tried the in operator?

If my answer was helpful please mark it as a solution.

jplew
Member

Hi @miljkovicmisa ,

thanks for the suggestion. In my original post I mentioned that I tried "in" already and it returns 0 results. So it's either buggy or not what I'm looking for.

 

The documentation is not clear either on its intended usage, nor does it give any examples. For example how do I search for a string with spaces? Which one of these? 

  • "ip__shopify__tags__in=dry trimmer"
  • "ip__shopify__tags__in=dry%20trimmer"
  • "ip__shopify__tags__in='dry trimmer'"

I can't verify these because nothing works.

 

Is it possible to bypass the clunky `crm_objects` method altogether and invoke the Hubspot API? I don't see why HubL coders or custom module builders should be denied the opportunity to use the API, which is far better documented than the native HubL functions.

0 Upvotes
miljkovicmisa
Top Contributor | Gold Partner

@jplew I also see another thing and I'm not sure if that causes the issue but the internal name of the property seems to have double underscores which is the way that hubspot differrentiates the operator from the property name, so maybe that's why it's buggy, maybe try it with another property and see if it works okay, properties should have single underscores, something like the following:
ip_shopify_tags__in=dry trimmer

P.S.: The query should take care of the spaces accordingly in its internal call so I wouldn't worry about that.

0 Upvotes
jplew
Member

That's a clever suggestion. I don't think that's the problem however, since I've used operators on other properties with a double-underscore. When I use the "in" operator with another property I still get 0 results. My actual queries look like this:

 

0 results

"ip__ecomm_bridge__source_app_id__contains=51381&ip__shopify__published_at__not_null=&limit=200&name__in=215"

 

0 results

"ip__ecomm_bridge__source_app_id__contains=51381&ip__shopify__published_at__not_null=&limit=200&shopify_slug__in=215"

 

0 results

"ip__ecomm_bridge__source_app_id__contains=51381&ip__shopify__published_at__not_null=&limit=200&ip__shopify__tags__in=215"

 

81 results

"ip__ecomm_bridge__source_app_id__contains=51381&ip__shopify__published_at__not_null=&limit=200"


Is the "in" operator documented anywhere?

 

0 Upvotes
miljkovicmisa
Top Contributor | Gold Partner

The in operator is documented as a valid operator in this link.

Also what about this bit:

ip__shopify__published_at__not_null=

Seems a bit funky containing the = sign like this.

0 Upvotes
jplew
Member

@miljkovicmisa wrote:

The in operator is documented as a valid operator in this link.

Also what about this bit:

ip__shopify__published_at__not_null=

Seems a bit funky containing the = sign like this.


Yes, I know the in operator is valid, I'm just not sure what it does or how to get it to work. 

 

I thought that = sign part was funky too, but it works. Whether I include it or not makes no difference, I get the same query results either way.

 

I appreciate you taking the time to help troubleshoot @miljkovicmisa .

0 Upvotes
jplew
Member

Hello,

I am writing a custom module to display specific products on a page. I want to filter all my products based on a tag, in this case the `ip__shopify__tags` property (I'm building a shop using the Shopify Integration).

 

I am trying to write a query like this:

 

{% set products = crm_objects(dynamic_page_crm_object_type_fqn, "ip__shopify__tags__contains=215","ip__shopify__tags") %}

 

But it does not filter them, it returns all 81 of my products and I'm expecting 25.
  

I tried modifying the query to use "in", like this: `ip__shopify__tags__in=215`

 

But it returned 0 products.

I should note that when I remove the query param and simply request all my products I get 81 results.
I know this string querying should work in principle, because it is working fine via the REST api using the CONTAINS_TOKEN operator:

 

```
POST https://api.hubapi.com/crm/v3/objects/products/search


body:

{
  "filterGroups": [
    {
      "filters": [
        {
          "value": "215",
          "propertyName": "ip__shopify__tags",
          "operator": "CONTAINS_TOKEN"
        }
      ]
    }
  ],
  "limit": 100,
  "after": 0
}
```
The above call produces 25 results, which is what I'm expecting. For example the results look like this:
 
{
    "total": 25,
    "results": [
        {
            "id": "943773526",
            "properties": {
                "createdate": "2021-01-12T18:44:52Z",
                "hs_lastmodifieddate": "2021-12-08T01:43:25.487Z",
                "hs_object_id": "943773526",
                "ip__shopify__handle": "215-dry-trimmer",
                "ip__shopify__published_at": "2021-04-19T07:21:51Z",
                "ip__shopify__tags": "215 Series, 215 trimmer, best dry trimmer, bud, bud sorter, bud trimmer, cannabis, cannabis sorter, dry trimmer, Dry Trimmers, greenbroz 215 dry trimmer, m trimmer, number one dry trimmer, top cannabis trimmer, trimmer",
                "name": "215 Dry Trimmer",
                "shopify_id": "5991330021539",
                "shopify_product_type": null,
                "shopify_slug": "215-dry-trimmer"
            },
            "createdAt": "2021-01-12T18:44:52Z",
            "updatedAt": "2021-12-08T01:43:25.487Z",
            "archived": false
        },
        ....
```
 
Alternatively, is there a way to query the REST API directly from a Hubspot module, and avoid using the `crm_objects` method altogether?
0 Upvotes