Rest API create incident from one instance to other
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2024 11:42 PM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2024 12:52 AM - edited 08-26-2024 01:59 AM
Few Modifications :-
- In the Business Rule, you're using
current.short_description.getDisplayValue()
andcurrent.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 usecurrent.short_description.getValue()
andcurrent.priority.getValue()
to get the actual value of the field. - In the Scripted REST API, you're using
request.body.data
to access the request body. However, thedata
property is not a standard property of therequest.body
object. Instead, you should userequest.body
directly to access the request body as a JSON object. - In the Scripted REST API, you're setting
incident.caller_id
torequestBody.caller_id
, but you're not sending thecaller_id
field in the request body from the Business Rule. You should either remove this line or add thecaller_id
field to the request body in the Business Rule. - 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/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2024 12:06 AM
@Ravi Gaurav
above code is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2024 01:38 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2024 01:41 AM
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]
****************************************************************************************************************