I have integrated 2 servicenow instances, but on updating the incident, it is creating a new incident in endpoint instance.

ritesh1
Tera Contributor

I have integrated 2 servicenow instances using REST Api POST Method. When I'm creating a incident in Instance1(Source), the same incident is also getting created in Instance2(Endpoint). But when I'm updating the same incident in Instance1(Source), instead of updating the associated incident in Instace2(Endpoint); a new incident is getting created in Instance2(Endpoint)

 

Here is the business rule Im using in Instance1(Source):

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

   
    var title = current.short_description.toString();
    var description = current.description.toString();
    var category = current.category.toString();
    var caller = current.caller_id.getDisplayValue().toString();
    var ag = current.assignment_group.getDisplayValue().toString();
    var st = current.state.getDisplayValue().toString();
    var imp = current.impact.getDisplayValue().toString();

    var myObj = {
        "short_description": title,
        "description": description,
        "category": category,
        "caller_id" : caller,
        "assignment_group" : ag,
        "state": st,
        "impact" : imp
    };


    // Add your code here
    var request = new sn_ws.RESTMessageV2();
    request.setEndpoint('https://devxxxxx.service-now.com/api/now/table/incident');
    request.setHttpMethod('POST');

    //Eg. UserName="admin", Password="admin" for this code sample.
    var user = 'username';
    var password = 'password';

    request.setBasicAuth(user, password);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader('Content-Type', 'application/json');
    request.setRequestBody(JSON.stringify(myObj));
    var response = request.execute();
    gs.log(response.getBody());

})(current, previous);

3 REPLIES 3

Aman Kumar S
Kilo Patron

You need to have an update BR, which uses HTTP method PUT/Patch to update the record in 2nd instance.

Since you are using POST method, it is going to create a new incident and make above BR which you have created to run only on insert

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

 

Best Regards
Aman Kumar

Can you please help me with script or steps on how to do this.

asifnoor
Kilo Patron

Hi,

Firstly you need to keep track of the incident number or sys_id of your instance 2 in your 1st instance, only then you can update that incident. 

Here are steps to do it

1. Create a field in your incident instance2_sysid (string field) to store the instance2 sys id in this.

2. Then in your BR, make the following changes. make this before insert and update BR. Then use the below code. 

for update, you have to use put and also need to pass sysid in the end point. Made changes accordingly.

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

   

    var title = current.short_description.toString();

    var description = current.description.toString();

    var category = current.category.toString();

    var caller = current.caller_id.getDisplayValue().toString();

    var ag = current.assignment_group.getDisplayValue().toString();

    var st = current.state.getDisplayValue().toString();

    var imp = current.impact.getDisplayValue().toString();

    var myObj = {

        "short_description": title,

        "description": description,

        "category": category,

        "caller_id" : caller,

        "assignment_group" : ag,

        "state": st,

        "impact" : imp

    };


    // Add your code here

    var request = new sn_ws.RESTMessageV2();

   if(current.operation().toString() == "insert") {
    request.setEndpoint('https://devxxxxx.service-now.com/api/now/table/incident');

    request.setHttpMethod('POST');
} else if (current.operation().toString() == "update") {
    var sys_id = current.instance2_sys_id.toString(); //MENTION RIGHT FIELD NAME HERE.
var end point = "'https://devxxxxxx.service-now.com/api/now/table/incident/"+sys_id+"";
    request.setEndpoint(endpoint);
request.setHttpMethod('PUT');
}

    //Eg. UserName="admin", Password="admin" for this code sample.

    var user = 'username';

    var password = 'password';

    request.setBasicAuth(user, password);

    request.setRequestHeader("Accept", "application/json");

    request.setRequestHeader('Content-Type', 'application/json');

    request.setRequestBody(JSON.stringify(myObj));

    var response = request.execute();
    
var res = JSON.parse(response.getBody());
current.YOUR_INSTANCE2_SYSID=res.result.sys_id; //change to your field name correctly.

})(current, previous);

Mark the comment as a correct answer and helpful if this has helped to solve the probelm.