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

Harshad Wagh
Tera Guru

Hello,

Its working as expected, if you use the direct webservice available ootb on incident table you will need to pass sys_id of incident of target instance.

 

if you want to use anything else lets say, 'number' you will need to create your custom scripted rest api with put/patch method and by making use of gliderecord, identify the record and update it.

 

Hope this helps

 

Thanks

Harshad

Ankur Bawiskar
Tera Patron
Tera Patron

@LaraReddy 

if you are using OOB table API then you would require sysId only of the target incident record

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

LaraReddy
Tera Guru

Hi Both,
Thanks for the response.

PLease find the below steps what we're currently following on to update the target instance record.

Step1:

LaraReddy_0-1688721370698.png

 

Step 2:

LaraReddy_1-1688721445510.png

Step 3:
Business RUle -> Before - Update -> INC table level

Script:

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

var r = new sn_ws.RESTMessageV2('CR instance', 'Update Incident');
 r.setStringParameterNoEscape('group',current.assignment_group);
 r.setStringParameterNoEscape('toPerson', current.assigned_to);
 r.setStringParameterNoEscape('state',current.state);
 r.setStringParameterNoEscape('callerDetails',current.caller_id);
 r.setStringParameterNoEscape('comments', current.work_notes);
 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);

 



Apart for these steps we're not doing any additional steps.

SInce we're completely new to integrations, could you pls guide us how to update the target instance records.

Note: Through this steps we're able to update the target instance INC record only one because of the same record sys_id got mentioned on end point level.

Advance thanks.

@LaraReddy 

make the BR as after update

this BR will run on every record update so don't hard-code the record sysId

Instead use variable substitution and replace the sysId with the actual record sysid

OR
set the endpoint dynamically from script and remove the endpoint used in PATCH method

I would suggest 2nd way as it's easier

sharing sample script for that

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

var r = new sn_ws.RESTMessageV2('CR instance', 'Update Incident');
r.setEndpoint('https://dev75096.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);
r.setStringParameterNoEscape('comments', current.work_notes);
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