Rest API create incident from one instance to other

Rajkumar Bommal
Tera Contributor

Create rest api in ServiceNow to create a new incident in another instance when priority of incident changes to “1” in the first instance. Also capture the response and display the sys id of the incident created on second instance
Created Business rule in 1st instance:

(function executeRule(current, previous /*null when async*/) {
    var requestBody = {
        short_description: current.short_description.getDisplayValue(),
        priority: current.priority.getDisplayValue()
    };
   
    var request = new sn_ws.RESTMessageV2();
    request.setHttpMethod('POST');
    request.setEndpoint(url);
    request.setRequestBody(JSON.stringify(requestBody));
   
    var response = request.execute();
    var responseBody = response.getBody();
    var responseObj = JSON.parse(responseBody);
   
    gs.info('Created incident sys_id on second instance: ' + responseObj.sys_id);
})(current, previous);
 
in second instance created a scripted REST:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var requestBody = request.body.data;
    var incident = new GlideRecord('incident');
   
    incident.initialize();
    incident.short_description = requestBody.short_description; //|| 'requested from another instance';
    incident.priority = requestBody.priority; //|| 1;
    incident.caller_id=requestBody.caller_id;
    var incidentSysId = incident.insert();

    response.setStatus(201);
    response.setHeader('Content-Type', 'application/json');
    response.setBody({
        sys_id: incidentSysId
    });
})(request, response);
 
can anyone find whats the issue..
 
4 REPLIES 4

Ravi Gaurav
Giga Sage
Giga Sage

Hi @Rajkumar Bommal 

Few Modifications :-

  1. In the Business Rule, you're using current.short_description.getDisplayValue() and current.priority.getDisplayValue(). These methods return the display value of the field, which might not be what you want to send in the request body. Instead, you might want to use current.short_description.getValue() and current.priority.getValue() to get the actual value of the field.
  2. In the Scripted REST API, you're using request.body.data to access the request body. However, the data property is not a standard property of the request.body object. Instead, you should use request.body directly to access the request body as a JSON object.
  3. In the Scripted REST API, you're setting incident.caller_id to requestBody.caller_id, but you're not sending the caller_id field in the request body from the Business Rule. You should either remove this line or add the caller_id field to the request body in the Business Rule.
  4. In the Scripted REST API, you're setting the response status to 201, but you're not checking if the incident was actually created successfully. You should add some error handling to make sure that the incident was created before setting the response status to 201.

Here's an updated version of the code that addresses these issues:

Business Rule:

(function executeRule(current, previous /*null when async*/) {
var url = 'https://dev225695.service-now.com/api/1073608/inc_create/create_inc';
var requestBody = {
short_description: current.short_description.getValue(),
priority: current.priority.getValue()
};

var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('POST');
request.setEndpoint(url);
request.setRequestBody(JSON.stringify(requestBody));

var response = request.execute();
var responseBody = response.getBody();
var responseObj = JSON.parse(responseBody);

gs.info('Created incident sys_id on second instance: ' + responseObj.sys_id);
})(current, previous);

Scripted Rest API:-

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body;
var incident = new GlideRecord('incident');

incident.initialize();
incident.short_description = requestBody.short_description;
incident.priority = requestBody.priority;

try {
var incidentSysId = incident.insert();
response.setStatus(201);
response.setHeader('Content-Type', 'application/json');
response.setBody({
sys_id: incidentSysId
});
} catch (e) {
response.setStatus(500);
response.setHeader('Content-Type', 'application/json');
response.setBody({
error: e.getMessage()
});
}
})(request, response);

I'm looking forward to hearing back from you. Please go ahead and test the updated code, and let me know if it works as expected. 

 

You can follow this video as well :-

https://www.youtube.com/watch?v=pcuP6HP8QpA&pp=ygUWc2VydmljZW5vdyBpbnRlZ3JhdGlvbg%3D%3D

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
Hi Youtube Family, I am Ravi Gaurav. I am Expert in ServiceNow . Welcome to my youtube channel. If you guys enjoyed it, make sure to Like👍 , Comment💬 and Subscribe❤️ _________________________________________ Copyright Disclaimer : All Rights to VideoLabel Co. & No Copyright infringement intende...

@Ravi Gaurav 
above code is not working

 

BillMartin
Mega Sage

Hi @Rajkumar Bommal ,

 

I hope this article finds you well. I have created this ServiceNow Integrations: Five (5) Key Steps to Create an Incident from One Instance to Another 

 

If you find the article lucrative please mark as helpful and accept as solution.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Rajkumar Bommal 

 

https://youtu.be/AMaWR64zzfA

 

This video will be helpful. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************
CISITSM #ITSM #csa #ServiceNow #TechnoFuncational Disclaimer: These videos are from my training batch. These videos did not promote any ServiceNow Sales pitch or marketing. These videos are only for knowledge purposes & basic on my experience & Knowledge. Redistribution or copying of functionality