- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 12:12 AM
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 && 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 && (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 && (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
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2015 08:44 PM
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)&& 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() &&!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) && !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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 09:42 AM
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:
if | condition1 | && | condition2 | Result |
---|---|---|---|---|
Case one | true | false | false | |
Case two | false | false | false | |
Case three | false | true | false | |
Case four | true | true | true |
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 == '' && 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 12:18 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 01:00 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 01:14 PM
Yeah I tried on both...
<j2:if test="$[gr_sc_item_option_mtom.sc_item_option.item_option_new.question_text.notNil() && gr_sc_item_option_mtom.sc_item_option.value.notNil()]">