Business rule to generate outbound REST, and update current record with a value taken from the REST response.

Jamsta1912
Tera Guru

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);

 

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

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.

View solution in original post

8 REPLIES 8

Mike Patel
Tera Sage

I did something similar when I was building integration with Jira. I used async instead of after. I know it's not best practice to use current.update but there was not other option. It is working fine for us and that should solve your delay issue.

SanjivMeher
Kilo Patron
Kilo Patron

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.

Thank you Sanjiv, I can see how that would be a tidy way to do it.

Please mark my response correct if it worked for you


Please mark this response as correct or helpful if it assisted you with your question.