JEXL evaluating conditions differently to Javascript

johndewhurst
Kilo Explorer

Within an email notification script, there is a section for including variables associated to a request, within this section there is a conditional that filters out variables where either the label or the value is blank (or false)...

        gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text && gr_sc_item_option_mtom.sc_item_option.value

This works, however when trying to port the equivalent logic in jelly (as a JEXL expression) it always resolves as false unless gr_sc_item_option_mtom.sc_item_option.value is boolean value true this is what I have...

        <j2:if test="$[gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text &amp;&amp; gr_sc_item_option_mtom.sc_item_option.value]">

This is my first UI so I expect this to be a basic error, have tried the following as well...

<j2:if test="$[gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text &amp;&amp; (gr_sc_item_option_mtom.sc_item_option.value||(gr_sc_item_option_mtom.sc_item_option.value.length>0))])">

and

<j2:if test="$[gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text &amp;&amp; (gr_sc_item_option_mtom.sc_item_option.value||gr_sc_item_option_mtom.sc_item_option.value.notNil())])">

So basically I need a JEXL expression that evaluates anything not Null, empty or false as true

1 ACCEPTED SOLUTION

John,



Nope..... I spoke too soon. None of that is true except for the statement that does not have a strike through it.



I did some digging around because I wasn't familiar with using "notNil()". However, I have used "nil()". I may be wrong but it seems that "nil()" can be used both with the JSUtil api or not. But I've never seen notNil() used without the JSUtil api.



So I did some experimenting and these are what worked for me:



<j2:if test="$[ JSUtil.notNil(gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text)&amp;&amp; JSUtil.notNil(gr_sc_item_option_mtom.sc_item_option.value) ]">




or




<j2:if test="$[ !gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text.nil() &amp;&amp;!gr_sc_item_option_mtom.sc_item_option.value.nil() ]">




or


<j2:if test="$[ !JSUtil.nil(gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text) &amp;&amp; !JSUtil.nil(gr_sc_item_option_mtom.sc_item_option.value) ]">



In conclusion appending ".notNil()" at the end wasn't spitting out neither true nor false when checking the value of either conditional that had it.


View solution in original post

9 REPLIES 9

ChrisBurks
Mega Sage

I believe jelly interprets the variables differently than in the regular javascript environment. Jelly is basing the conditional as if it's an existing object and not necessarily whether there is a value held in that object. For instance if you were to just have your first conditional,


gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text, as the only condition in the jelly if statement it will always evaluate to true whether it has a value in it or not. This supports your statement that your original if statement only evaluates to true when the second conditional is true.


Examples:


ifcondition1&&condition2Result
Case onetruefalsefalse
Case twofalsefalsefalse
Case threefalsetruefalse
Case fourtruetruetrue


So your jelly if statement is limited to cases one and two since it's interpreting the first conditional as always true since it's an existing object. It just doesn't have any value.


You might have to change your expression to something like:



<j2:if test="$[!gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text == '' &amp;&amp; gr_sc_item_option_mtom.sc_item_option.value]">



In that example your first conditional will now have two possible outcomes where if it has a value it will evaluate to true and if it's the same as blank it will evaluate to false.


Was looking for something along the lines of notNil() to catch null as well (don't think I need to worry about undefined really), tried that and that did not work either, is there any reason why that wouldn't or could there be another bug I've overlooked?


Where did you try the notNil()? I see in your original post you used it on the second condition. Did you try on the first as well? Or you might have to use it on both conditionals.


Yeah I tried on both...


<j2:if test="$[gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text.notNil() &amp;&amp; gr_sc_item_option_mtom.sc_item_option.value.notNil()]">