Getting HubDB Column Data

Hi I am trying to call HubDB row data base on a column select info.

So if business_outcome is Increase Loyalty then use the data in that row only (image, title, decripction etc…)

Currently this works by using the row id:

{% set row = hubdb_table_row(121356853, 191349040224) %}

I would rather use a column select option than the row ID.
How would I alter the code to action the above?
Thanks

{% set table = hubdb_table_rows(121356853) %}
{% for row in table %}
 {% if row.business_outcome.label == "Increase Loyalty" %}
 <div>{{row.name}}, etc.</div>
 {% endif %}
 {% endfor %}

That assumes the name of the column is business_outcome i.e. the label hasn’t been changed.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

Thanks Barry

This is how I currently have the code written that works via row id:

{% if module.business_outcome_resource == "Increase Sales" %}
 {% set row = hubdb_table_row(121356853, 191295276975) %}
{% elif module.business_outcome_resource == "Increase Loyalty" %}
 {% set row = hubdb_table_row(121356853, 191349040224) %}
{% elif module.business_outcome_resource == "Win Mindshare and Market Share" %}
 {% set row = hubdb_table_row(121356853, 191349738266) %}
{% elif module.business_outcome_resource == "Improve Experience" %}
 {% set row = hubdb_table_row(121356853, 191349040228) %}
{% elif module.business_outcome_resource == "Increase Engagement" %}
 {% set row = hubdb_table_row(121356853, 191349738267) %}
{% elif module.business_outcome_resource == "Measure Program Success" %}
 {% set row = hubdb_table_row(121356853, 191349738268) %}
{% elif module.business_outcome_resource == "Become Compliant" %}
 {% set row = hubdb_table_row(121356853, 191349040230) %}
{% elif module.business_outcome_resource == "Optimize Margins" %}
 {% set row = hubdb_table_row(121356853, 191349040231) %}
{% elif module.business_outcome_resource == "Centralize Ecosystem Orchestration" %}
 {% set row = hubdb_table_row(121356853, 191349738271) %}
{% elif module.business_outcome_resource == "Consolidate User Experience" %}
 {% set row = hubdb_table_row(121356853, 191349738272) %}
{% elif module.business_outcome_resource == "Align Sales and Marketing" %}
 {% set row = hubdb_table_row(121356853, 191349738273) %}
{% elif module.business_outcome_resource == "Incentivize Learning and Education" %}
 {% set row = hubdb_table_row(121356853, 191349738274) %}
{% elif module.business_outcome_resource == "Tado" %}
 {% set row = hubdb_table_row(121356853, 191842089330) %}
{% endif %}

<div class="dnd-section" style="padding-top: 0px;">
 <div class="row-fluid">
 <div class="resource-featured resource-featured--default">
 <div class="resource-featured__container" style="background: rgba({{ module.style.background_color.color|convert_rgb }}, {{ module.style.background_color.opacity / 100 }});">
 <h2 class="">{{ row.intro }}</h2> 
 <div class="resource-featured__resource"> 
 <img src="{{ row.image.url }}" alt="{{ row.name }}" class="resource-featured__image" loading="lazy" style="max-width: 100%; height: auto;">
 <div class="resource-featured__content">
 <p class="tag type">{{ row.type.label }}</p>
 <h3 class="resource-featured__heading">{{ row.heading }}</h3>
 <div class="resource-featured__description">{{ row.description }}</div>
 <div class="resource-featured__buttons mt-sm">
 <a href="{{ row.button1_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button1_text }}
 </a>
 <a href="{{ row.button2_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button2_text }}
 </a>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
</div>

I have the code that builds the page after the {% endif %}

With you code suggestion I need to have the code that builds the page between the if and for statement. What is the best way for me to edit my code? Thanks

Hey @Woodsy :raising_hands: It looks like @kosalaindrasiri and @BarryGrennan are doing a great job troubleshooting along with you.

Looking at the documentation section "Getting rows, I wonder if this what you’re looking for.

Instead of using `hubdb_table_row()` repeatedly for each specific ID, have you tried using the `hubdb_table_rows()` function to fetch the entire table first?

This might let you loop through the results and use a single if statement to find the row where the `business_outcome` column matches your module’s selection.

Best,

Jaycee

Hey @Woodsy,

Instead of using a long {% if %} chain with hardcoded row IDs, you can dynamically fetch the correct HubDB row based on the selected business_outcome value by looping through all rows and matching the select field value,

{% set table_id = 121356853 %}
{% set rows = hubdb_table_rows(table_id) %}
{% set row = none %}

