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 

did you try adding logs before the RegEx is the variable has value in it?

(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

gs.info("Latest notes" + 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);

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

@LaraReddy 

try adding logs and see what value comes here

(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

gs.info("Latest notes" + 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);

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

Amit Gujarathi
Giga Sage
Giga Sage

Hi @LaraReddy ,
I trust you are doing great.

I recommend implementing a solution using the following approach:

  1. Retrieve the mirror incident ticket sys_id in the other ServiceNow instance based on a unique identifier shared between the two instances. This identifier could be a common field value or a custom field that you set up.

  2. Whenever an incident ticket is updated in the first instance, make a request to the other instance's REST API to retrieve the sys_id of the mirror incident ticket by searching for the corresponding unique identifier.

  3. Once you have obtained the mirror ticket sys_id, use the REST API PUT/Patch method to update the necessary fields in the mirror incident ticket.

// Retrieve the mirror ticket sys_id based on a unique identifier
function getMirrorTicketSysID(uniqueIdentifier) {
  var gr = new GlideRecord('incident'); // Replace 'incident' with the appropriate table name
  gr.addQuery('unique_identifier', uniqueIdentifier); // Replace 'unique_identifier' with the actual field name
  gr.query();
  if (gr.next()) {
    return gr.getValue('sys_id');
  }
}

// Update the mirror incident ticket using the obtained sys_id
function updateMirrorTicket(uniqueIdentifier, updatedFields) {
  var mirrorSysID = getMirrorTicketSysID(uniqueIdentifier);
  if (mirrorSysID) {
    var gr = new GlideRecord('incident'); // Replace 'incident' with the appropriate table name
    gr.get(mirrorSysID);
    gr.update(updatedFields);
  }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi