Update incident from Scripted Rest API

Neha Agarwal
Giga Expert

My requirement is to update multiple fields on incident table through rest scripted API and the situation is any field can be updated during PUT call which means It is not necessary that all the fields mentioned inside update function will be updated at once. it is not working well if I do it through Scripted API as my glide records are pushing Blank data to the incident. Here is my code snippet, Can you suggest what is the best way to get this worked or I need to update it through import set?

for example if Group is not updated only priority is updated, How can I control this so that only modified fields gets updated not all.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var data = JSON.parse(request.body.dataString);

var group = data.assignment_group;
var impact = data.temp1;
var urgency = data.temp2;
var notes = data.work_notes;
var vendor_tkt = data.vendor_tkt;

//Set Assignement group based on Group name
if (group != "") {
var gp = new GlideRecord('sys_user_group');
gp.addQuery('u_legacy_group_name', group);
gp.query();
if (gp.next())
var gpid = gp.sys_id;
}
//set impact and Urgency

if(impact!="" && urgency!=""){
var imp, urg;
if (impact == "1-Extensive/Widespread" || impact == "2-Significant/Large")
imp = 1;

else if (impact == "3-Moderate/Limited")
imp = 2;

else if (impact == "4-Minor/Localized")
imp = 3;


if (urgency == "1-Critical" || urgency == "2-High")
urg = 1;

else if (urgency == "3-Medium")
urg = 2;

else if (urgency == "4-Low")
urg = 3;
}

var gr = new GlideRecord('incident');
gr.addQuery('third_party_reference', vendor_tkt);
gr.query();
if (gr.next())

gr.urgency = urg;
gr.impact = imp;
gr.assignment_group = gpid;
gr.work_notes = notes;
gr.update();

response.setStatus(200);

})(request, response);

1 ACCEPTED SOLUTION

Hi,

you can do this to check for emptiness and undefined

if(impact != '' && impact != undefined){

 

}

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

if the incoming value after parsing is not empty then only update that field

you can add that logic

if(urg){

gr.urgency = urg;

}

if(imp){
gr.impact = imp;

}

if(gpid){
gr.assignment_group = gpid;

}

if(notes){
gr.work_notes = notes;

}
gr.update();

Regards
Ankur

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

Hi Ankur, 

Thanks for Replying, the issue is when I do JSON parsing that time itself the result is 'undefined' if there is no value being passed, like in below example only impact is getting updated and I am gliding with blank group value any group which has blank  match will be picked up (Data is not so clean).

var data = JSON.parse(request.body.dataString);

var group = data.assignment_group;
var impact = data.temp1;
var urgency = data.temp2;
var notes = data.work_notes;
var vendor_tkt = data.vendor_tkt;

Is there a way to filter the fields during JSON parsing itself, if value is not being passed by third party then  I should not glide and update that field.

 

Thanks,Neha

Hi,

you can do this to check for emptiness and undefined

if(impact != '' && impact != undefined){

 

}

Regards
Ankur

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

Thanks Ankur for taking time to help, everything works well now.