- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-23-2024 06:53 PM
Hi,
I am integrating two instances by REST API and in the first instance, I am using POST method to automate creating incident from the first instance to the other.
I am using these variables as in json
{
"short_description": "${details}",
"caller_id": "${caller}",
"category": "${cat}",
"description": "${desc}"
}
and I am using this script in business rules
(function executeRule(current, previous /*null when async*/ ) {
var r = new sn_ws.RESTMessageV2('ServiceNow_E-Bonding', 'Create an Incident');
r.setStringParameterNoEscape('details', current.short_description);
r.setStringParameterNoEscape('desc', current.description);
r.setStringParameterNoEscape('caller', current.caller_id);
r.setStringParameterNoEscape('cat', current.category);
var response = r.execute();
var responseBody = response.getBody();
gs.info("The body is " +responseBody);
var httpStatus = response.getStatusCode();
gs.info("The code is"+httpStatus);
})(current, previous);
Although It is creating an automated ticket in the other instance when I create in the first instance but it does not capture the variables from the first instance also logs are not showing for responseBody and httpStatus in the log table(also used gs.log). The automated incident is empty and have no data what I provided in the other instance.
Please help.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2024 06:53 AM
Hi @MohdF
I see you're using ServiceNow's Business Rules to integrate two instances via REST API.
Let's break down the issues you're facing:
1. Variables not being captured:
In your script, you're using current.short_description
, current.description
, current.caller_id
, and current.category
to set the variables for the REST API call. However, these variables are not being populated with the actual values from the first instance.
To fix this, you need to ensure that the current
object is properly populated with the values from the first instance. You can do this by using the current
object's properties, such as current.short_description
, current.description
, etc. directly in your JSON payload.
{
"short_description": current.short_description,
"caller_id": current.caller_id,
"category": current.category,
"description": current.description
}
Logs not showing for responseBody and httpStatus:
To troubleshoot this, you can try using gs.debug()
instead of gs.info()
to log the response body and HTTP status code. This will help you identify if there are any issues with the API call.
For example:
var response = r.execute();
var responseBody = response.getBody();
gs.debug("Response Body: " + responseBody);
var httpStatus = response.getStatusCode();
gs.debug("HTTP Status Code: " + httpStatus);
3. Automated incident is empty:
This could be due to the fact that the variables are not being captured correctly, as mentioned in point 1. Make sure that the JSON payload is correctly formatted and the variables are being populated with the actual values from the first instance.
Additionally, you can try logging the JSON payload before sending the REST API call to ensure that it's correctly formatted:
var payload = {
"short_description": current.short_description,
"caller_id": current.caller_id,
"category": current.category,
"description": current.description
};
gs.debug("JSON Payload: " + JSON.stringify(payload));
r.setRequestBody(JSON.stringify(payload));
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-24-2024 05:22 AM
Hi MohdF,
Please refer to this article : https://www.servicenow.com/community/developer-articles/servicenow-to-servicenow-integration-step-by...
Thanks!
P.S. Please mark as Helpful if I was able to clear your doubt.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2024 06:53 AM
Hi @MohdF
I see you're using ServiceNow's Business Rules to integrate two instances via REST API.
Let's break down the issues you're facing:
1. Variables not being captured:
In your script, you're using current.short_description
, current.description
, current.caller_id
, and current.category
to set the variables for the REST API call. However, these variables are not being populated with the actual values from the first instance.
To fix this, you need to ensure that the current
object is properly populated with the values from the first instance. You can do this by using the current
object's properties, such as current.short_description
, current.description
, etc. directly in your JSON payload.
{
"short_description": current.short_description,
"caller_id": current.caller_id,
"category": current.category,
"description": current.description
}
Logs not showing for responseBody and httpStatus:
To troubleshoot this, you can try using gs.debug()
instead of gs.info()
to log the response body and HTTP status code. This will help you identify if there are any issues with the API call.
For example:
var response = r.execute();
var responseBody = response.getBody();
gs.debug("Response Body: " + responseBody);
var httpStatus = response.getStatusCode();
gs.debug("HTTP Status Code: " + httpStatus);
3. Automated incident is empty:
This could be due to the fact that the variables are not being captured correctly, as mentioned in point 1. Make sure that the JSON payload is correctly formatted and the variables are being populated with the actual values from the first instance.
Additionally, you can try logging the JSON payload before sending the REST API call to ensure that it's correctly formatted:
var payload = {
"short_description": current.short_description,
"caller_id": current.caller_id,
"category": current.category,
"description": current.description
};
gs.debug("JSON Payload: " + JSON.stringify(payload));
r.setRequestBody(JSON.stringify(payload));
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-24-2024 06:16 PM
Hi @Ravi Gaurav, first thank you for the response, been learning a lot a lot from your videos and second, I have successfully created the above requirement which I have been trying on E-Bonding and Integration, I was doing one thing wrong and now I am all good.
Also, I need to know one more things, how can I make incident records be updated on the target instance when I update it on the source instance?
Your response will be appreciated.
Thanks,
Faraz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2024 12:09 AM
Hi @MohdF
I'm glad to hear that you've been learning a lot from my videos and that you've successfully implemented your E-Bonding and Integration requirement! It's great to see your progress.
Regarding your new question on updating incident records on the target instance when they are updated on the source instance, you can achieve this through the below:-
1. Create a rest message and under rest Method use (patch/put)
2. Send ${sys_id} in content type and write a Business rule like the below
suppose you have to update short_description..
Business Rule :-(you can use after or async)
In Advance section write the below
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/