REST API - Patch and PUT methods to update multiple incident tickets.

LaraReddy
Tera Guru

Hi All,
We're integrated two servicenow instances to create the incident tickets when ever an incident ticket is created in one instance we need to replicate the same in another instance.
And we have used REST API - POST method and it's working fine now.

But we want to update the incident when ever we update the incidents in one instance need to set the same field info in another instance.

We're trying to use REST API  "PUT / Patch" methods, but it's not updating in another instance.

Note: It's working properly only when we give the ticket sys_id in endpoint.

But we have more than one incident tickets and we need to update only mirror incident ticket in another instance.

And sys_id are same in both the tickets on instance level.


Advance thanks.

22 REPLIES 22

@LaraReddy 

don't use test link; it won't work as there is no reference for current object.

please update actual record in source and let BR run and then verify in target

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

HI Ankur,
Thanks for the response.
It's working fine but the work notes are not moving to target instance.
Even though we got mentioned on BR level.

Advance thanks.

@LaraReddy 

Glad to know that it worked

for that you need to fetch latest work notes like this

r.setStringParameterNoEscape('comments', current.work_notes.getJournalEntry(1));
Please mark my response as correct and close the thread
Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HI Ankur,
If we add the above the line, then status of code on logs level coming as : 

This is update incident result: {"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}


Advance thanks.

@LaraReddy 

then do this

(function executeRule(current, previous /*null when async*/) {

	var r = new sn_ws.RESTMessageV2('CR instance', 'Update Incident');
	r.setEndpoint('https://dev1209.service-now.com/api/now/table/incident/' + current.sys_id);
	r.setStringParameterNoEscape('group',current.assignment_group);
	r.setStringParameterNoEscape('toPerson', current.assigned_to);
	r.setStringParameterNoEscape('state',current.state);
	r.setStringParameterNoEscape('callerDetails',current.caller_id);

	var notes = current.work_notes.getJournalEntry(1); // get the latest work notes
	var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
	notes = notes.replace(dateRE, '');	

	r.setStringParameterNoEscape('comments', notes); // this will just includet the text and remove the date timestamp etc
	r.setStringParameterNoEscape('shortDesc', current.short_description);
	//r.setStringParameterNoEscape('id',current.sys_id);

	var response = r.execute();
	var responseBody = response.getBody();

	gs.log("This is update incident result: " + responseBody);
	var httpStatus = response.getStatusCode();

	gs.log("Status of the update incident:" + httpStatus);
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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