How to write if statement in Jelly Code for Object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 12:46 AM
Hello,
I have the following code: <j2:forEach items="$[jvar_grCategories]" var="jvar_json"> , this should return N object so i need to write an if statement if( jvar_json.type == "car" ) then print "Type:Car".
I wrote the following code but the code is not working?
<j2:if test="$[ jvar_json.type == 'car']">
Type : $[jvar_json.type]
</j2:if>
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 01:10 AM
Please refer below - Lets see if this can help you
https://www.servicenow.com/community/developer-forum/if-condition-in-jelly-script/m-p/2112663
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2022 02:35 AM
@Saif7 Tried and tested solution.
I have added the full code I tried below.
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_grCategories">
var categories = [{
"type":"car"
},{
"type":"truck"
}];
categories;
</g:evaluate>
<j:forEach var="jvar_json" items="${categories}">
<j:if test="${jvar_json.type == 'car'}">
Type : ${jvar_json.type}
</j:if>
</j:forEach>
</j:jelly>
Result:
Note:
When I used jvar_grCategories it was not working but when I used categories it worked. So please try like this. If it does not work out then please post full code.
Please mark as correct answer if this solves your issue.