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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please explain with some example

Thanks
Ankur

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

I have script include with get rest message, I don’t need to always update records, only in case if one of field has changed value

Hi,

So you are consuming rest message of 3rd party.

If you only want to update record if any field has changed as compared to the response received you should be having some query on unique field and then compare the field value with the response field value then you can handle this

Example: Consider you want to compare short description received from REST Message against incident short description and update notes only when value is different

Sample Script:

var responseBody = '{"number":"INC001","short_description":"helloabc","notes":"notes123"}';

// parse the above json

var parser = JSON.parse(responseBody);

var responseInc = parser.number; // this gives INC001

var shortDescAfterParsingResponse = parser.short_description; // this gives helloabc

var inc = new GlideRecord('incident');

inc.addQuery('number', responseInc);

inc.query();

if(inc.next()){

if(inc.short_description != shortDescAfterParsingResponse){

// values are different so update notes

inc.notes = parser.notes;

inc.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

 

I tried this method but always have updated records, you mean, i need to check all fields independently ?

find_real_file.png