Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to stop the for loop in client side

shaik_irfan
Tera Guru

Hi,

 

I have a script include which is returning me with the JSON object, my requirement is if i get atleast one flag as true then i need to throw an error if  not i need to set the server name's received from the server.

 

[{"flagg":false,"serverName":"Winodws Server 2003"},{"flagg":true,"serverName":"IIS"},{"flagg":false,"serverName":"Winodws Server 2004"}]

 

for above object result i have true flag for IIS so if there at least one true flag in the complete object result then i should throw an error if not i need to set value in Server name variable.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

try running this in scripts background; use similar in client script

var str = '[{"flagg":false,"serverName":"Winodws Server 2003"},{"flagg":true,"serverName":"IIS"},{"flagg":false,"serverName":"Winodws Server 2004"}]';

var parser = new JSONParser();

var parsedData = parser.parse(str);

var flag = false;

for(var i=0;i<parsedData.length;i++){

if(parsedData[i].flagg){
flag = true;
break;
}

}

if(flag){

// do whatever required

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shaik,

try running this in scripts background; use similar in client script

var str = '[{"flagg":false,"serverName":"Winodws Server 2003"},{"flagg":true,"serverName":"IIS"},{"flagg":false,"serverName":"Winodws Server 2004"}]';

var parser = new JSONParser();

var parsedData = parser.parse(str);

var flag = false;

for(var i=0;i<parsedData.length;i++){

if(parsedData[i].flagg){
flag = true;
break;
}

}

if(flag){

// do whatever required

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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