Set response body in REST API

anu64
Tera Contributor

Incident management integration, when source instance creates an incident in target instance via web service (REST API). on creation, in target instance it should send a response which contains the incident number and incident sys_id. how can I do this from business rule.

4 REPLIES 4

Monika2
Giga Guru

Hi Anu,

You can copy the code from Preview script usage in REST Message and paste the same in your business rule.

You can create async business rule for same.

PFB code for your reference

try {
var r = new sn_ws.RESTMessageV2('Demo','Create Incident');
r.setStringParameterNoEscape('Description',current.description);
r.setStringParameterNoEscape('impact',current.impact);
r.setStringParameterNoEscape('short_description',current.short_description)

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

var responseObj = JSON.parse(responseBody); //Parse response body
var inc = responseObj.number; // get the incident number of target instance



current.correlation_id = inc; // save the number in correlation id field

current.setWorkflow(false);
current.update();
}

catch(ex) {
var message = ex.getMessage();

}

 

Hope it helps.

 

Regards,

Monika

MrMuhammad
Giga Sage

Hi @anu ,

Please help me understand few things.

  • When incident in getting created on the target instance are you storing sys_id of source incident in correlation id of target incident?
  • What field is set as colesce on the target/source instances? 
  • The setup I have currently in that we are setting the number of source incident into the correlation display of target incident. Are you following the same or otherwise what is the process or which field is being used to for mapping number of source incident with target incident?

Thanks & Regards,

Sharjeel

Regards,
Muhammad

anu64
Tera Contributor

yes we are storing the source incident number in the correlation display field in the target instance

 

So this is somthing we are doing. Please see the documentation in the code and adjust as per your need.

 

 // u_correlation_display --> Correlation Dipslay field name in Staging table of target table
 // u_correlation_id --> Correlation ID field name in Staging table of target table
 // u_number --> Number field name in Staging table of target table
 // SERVICE ACCOUNT NAME --> Account created in source incident and used for integration
 // reqStr --> YOur request body send. it to the target instance using your Rest API.


 // Incident created by another instance: Send and store sys_id of the inserted record in the correlation display and number in correlation id of the corresponding record in the source instance
   if (current.operation() == 'insert' && current.sys_created_by == 'SERVICE ACCOUNT NAME') {
        
        var reqObj = {};
	 reqObj.u_correlation_id = current.sys_id.toString();
        reqObj.u_correlation_display = current.number.toString();
        reqObj.u_number = current.correlation_id.toString();

        var reqStr = JSON.stringify(reqObj);

   }

Please note - we are using post Rest method for both insert/update. So when we make Post request to update the record back, we send number back and in the target web service we set number as colesce field. so it matches the recieved number and update the target record with correlation display with number of source incident and correlation id with sys_id of source incident. 

Regards,
Muhammad