FAQ: How can I substitute a default value when a handlebars expression for a field value evaluates to null?

Q: After field mapping using a handlebars expression, I’m getting a null value in that field sometimes, which is causing errors. I need to check whether the first 31 characters of a field, lastname, are null. If so, I want to substitute the field value of the firstname field instead. How can I check whether the result of the field mapping handlebars expression is null, then substitute a default value for that null value?

A: Try this:

{{#if lastname}}{{substring lastname "0" "30"}}{{else}}{{firstname}}{{/if}}

#if checks whether the first 31 characters of the lastname field has a value or not. If not, it substitutes the value of the firstname field.

There's a lot more information on handlebars in our handlebars reference guide. Check it out and let us know what we can do to improve it!

These handlebars questions are all coming from other customers, so please let us know what questions you have and how we can help! Happy handlebarring!

 

0

Comments

2 comments
Date Votes
  • What does this expression do if the first 31 char are not null?  Return the whole lastname field value, or the substring, or...?

    Trying to put this syntax in natural language, because this from the documentation didn't do it for me:

    If the argument in the {{#if field}} is true, then it prints the value from the context. If the argument is false (either undefined, null, " ", 0, or [ ] ), then the block prints the else condition. 

    {{#if lastname}}{{substring lastname "0" "30"}}{{else}}{{firstname}}{{/if}}

    Check the field {lastname}.  If the substring specified returns any valid value (Because there is no logical condition included such as =),  output value from {lastname}.  Else output value from field {firstname}.  ?

    Or: Output value from {lastname} IF the substring specified returns any valid value (Because there is no logical condition included such as =).  Else output value from field {firstname}.  ?

    Or: Check the field {lastname}.  If the substring specified returns any valid value (Because there is no logical condition included such as =),  output substring specified.  Else output value from field {firstname}.  ?

    Not sure what work putting lastname in the first brackets with #if is doing?  #2 seems most likely to me.  Is it specifying the input to the argument?  Or the output?  Or both?  There is no condition here which can be 'True' or 'False' in the logical sense, so it's tripping me up.  Something like the below would make sense to me as an argument, checking is the field lastname longer than 31 characters?  If it is, the expression would be F, if not, the values match and it would evaluate T.   

    {{#if lastname}} = {{substring lastname "0" "30"}}
    0
  • Hi Rob Riccio,

    For clarification, this is a nested expression that uses two different handlebars expressions that can be considered independently: #if_else and substring.

    For #if_else, if the argument in the {{#if field}} is false (meaning it is undefined, null, " ", 0, or [ ]), then the block prints the else value. If the argument is true, the else condition is never triggered and the string prints as received from the source data.

    The secondary nested substring expression only limits the value returned by the "true" value of the #if_else to a maximum of 31 characters.

    So for this: 

    {{#if lastname}}{{else}}{{firstname}}{{/if}}

    If the record returns a value for the lastname field of any length, it's prints that value (otherwise it prints whatever returns in the firstname field). 

    Adding the substring expression: 

    {{#if lastname}}{{substring lastname "0" "30"}}{{else}}{{firstname}}{{/if}}

    The same behavior happens with the exception that the value will be limited to 31 characters at maximum.

    Only the first 31 characters of any returned lastname value will return. In both cases, the firstname prints only if the lastname value is undefined, null, " ", 0, or [ ].

    Is that less confusing?

    1

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post