{% for item in rows %}
 {% if item.resource_name == module.business_outcome_resource %}
 {% set row = item %}
 {% break %} {# Exit the loop once a match is found #}
 {% endif %}
{% endfor %}

Note: Remember to replace resource_name with the actual internal name of your HubDB column!

Then use {{ row.intro }}, {{ row.image.url }}, etc. as before.

This approach is cleaner, scalable, and automatically adapts to new options in your HubDB table.

Refer: HubSpot HubL hubdb_table_rows function

Hope this will help!

Regards,

Hey @kosalaindrasiri,

while your answer has a few points, there are a few misleading information as well. Also it seems like it was Ai generated.

If this should be the case - Ai is not forbidden, but it should be mentioned that the post or parts of it were Ai generated. It just helps the community.

First of all:

there’s no

{% break %}

variable in HubL.

Jinja2 has one.

HubL is based on Jinja2, so Ai very often mistakenly confuses HubL with Jinja2, and while it can happen that pure Jinja2 code will work in HubSpot, putting this variable in the code you’re suggesting doesn’t make any sense.

Secondly:

unless you’re doing something like update, append etc. there’s no real use in adding

{% set row = none %}

to the code.

So why inflating the code unnecessarily?

Furthermore - besides being able to write

{{ row.business_outcome }}

what’s the reason of adding

{% set row = item %}

to the code?

{{ item.business_outcome }}

would work perfectly fine in this case.

Or simply just write

{% for row in rows %}
 {% if row.resource_name == module.business_outcome_resource %}
 {{ row.intro }} 
 {% endif %}
{% endfor %}

Last but not least, if there would be an update/append function in your suggested code, the initial “set” shouldn’t be “none” but something like

{% set row = {} %}

or

{% set row = [] %}

The if-statement part @Woodsy provided technically works.

It is - more or less - easy to handle and also extendable for future updates.

Can it be written completely different and optimized? Yes, but it’s the documented way of an if-statement and therefore a good starting point as nobody knows who will deal with this code in the future. There are many ways to achieve a desired outcome, but if an unexperienced developer will look at something like

{% set table = hubdb_table(123456789) %}

{% set hs_object_matcher = {
 "Increase Sales" : "191295276975",
 "Increase Loyalty" : "191349040224", 
...
}
%}

{% for column_name, hubdb_col_id in hs_object_matcher.items() %}
 {% if module.business_outcome_ressource == column_name %}
 {% set row = hubdb_table_row(table, hubdb_col_id) %}
 {% endif %}
{% endfor %}

^ just an example from my head to outline a potentially different way of writing the code; might not work

it might be super confusing to him/her. The machine, doesn’t really care.

So keeping the code as easy to understand for humans and clean as possible should always be a priority.
best,
Anton

Hi @kosalaindrasiri @Anton @BarryGrennan @Jaycee_Lewis

Thank you all for taking the time to look at my post.

I have tried what @kosalaindrasiri had suggested and also variations based on @Anton comments. Unfortunatly I haven’t been able to get the code working.

Ideally it would be more user friendly to use a single if statement to find the row where the `business_outcome` column matches the module’s selection. This way I would only need 1 if statement that doesn’t rely on adding the row IDs for each selection. If in the future more database items are added the only thing in the module that will need to be modified is the select field to add further business outcomes.

I would be extremly gratefull if you could show me how I would be able to achive the above so that I can then learn from it. I will like need to do similar or more complex versions based on this so any assistance will help me understand how it all fits together.

Thanks

J

Hey @Woodsy,
my post and the code was not intended as a working soltuion, as I don’t want to steal the show from @BarryGrennan.

The only purpose of my code was only to outline a potential Ai mistake and a different approach.

I’ll let @BarryGrennan take it from here :slightly_smiling_face:

best,

Anton

@Woodsy, could you explain your project a bit more?
You’re not putting your html within a for loop, which would suggest to me that you only want one row output on on page. But what on that page is setting which row to use? And if that’s the case, why not use dynamic pages?
Also, the way you’re aproaching it would seem to suggest that the business outcome is meant to be unique on each row, is that the case? If so, again I’d consider using dynamic pages and use the page title instead of a dropdown value that could be the same on multiple rows.

Maybe I’m not understanding the project though. Some further explanation will help.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

The module is going to be added to some landing pages and web pages. Near the bottom of all the solution pages is a featured resource area. I am looking to add the module to the page and let the content creators select which featured resource they want to show based on the pages overall business outcome.

Dynamic pages wouldn’t work in this instance as the module is only a small part of the overall page and could be used anywhere that a featured resource is required.
How I originally set up the code was using all the possible if statements depending on which modules selection. The code under all the if statements is where the feature resource panel is built based on the above selection.

I was hoping to possibly reduce the need for having to include all and future if statements by possibly looping all the columns selections and getting the code to display the row that matches the selection. In the database there will only be one row per business outcome.

I hope that give some context around the method in my madness.

@Woodsy in that case it would make more sense to use a HubDB Row field.
Set the table and fetch all necessary columns.
Then use

{% set row = module.hubdbrow_field.columns %}

And you should be able to use your existing html.

The content creator would then just select which row they want.

I still wouldn’t use a select field for Business Outcomes here, although there’s no absolute need to change that.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

@BarryGrennan would that mean I can remove all the if statements:

{% if module.business_outcome_resource == "Increase Sales" %}
 {% set row = hubdb_table_row(121356853, 191295276975) %}
{% elif module.business_outcome_resource == "Increase Loyalty" %}
 {% set row = hubdb_table_row(121356853, 191349040224) %}
{% elif module.business_outcome_resource == "Win Mindshare and Market Share" %}
 {% set row = hubdb_table_row(121356853, 191349738266) %}
{% elif module.business_outcome_resource == "Improve Experience" %}
 {% set row = hubdb_table_row(121356853, 191349040228) %}
{% elif module.business_outcome_resource == "Increase Engagement" %}
 {% set row = hubdb_table_row(121356853, 191349738267) %}
{% elif module.business_outcome_resource == "Measure Program Success" %}
 {% set row = hubdb_table_row(121356853, 191349738268) %}
{% elif module.business_outcome_resource == "Become Compliant" %}
 {% set row = hubdb_table_row(121356853, 191349040230) %}
{% elif module.business_outcome_resource == "Optimize Margins" %}
 {% set row = hubdb_table_row(121356853, 191349040231) %}
{% elif module.business_outcome_resource == "Centralize Ecosystem Orchestration" %}
 {% set row = hubdb_table_row(121356853, 191349738271) %}
{% elif module.business_outcome_resource == "Consolidate User Experience" %}
 {% set row = hubdb_table_row(121356853, 191349738272) %}
{% elif module.business_outcome_resource == "Align Sales and Marketing" %}
 {% set row = hubdb_table_row(121356853, 191349738273) %}
{% elif module.business_outcome_resource == "Incentivize Learning and Education" %}
 {% set row = hubdb_table_row(121356853, 191349738274) %}
{% elif module.business_outcome_resource == "Tado" %}
 {% set row = hubdb_table_row(121356853, 191842089330) %}
{% endif %}

and replce it with something like this:

{% if module.business_outcome_resource == "business_outcome" %}
{% set row = module.hubdbrow_field.columns(business_outcome) %}
{% endif %}

<div class="dnd-section" style="padding-top: 0px;">
 <div class="row-fluid">
 <div class="resource-featured resource-featured--default">
 <div class="resource-featured__container" style="background: rgba({{ module.style.background_color.color|convert_rgb }}, {{ module.style.background_color.opacity / 100 }});">
 <h2 class="">{{ row.intro }}</h2> 
 <div class="resource-featured__resource"> 
 <img src="{{ row.image.url }}" alt="{{ row.name }}" class="resource-featured__image" loading="lazy" style="max-width: 100%; height: auto;">
 <div class="resource-featured__content">
 <p class="tag type">{{ row.type.label }}</p>
 <h3 class="resource-featured__heading">{{ row.heading }}</h3>
 <div class="resource-featured__description">{{ row.description }}</div>
 <div class="resource-featured__buttons mt-sm">
 <a href="{{ row.button1_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button1_text }}
 </a>
 <a href="{{ row.button2_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button2_text }}
 </a>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
</div>

business_outcome_resource is the module select dropdown.

business_outcome is the HubDB column

The above code isn’t currently correct but I’m not sure on how to write it to keep it simple with just one if statement.

Thanks in advance for any assistance.

@BarryGrennan @Anton @Jaycee_Lewis @kosalaindrasiri
Hi all, I was just checking in to see if you are able to point me in the right direction regarding how to reduce the need to manually add if stamements for each item. Am I mssing something or will I have to keep adding more if statements in the future. Ideally if I just add a new dropdown that would be better. So the code would cycle through until it matched the selected item. Thanks in advance.

Hey @Woodsy. I feel like @BarryGrennan @Anton gave us the best answers here. I’ll leave it up t them to follow if they have any additional suggestions. — Jaycee

Hi @BarryGrennan @Anton

I’m just reopening this project had was wondering if when you have time could you let me know if I am on the right path to getting this working?
I have replace the code with a more simpler if comand based on Barrys previous post. Unfotunatly it isn’t pulling in the data based on which business outcome is selected. Here is the code that I have changed it to:

{% if module.business_outcome_resource == "business_outcome" %}
{% set row = module.hubdbrow_field.columns(business_outcome) %}
{% endif %}

<div class="dnd-section" style="padding-top: 0px;">
 <div class="row-fluid">
 <div class="resource-featured resource-featured--default">
 <div class="resource-featured__container" style="background: rgba({{ module.style.background_color.color|convert_rgb }}, {{ module.style.background_color.opacity / 100 }});">
 <h2 class="">{{ row.intro }}</h2> 
 <div class="resource-featured__resource"> 
 <img src="{{ row.image.url }}" alt="{{ row.name }}" class="resource-featured__image" loading="lazy" style="max-width: 100%; height: auto;">
 <div class="resource-featured__content">
 <p class="tag type">{{ row.type.label }}</p>
 <h3 class="resource-featured__heading">{{ row.heading }}</h3>
 <div class="resource-featured__description">{{ row.description }}</div>
 <div class="resource-featured__buttons mt-sm">
 <a href="{{ row.button1_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button1_text }}
 </a>
 <a href="{{ row.button2_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button2_text }}
 </a>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
</div>

Thanks in advance for any help you are able to offer.

There’s no need for an if statement.

{% set row = module.hubdbrow_field.columns %}

I’d suggest reading up on on how HubDb Row Fields work


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

Hi, I’m thinking there may more complexity around this.

This is the Glossary DB that has a business_outcome_resource column.

Depending on what is selected here then pulls that row content from the Feature Resource DB. This is the feature resource DB.

This is the current code that works but rather than keep adding if statements every time a new resource is added I was looking to shorten the code so that it would accept any new row from the feature resource DB:

{% if dynamic_page_hubdb_row.business_outcome_resource.label == "Increase Sales" %}
 {% set row = hubdb_table_row(121356853, 191295276975) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Increase Loyalty" %}
 {% set row = hubdb_table_row(121356853, 191349040224) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Win Mindshare and Market Share" %}
 {% set row = hubdb_table_row(121356853, 191349738266) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Improve Experience" %}
 {% set row = hubdb_table_row(121356853, 191349040228) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Increase Engagement" %}
 {% set row = hubdb_table_row(121356853, 191349738267) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Measure Program Success" %}
 {% set row = hubdb_table_row(121356853, 191349738268) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Become Compliant" %}
 {% set row = hubdb_table_row(121356853, 191349040230) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Optimize Margins" %}
 {% set row = hubdb_table_row(121356853, 191349040231) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Centralize Ecosystem Orchestration" %}
 {% set row = hubdb_table_row(121356853, 191349738271) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Consolidate User Experience" %}
 {% set row = hubdb_table_row(121356853, 191349738272) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Align Sales and Marketing" %}
 {% set row = hubdb_table_row(121356853, 191349738273) %}
{% elif dynamic_page_hubdb_row.business_outcome_resource.label == "Incentivize Learning and Education" %}
 {% set row = hubdb_table_row(121356853, 191349738274) %}
{% endif %}

The if statements look up business_outcome_resource column in the Glossary DB then selects the Feature Resource DB (121356853). The last number string is the row in the Feature Resource DB.

I am looking for the code to firstly know which business_outcome_resource has been selected in the Glossary DB and then select the row with the same business_outcome in the Feature Resource DB.

Thanks in advance for any help you are able to offer.

I have found a way to reduce the if statement by using the for statement as follows:

{% set CategoryParam = "&business_outcome=" + dynamic_page_hubdb_row.business_outcome_resource.label %}
{% for row in hubdb_table_rows(121356853, CategoryParam) %}

<div class="dnd-section" style="padding-top: 0px;">
 <div class="row-fluid">
 <div class="resource-featured resource-featured--default">
 <div class="resource-featured__container">
 <h2 class="">{{ row.intro }}</h2> 
 <div class="resource-featured__resource"> 
 <img src="{{ row.image.url }}" alt="{{ row.name }}" class="resource-featured__image" loading="lazy" style="max-width: 100%; height: auto;">
 <div class="resource-featured__content">
 <p class="tag type">{{ row.type.label }}</p>
 <h3 class="resource-featured__heading">{{ row.heading }}</h3>
 <div class="resource-featured__description">{{ row.description }}</div>
 <div class="resource-featured__buttons mt-sm">
 <a href="{{ row.button1_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button1_text }}
 </a>
 <a href="{{ row.button2_url }}" class="button button--primary resource-featured__button resource-featured__button--cta">
 {{ row.button2_text }}
 </a>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
</div>
{% endfor %}