- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2018 06:33 PM
Hello community.
I have a question about a very common use case in an integration with a third party system, for which I'm still unsure of the best practice approach. Here is the process:
1) Incident created in our ServiceNow instance.
2) Business rule runs to send outbound REST message to third party system.
3) REST response received, which contains the third party ref (incident number in the third party's system).
4) Response parsed, and third party ref added to correlation ID of the current record (ie the newly created incident).
Until now, I've tended do this all in a BEFORE business rule, but I realise that's probably not best practice as it means we're generating the outbound REST message before our incident has been created. The incident might for some reason fail to be created, but we would already have created the corresponding incident on the partner's system.
Is it better to do this in an AFTER business rule, or an ASYNC rule? In both cases, I'm having to use current.update() which I know is also not a good practice. But I'm using current.setWorkflow(false) beforehand, to stop recurring business rules.
I've set up a business rule that fires a REST message at a mock server I've set up in Postman, to test this out. I'm finding that the AFTER rule works best, but there might be performance issues experienced by the user if the response is delayed for some reason.
Any advice or thoughts on this?
For completeness, here is the code from my business rule:
(function executeRule(current, previous /*null when async*/) {
try {
var r = new sn_ws.RESTMessageV2('PostManMock_INC_1', 'CreateNewINC');
var incNum = current.getValue('number');
r.setStringParameterNoEscape('number', incNum);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var parsed = JSON.parse(responseBody);
var postMockINC = parsed.client_ref;
gs.info('XXXX Postman Mock Incident: ' + current.number + ': ' + postMockINC);
current.correlation_id = postMockINC;
current.correlation_display = 'Postman Mock';
current.setWorkflow(false);
current.update();
}
catch(ex) {
var message = ex.getMessage();
}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Integrations

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2018 09:01 PM
I usually prefer creating an event and calling that event from a Business rule. And then associate the event to a script action. And add all my code to script action. So that they are kept separate ans async.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 11:54 AM
Hi Sanjiv,
I'd like to leave the question unanswered for now, as there may be more suggestions to come. This might be one of those where there isn't a definitive right answer! I appreciate your input 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2021 11:54 AM
Hi Sanjiv,
I just had a doubt. Here you are using which business rule(before/after). They will execute async but
For Ex: we consider before business rule, if record creation fails still event is executing right and it will get response from rest call
Another option: we consider after business rule, execute async is it (i.e. business rule is executing and simultaneously event is executing)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2019 02:31 PM
Hi Jamsta,
I was wondering if you could help me in achieving this.
My requirement are exaclty the same as yours . And the code too looks very similar .
But no luck , In response i am getting a blank response i.e.No value in the correlation id field .
The requirement is I am integrating two servicenow instances (Instance 1 and Instance 2) using REST API and have used a POST method for this .
I am pushing the values using POST method and Its business rule and it is successfully creating an incident in instacne 2 with correlation id as the incident number of instance 1 .
I need the Incident number of instance 2 back in the Correlation id field of instance 1 .
But i get a blank response .
Few clarifications:
Have you used only POST Method and running a business rule for post itself ?
Here is the link where i posted my concern regarding this :
And here is my attached code :
try {
var r = new sn_ws.RESTMessageV2('Test incident','Create Incident');
r.setStringParameterNoEscape('Description',current.description);
r.setStringParameterNoEscape('impact',current.impact);
r.setStringParameterNoEscape('short_description',current.short_description);
var incNum = current.getValue('number');
r.setStringParameterNoEscape('correlation_id', incNum);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseObj = JSON.parse(responseBody);
var pINC = responseObj.number;
current.correlation_id = pINC;
current.correlation_display = 'Postman Mock';
current.setWorkflow(false);
current.update();
}
catch(ex) {
var message = ex.getMessage();
Your response will be appreciated 🙂
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2019 03:41 AM
Hello shimo. Sorry for my delay replying. I will post on your new thread.