How to add IF condition for Json script in ServiceNow

Avinash16
Tera Contributor

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"
    }
}

2 REPLIES 2

H_9
Giga Guru

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.

Thanks. 🙂

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So what script did you start and where are you stuck?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader