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

Thanks for your continued support.

After adding the above mentioned lines related to comments , But still we're getting the same error.

LaraReddy_0-1688729649135.png

 

 

Advance thanks.

@LaraReddy 

can you add log for this and share how the request body looks like

(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, '');

gs.info('notes is' + notes);

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.info('Request body is' + r.getRequestBody()); // check this

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

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

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

Hi Ankur,

Thanks for the update.

Entered work notes are not showing on logs level but it's not passing to target instance.
Please find the below logs steps:

Notes logs:

 

LaraReddy_0-1688732887941.png

 

Request body is logs:
LaraReddy_1-1688732969866.png

 

 

Advance thanks.

 

@LaraReddy 

it seems work notes has data and is included correctly in the request body but has new line character

try to remove that and test once

Also try to use POST method instead of PATCH

var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
notes = notes.replace(dateRE, '');

notes = notes.replace('\n',"");

gs.info('notes is' + notes);

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

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

Hi Ankur,
Tried your suggestions. But now logs level notes coming as empty and also tried with post method same notes coming as blanck.

LaraReddy_0-1688737166772.png

 

pls check Script once:

(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, '');
    notes = notes.replace('\n',"");
 gs.info('notes is' + notes);
 
 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.info('Request body is' + r.getRequestBody()); // check this


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

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

 

Advance thanks.