How to add IF condition for Json script in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2022 03:53 AM
How to add IF condition to the below Json script. I want to include another catalog item for the button in the portal based on Logged in user Company
{
"href": {
"value": "?id=sc_cat_item&sys_id=5846caba1bd3a45069c3206e3b4bcb38",
"displayValue": "?id=sc_cat_item&sys_id=5846caba1bd3a45069c3206e3b4bcb38"
},
"button_text": {
"value": "Click here to open a New Case",
"displayValue": "Click here to open a New Case"
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2022 04:32 AM
Hi Avinash,
Assuming you will put this JSON in some variable, the script will be as follows:
var sCatalogItemUrl = "";
if (<condition pass>) {
sCatalogItemUrl = "?id=sc_cat_item&sys_id=5846caba1bd3a45069c3206e3b4bcb38";
}
else {
sCatalogItemUrl = "<Another catalog item url>";
}
var oJson = {
"href": {
"value": sCatalogItemUrl ,
"displayValue": sCatalogItemUrl "
},
"button_text": {
"value": "Click here to open a New Case",
"displayValue": "Click here to open a New Case"
}
}
gs.print(JSON.stringify(oJson));
Please mark the answer correct if it helps. 🙂
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2022 05:02 AM
Hi,
So what script did you start and where are you stuck?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader