How to check if one of field was updated in Script Include ?

Serhii5
Mega Guru

Hi guys, is it possible to check if one of the fields was updated in Script Include ? im populate fields with script include rest api data, and i need to check if fields the same - do not update it

1 ACCEPTED SOLUTION

Hi,

Since you have multiple record sys_ids you need to iterate over each record and check

Also if you want to check for multiple fields then you need to check them one by one and if not same then update

sample script below:

as.addQuery('id', 'IN', ids);

as.query();

while(as.next()){

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

if(as.u_account_status != result[i].AccountStatus)

as.u_account_status = result[i].AccountStatus;

if(as.u_account_status_name != result[i].AccountStatusName)

as.u_account_status_name = result[i].AccountStatusName;

as.update();

}

}

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

12 REPLIES 12

Hi,

Since you have multiple record sys_ids you need to iterate over each record and check

Also if you want to check for multiple fields then you need to check them one by one and if not same then update

sample script below:

as.addQuery('id', 'IN', ids);

as.query();

while(as.next()){

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

if(as.u_account_status != result[i].AccountStatus)

as.u_account_status = result[i].AccountStatus;

if(as.u_account_status_name != result[i].AccountStatusName)

as.u_account_status_name = result[i].AccountStatusName;

as.update();

}

}

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

Oh, thanks, it will long condition

Small correction in script; it should be like this

First use for loop for json object and for each for loop query the target record with that result ID and then inside the while check for fields

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

var as = new GlideRecord('table');

as.addQuery('id', result[j].ID.toString());

as.query();

while(as.next()){

if(as.u_account_status != result[j].AccountStatus)

as.u_account_status = result[j].AccountStatus;

if(as.u_account_status_name != result[j].AccountStatusName)

as.u_account_status_name = result[j].AccountStatusName;

as.update();

}

}

}

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

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

Maybe be better to iterate throw as and result with “for in” and check all keys with one line condition?

Hi,

i believe iterating over the json would be easier since you can query with the particular request ID in the addQuery with the request ID

If you iterate over using addQuery('id','IN', ids)

then it would be difficult to keep track of which result ID you need to use for value comparison

Regards
Ankur

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