FAQ: How can I check for two different values using a handlebars expression?

Comments

11 comments

  • PSA Admin

    Did you really just post this a week ago? Good timing again!

    0
  • Courtney Jordan Experience Strategy & Design Director Community moderator
    Celigo University Level 4: Legendary
    Awesome Follow-up
    Top Contributor

    Thanks, PSA Admin (aka Jim!) And thank you for all of your help in strengthening our community! We're in the process of going through customer questions on handlebars and will be getting more of these out as soon as we can! If you have any tips and tricks, please share!

    0
  • Ariel Browne
    Engaged

    Hi Courtney Jordan, how would this look for a multi-nested statement with 'AND' functionality?

    For example:
    - If sales_term_months = 36 AND Billing_Frequency = Annual THEN Invoice_Count = 3 otherwise...
    - If sales_term_months = 12 AND Billing_Frequency = Annual THEN Invoice_Count = 1 otherwise...
    - If sales_term_months = 12 AND Billing_Frequency = semi-Annual THEN Invoice_Count = 2 otherwise...
    - Invoice_Count = unknown


    0
  • Stephen Brandt Documentation Manager Community moderator
    Celigo University Level 4: Legendary
    Engaged
    Great Answer
    Top Contributor

    Hi, Ariel Browne. This is as compact of a way as I could find to make a handlebars statement that evaluates correctly for all four cases above: 

    {{#compare sales_term_months "==" 36}}{{#compare Billing_Frequency "==" "Annual"}}3{{else}}unknown{{/compare}}{{else}}{{#compare sales_term_months "==" 12}}{{#compare Billing_Frequency "==" "Annual"}}1{{else}}{{#compare Billing_Frequency "==" "semi-Annual"}}2{{else}}unknown{{/compare}}{{/compare}}{{else}}unknown{{/compare}}{{/compare}}

    The "low-code" solution simply doesn't offer, to my knowledge, the familiar elseif() or if(x && y) or select-case logic. 

    Perhaps it will help to see it with whitespace (which you'd want to remove for real-world JSON or import mapping) to understand how the nested statements are structured. 

     

    0
  • Ugene Jude Channel Partner
    Engaged
    Celigo University Level 4: Legendary

    Hi Team,

    May be I am totally missing a very simple aspect. Help is much appreciated on the below.

    can we apply compare to multiple values...like

    if  value of country= US or CA or UK, x, else y

    I couldnt get around having that "or"

    in handlebars

    0
  • Stephen Brandt Documentation Manager Community moderator
    Celigo University Level 4: Legendary
    Engaged
    Great Answer
    Top Contributor

    Hello, Ugene Jude. I confirmed the nested approach just above with a support supervisor, and it applies in your case as well. There is no "or" allowed within {#compare}, {#contains}, or {#if} – by design. 

    There might be an easier approach, though, depending on what you're trying to accomplish. Could you perhaps apply this logic in import mapping, in a static lookup "table?" For example,

    US = X
    CA = X
    UK = X

    Then, set the default value to Y for everything else? That's assuming you're not trying to build a JSON or XML template in an Advanced field editor, in which case handlebars or JavaScript would be your best and only approaches. 

    0
  • Ugene Jude Channel Partner
    Engaged
    Celigo University Level 4: Legendary

    Thank you for the quick response Stephen. Our else requirement further checks other fields from the payload. So the intention was to check for feasibility of an OR to at least shorten the compare block. The information yo  have clarified that part. Thanks again.

    0
  • Boss Admin
    Awesome Follow-up
    Engaged

    Hi Stephen Brandt,

    I am using the compare function on three export fields on import mapping step into NetSuite. When I run the flow I get errors that the values for these fields are not set.

    Following is my source export data from a custom REST API.

    {
    "success": true,
    "message": "Styles ready to push are retrieved",
    "errors": "",
    "result": {
    "styles": [
    {
    "item_number": "30005-11",
    "upc_code": "01915173000511",
    "display_name": "No-Promo-Style-SA XXS",
    "item_category": "Outerwear",
    "launch_event": "Mainline",
    "promo": "No"
    },
    {
    "item_number": "30005-12",
    "upc_code": "01915173000512",
    "display_name": "No-Promo-Style-SA XS",
    "item_category": "Outerwear",
    "launch_event": "Mainline",
    "promo": "No"
    },
    {
    "item_number": "30005-13",
    "upc_code": "01915173000513",
    "display_name": "No-Promo-Style-SA S",
    "item_category": "Outerwear",
    "launch_event": "Mainline",
    "promo": "No"
    },
    {
    "item_number": "30005-14",
    "upc_code": "01915173000514",
    "display_name": "No-Promo-Style-SA M",
    "item_category": "Outerwear",
    "launch_event": "Mainline",
    "promo": "No"
    }
    ]
    }
    }

    Here is the import mapping definition image.

    Here is the screenshot of error. 

    I tried result.styles.[*].promo expression as well and it resulted in invalid handlebar expression.

    Please advise what am I doing wrong here? Thank you.

    0
  • Kate Larson Senior UX Researcher Community moderator

    Hi Boss Admin,

    I checked with one of our handlebars experts. He says the issue is that you're using wildcards in your expression. You'll need to specify the exact indices for the values you want to match. I hope that helps!

    0
  • Boss Admin
    Awesome Follow-up
    Engaged

    Hi Kate Larson,

    That seems a bit strange. The same kind of expression ( result.styles[*].field ) is used to map other fields and these are working fine. The difference here is that I am using the #compare function. So, If I understand it correctly, the expert is saying that the handlebars won't accept wildcard while "standard" filed  mappings work with wildcard? I am using a similar expression on record lookup to ignore existing records. Also, when processing multiple records as part of import mapping, how can one give specific indexes while doing the mapping?

    Appreciate your help. Thank you.

    0
  • Kate Larson Senior UX Researcher Community moderator

    Hi Boss Admin,

    I was understanding this to be similar to this situation: https://docs.celigo.com/hc/en-us/community/posts/360077557172-Trouble-with-compare-handlebar

    There is another post that may help here: https://docs.celigo.com/hc/en-us/community/posts/360076262051-Handlebars-with-helper-doesn-t-work-for-array-elements-does-it-

    However, so that you can get the best guidance with your specific issue, I recommend opening a Support ticket.

    Thanks

    0

Please sign in to leave a comment.