Help! I’m trying to convert a string to an int but not having any joy:
This is what I’m doing:
{% set event_id = ‘{{ p145771107_hapily_registrant.event_id |int}}’ %}
Where event_id is a single line text field on the event registrant object. But it returns zero:
If I hardcode an example event id value it works e.g.
{% set event_id = “167207073994”|int %}
I’ve tried trimming etc. but it doesn’t seem to want to convert it into an int
Hi @JWright76 ,
The issue you’re facing with converting a string to an integer in HubL is a common one, especially when working with dynamic data like a custom object’s event_id.
The main problem is the use of quotes and {{ }} inside your {% set %} statement. Those are only needed when you’re outputting values, not when you’re assigning them.
Instead of {% set event_id = ‘{{ p145771107_hapily_registrant.event_id |int }}’ %}, use
{% set event_id = p145771107_hapily_registrant.event_id |int %}
This will correctly apply the |int filter and store the result as an integer.
Thanks for getting back @ArisudanTiwari . I did actually try that but still no joy, although having output the length of the strings it seems there’s something going on with the event_id field
{% set event_id = p145771107_hapily_registrant.event_id | string | trim %}
{% set event_id_text = “167207073994” | string | trim %}
event_id length: {{ event_id | length }}<br>
event_id_text length: {{ event_id_text | length }}<br>
EventId: {{ event_id }} <br>
EventId (text): {{ event_id_text }} <br>
Outputs:
event_id length: 41
event_id_text length: 12
EventId: 167207073994
EventId (text): 167207073994
so no idea what the extra chars are in the event_id field
Thanks for all responses on this. I actually got this working by simplly enabling programmable email for the module! It’s a bit odd the way it failed without this enabled, a ‘you’re trying to using functions that require programmable email’ message might be nice
Hi @JWright76 ,
Try the this code -
{% set event_id = p145771107_hapily_registrant.event_id | string | trim %}
{% set event_id_clean = event_id | replace(‘[^0-9]’, ‘’, ‘g’) %}
{% set event_id_int = event_id_clean | int %}
Cleaned EventId: {{ event_id_clean }}<br>
As Integer: {{ event_id_int }}
Thanks, but it’s giving me a cannot resolve property error in Design Manager.
I’m actually convinced that despite me using
{% set event_id = p145771107_hapily_registrant.event_id %}
It’s still only setting event_id to be the literal value of: “{{p145771107_hapily_registrant.event_id}}”
as this is actually 41 chars long. I’ve tried using render filter also, but with no luck. So now I’m probably going to use a different approach now and just stamp the fields I need onto the event registration record, I’d prefer to be able to pull it straight from the event record, but think when you start having to do things like trim and regex replace the code becomes a bit prone to breaking. I will investigate further when have more time.
Hey, @JWright76, thanks for the fantastic question. I think you’ve gotten yourself pretty dang close here.
What about, instead of trying to assign the property directly, create it as a string first and then using the render filter to process it? This explicitly tells HubL to treat the contents of the string as HubL and to execute it.
{% set event_id_string_token = "{{ p145771107_hapily_registrant.event_id }}" %}
{% set rendered_event_id = event_id_string_token|render|trim %}
{% set event_id_int = rendered_event_id|int %}
If that fails, at least we’ll have another clue to work with. Have fun testing! — Jaycee
HUBL: converting string to int
Hi @JWright76
The issue is with how you’re using quotes and {{ }} in your {% set %} statement. In HubL:
{{ }}is used to output a value to the page.{% %}is used to assign or process logic.
So, this line is incorrect:
{% set event_id = ‘{{ p145771107_hapily_registrant.event_id |int }}’ %}
This returns 0 because you’re actually converting the string literal '{{ p145771107_hapily_registrant.event_id |int }}' — not the real value.
Use this instead:
{% set event_id = p145771107_hapily_registrant.event_id | int %}
This properly fetches the event_id, converts it to an integer using the |int filter, and assigns it to event_id.
Let me know if you want to debug specific values or check if the field is null or empty before converting.
