Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hello Guys,

Many Times we have requirement to fetch the data from different instance to our main instance. So I just come up with my new implementation of REST message integration on my PDI. It works for both insert and update on incident record.

Steps:-

1) Create User for Integration and give the below roles(On Target Instance):-

find_real_file.png

2) Create REST message(On source Instance):-

As shown on below screenshot write-down the correct API of your target instance table and create the post method for that as it used to insert records. Keep the authentication type as Basic only.

find_real_file.png

HTTP Method:-

After creating HTTP method make sure that you click on that Test button to check weather it returns correct HTTP success code or not.

find_real_file.png

 

3) Create Business Rule(On source Instance):-

3.1) Business Rule when Incident is insert:-  Also Handles Multiple Attachments and sends it to the target instance.

 When to Run:- After Insert

 

(function executeRule(current, previous /*null when async*/ ) {
var body = {
"short_description": current.getValue("short_description"),
"description": current.getValue("description"),
"urgency": current.getValue("urgency"),
"impact": current.getValue("impact"),
"state": current.getValue("state"),
//"sys_id":current.getValue("sys_id"),
"caller_id": current.getValue("caller_id")
};
try {
var r = new sn_ws.RESTMessageV2('Create Incident', 'post');
r.setRequestBody(JSON.stringify(body));
var response = r.execute();
var responseBody = response.getBody();
var json = JSON.parse(responseBody);

var parser = new JSONParser();
var parsedData = parser.parse(responseBody);
gs.log('correlation_id:-' + parsedData.result.sys_id);
current.correlation_id=parsedData.result.sys_id;
current.update();

var remoteInc_sys_id = json.result.sys_id;
var httpStatus = response.getStatusCode();
gs.log("Response status code is " + httpStatus);
if (httpStatus == "201") {
gs.log("Entered into the if conditon");
//Fetch attachment associated with the incident
var att = new GlideRecord("sys_attachment");
att.addQuery("table_sys_id", current.sys_id);
att.query();
//if you want to push all attachmetns, use while loop here
if (att.next()) {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("post");
attachmentMessage.setBasicAuth("UserID", "Password"); // Provide your integration user ID and Password
attachmentMessage.setEndpoint("https://dev89807.service-now.com/api/now/attachment/file");
attachmentMessage.setQueryParameter("table_name", att.table_name);
attachmentMessage.setQueryParameter("table_sys_id", remoteInc_sys_id);
attachmentMessage.setQueryParameter("file_name", att.file_name);
attachmentMessage.setRequestHeader("Content-Type", att.content_type);
attachmentMessage.setRequestHeader("Accept", "application/json");
attachmentMessage.setRequestBodyFromAttachment(att.sys_id);
response = attachmentMessage.execute();
responseBody = response.getBody();
httpStatus = response.getStatusCode();
}
}
} catch (ex) {
var message = ex.message;
}
})(current, previous);   

 

3.2) Create Business Rule on Update :-  

When to Run:- Before Update  

 

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

// Add your code here
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://dev89807.service-now.com/api/now/table/incident/'+current.correlation_id);
request.setHttpMethod('PUT');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'Admin';
var password = 'Password'; // Provide your integration user ID and Password

request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody("{\"work_notes\":\""+ current.work_notes +"\"}");
gs.log(current.work_notes);
var response = request.execute();
gs.log(response.getBody());

})(current, previous);  

 

 

If you are facing some issues then please comment on this post. I will try to resolve it as early as possible.

Please Mark my article as Helpful If it added some value to your knowledge.

 

Kind Regards,

Gunjan Kiratkar

Comments
AjayKumarS
Tera Explorer

Hi Gunjan,

 it was very helpful with the code you have provided for insert and update from source instance to target instance. 

I just wanted you to help us with how to update the incident in target instance and that gets updated in 

source instance. 

Version history
Last update:
‎07-30-2021 05:59 AM
Updated by: