# Problem when filtering a HubDB query with a custom module field

**URL:** <https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434>\
**Category:** CMS Development\
**Created:** [November 17, 2016, 6:58pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434 "2016-11-17T18:58:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohva](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mohva/32/25270_2.png) [@Mohva](https://community.hubspot.com/u/Mohva)\
**Post date:** [November 17, 2016, 6:58pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/1 "2016-11-17T18:58:53Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![nickdeckerdevs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nickdeckerdevs/32/92875_2.png) [@nickdeckerdevs](https://community.hubspot.com/u/nickdeckerdevs)\
**Post date:** [November 18, 2016, 12:58pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/2 "2016-11-18T12:58:42Z")

</div>

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 %} `

---

<div class="post-metadata">

**Author:** ![Mohva](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mohva/32/25270_2.png) [@Mohva](https://community.hubspot.com/u/Mohva)\
**Post date:** [November 18, 2016, 1:32pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/3 "2016-11-18T13:32:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![boulter](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/boulter/32/50028_2.png) [@boulter](https://community.hubspot.com/u/boulter)\
**Post date:** [November 18, 2016, 3:15pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/4 "2016-11-18T15:15:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![adriancblack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/adriancblack/32/13521_2.png) [@adriancblack](https://community.hubspot.com/u/adriancblack)\
**Post date:** [March 26, 2017, 3:48pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/5 "2017-03-26T15:48:01Z")

</div>

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 %}

---

<div class="post-metadata">

**Author:** ![Mohva](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mohva/32/25270_2.png) [@Mohva](https://community.hubspot.com/u/Mohva)\
**Post date:** [March 27, 2017, 5:44pm UTC](https://community.hubspot.com/t/problem-when-filtering-a-hubdb-query-with-a-custom-module-field/434/6 "2017-03-27T17:44:56Z")

</div>

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](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.
