CMS Development

Mohva
Participant

Problem when filtering a HubDB query with a custom module field

SOLVE

Hi,

 

I'm trying to add a filter to a HubDB query, using the content from a text field in a cusom module.

 

In my custom module, I have the following code, wich works fine:

 

{% for row in hubdb_table_rows(table_id,"&partner__eq=3") %}
Print Something
{%endfor%}

 

I want the "3" to be dinamically set on each different page. For that, I'm using a Text Field on the Custom module.

 

I would expect that something like this would work:

{% for row in hubdb_table_rows(table_id,"&partner__eq="~widget.partner_number) %}
Print Something
{%endfor%}

 

But this, or any variant form of concatenating the string with the field content, gives me the same result:

"The following validation error was found:"

With no extra detail.

 

I even tried to do domething like this, wich works:

{% set partner_number="3"%}
{% set pre_filter="&partner__eq="%}
{% set filter= pre_filter ~ partner_number %}

{% for row in hubdb_table_rows(155348,filter) %}

 

But when I change the "3" with widget.partner_number, it gives the same error.

 

Does anyone has any idea what it might be the problem?

 

Thanks,

 

 

 

 

1 Accepted solution
nickdeckerdevs
Solution
Contributor | Diamond Partner
Contributor | Diamond Partner

Problem when filtering a HubDB query with a custom module field

SOLVE

I set up all my queries outside of that for loop, like you are showing at the bottom.

{% set queryparams = "" %} 
{% if request.query_dict.fname %}
{% set queryparams = queryparams ~ "&fname="~request.query_dict.fname|title|urlencode %}
{% endif %} 

 

View solution in original post

5 Replies 5
nickdeckerdevs
Solution
Contributor | Diamond Partner
Contributor | Diamond Partner

Problem when filtering a HubDB query with a custom module field

SOLVE

I set up all my queries outside of that for loop, like you are showing at the bottom.

{% set queryparams = "" %} 
{% if request.query_dict.fname %}
{% set queryparams = queryparams ~ "&fname="~request.query_dict.fname|title|urlencode %}
{% endif %} 

 

adriancblack
Participant | Partner
Participant | Partner

Problem when filtering a HubDB query with a custom module field

SOLVE

How do you acheave this but with THE TABLE ID being dinamic?

 

ie:

 

{% set queryparam = "&orderBy=order" %}

{% set tableid = "widget.table_id" %} /* table id can change from page to page */

{% for row in hubdb_table_rows( tableid , queryparam )%}

 content here
{% endfor %}

0 Upvotes
Mohva
Participant

Problem when filtering a HubDB query with a custom module field

SOLVE

In my case, the table_id is always the same.

 

I can imagine different ways you can make it dynamic. If the change occurs in the page level, you can create a property on a Custom Module that allows you to set the related table_id for each page. 

 

If you need to be dynamic within the page, you will have to put a Hubl variable with all the tables ids available and make your algorithm from there.

 

Or if your tables will be changin, maybe is better to use te REST API and process the tables using Javascript (http://developers.hubspot.com/docs/methods/hubdb/get_tables).

 

But in that case, you have to be more carefull since you'll have to go throught authentication, so you probably don't wan't to expose that to the general public.

 

Hope that helps.

0 Upvotes
Mohva
Participant

Problem when filtering a HubDB query with a custom module field

SOLVE

Not sure what did the trick, but it worked like a charm.

Here is the code I implemented, using yours as base:

 

{% set queryparams = "" %}
{% if widget.partner_number %}
{% set queryparams = queryparams ~ "&partner__eq="~widget.partner_number|title|urlencode %}
{% endif %}

{% for row in hubdb_table_rows(tableid, queryparams ) %}

Print Something

{% endfor %}

 

Thanks.

 

 

boulter
HubSpot Product Team
HubSpot Product Team

Problem when filtering a HubDB query with a custom module field

SOLVE

I'm glad you got it working. A few tips:

 

  • "__eq" is the default comparison operator, so you can just use "partner=".
  • You don't need to start the query with an '&'. It should still work, but it's unnecessary.
  • If partner_number is really just a number, you don't the title or urlencode filters.
  • Nick is correct in that you do have to build the query string before the hubdb_rable_rows call.