Outbound REST calls from business rules

Tom23
Tera Expert

Hi community people, I’ll start this by saying my REST API knowledge starts and ends with coping pasting code from google.

 

I’m trying to have the below run in a business rule but can I make it work (we're trying to use this to test that messages are passing through successfully, long term we want to be able to pass variables from a specific record into this)……

 

Anyone that can point me in the right direction?

 

 

try {

 

var r = new sn_ws.RESTMessageV2();

 

 

   r.setHttpMethod("put");

   r.setEndpoint("http://jenkins.*****.co.uk:8080");

   r.setRequestHeader("Content-Type", "application/json");

   r.setBasicAuth(******************);

               

   var response = r.execute();

   var outputstatuscode = response.getStatusCode();

   var outputbody = response.getBody();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

}) (current, previous);

1 ACCEPTED SOLUTION

Did you get the code from the Preview Script Usage of the Rest Message Method? It should look something like this, it just the ootb example for Infoblox

 try { 
 var r = new sn_ws.RESTMessageV2('Infoblox Rest Message', 'put');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.getMessage();
}

 

So you so specify the Rest Message and the method when you declare r and then you can set the variables you have defined in the rest message

View solution in original post

4 REPLIES 4

simonbergstedt
Tera Guru

I recommend starting here: https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/outbound-rest/concept/c_OutboundRESTWebService.html

 

But basicly what you do is create a REST Message that you can call on through a business rule, ui action or whatever you want to trigger it. In that REST message you specify the Endpoint (jenkins.... in your case), the authorization method/credentials, headers and variables that you the populate in the business rule, ui action, etc. If you want to see the result of it just print out httpStatus to the log or a gs.addInfoMessage. That way you can see that it triggers like it should and get an error/succes-code.

 

It's actually pretty straightforward, fast and most of all fun way to work with REST and SOAP.

Hi Simon,

 

Sorry, I should maybe have been clearer in the initial post. We have set up the REST message and this tests successfully, but we're getting the below errors when trying to actually call the script:

Caught error attempting to enqueue change [u_msa_fulfilment.15d10840db3b9bc078d02de74b961962]: java.lang.IllegalArgumentException: com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)
com.glide.record_watcher.async.SerializedElement.<init>(SerializedElement.java:25)
com.glide.record_watcher.RecordWatcherService.deserialize(RecordWatcherService.java:95)
com.glide.record_watcher.RecordWatcherService.enqueue(RecordWatcherService.java:55)
com.glide.script.GlideRecord.postChange(GlideRecord.java:4839)
com.glide.script.GlideRecord.postUpdate(GlideRecord.java:4803)
com.glide.script.GlideRecord.updateWithoutResettingOperation(GlideRecord.java:4387)
com.glide.script.GlideRecord.update(GlideRecord.java:4301)
com.glide.script.GlideRecord.jsFunction_update(GlideRecord.java:4117)
sun.reflect.GeneratedMethodAccessor231.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
org.mozilla.javascript.FunctionObject.doInvoke(FunctionObject.java:670)
org.mozilla.javascript.FunctionObject.call(FunctionObject.java:614)
org.mozilla.javascript.ScriptRuntime.doCall(ScriptRuntime.java:2582)
org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:85)
org.mozilla.javascript.gen.sys_ui_action_47fd7f4dc0a8000600a552278b5232ab_script_2154._c_script_0(sys_ui_action.47fd7f4dc0a8000600a552278b5232ab.script:2)
org.mozilla.javascript.gen.sys_ui_action_47fd7f4dc0a8000600a552278b5232ab_script_2154.call(sys_ui_action.47fd7f4dc0a8000600a552278b5232ab.script)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:563)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3428)
org.mozilla.javascript.gen.sys_ui_action_47fd7f4dc0a8000600a552278b5232ab_script_2154.call(sys_ui_action.47fd7f4dc0a8000600a552278b5232ab.script)
org.mozilla.javascript.gen.sys_ui_action_47fd7f4dc0a8000600a552278b5232ab_script_2154.exec(sys_ui_action.47fd7f4dc0a8000600a552278b5232ab.script)
com.glide.script.ScriptEvaluator.execute(ScriptEvaluator.java:263)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:110)
com.glide.script.ScriptEvaluator.evaluateString(ScriptEvaluator.java:76)
com.glide.script.GlideRhinoHelper.evaluateAsString(GlideRhinoHelper.java:135)
com.glide.script.ActionScript.conditionalEval(ActionScript.java:97)
com.glide.script.ActionScript.execute(ActionScript.java:82)
com.glide.script.ActionScriptProcessor.processScript(ActionScriptProcessor.java:144)
com.glide.script.Action.process(Action.java:116)
com.glide.ui.RedirectTransaction.invokeAction(RedirectTransaction.java:524)
com.glide.ui.RedirectTransaction.handleActions(RedirectTransaction.java:480)
com.glide.ui.RedirectTransaction.inboundActions(RedirectTransaction.java:316)
com.glide.ui.RedirectTransaction.process(RedirectTransaction.java:182)
com.glide.ui.GlideServletUITransaction.process(GlideServletUITransaction.java:93)
com.glide.processors.AProcessor.runProcessor(AProcessor.java:483)
com.glide.processors.AProcessor.processTransaction(AProcessor.java:205)
com.glide.processors.ProcessorRegistry.process0(ProcessorRegistry.java:178)
com.glide.processors.ProcessorRegistry.process(ProcessorRegistry.java:167)
com.glide.ui.GlideServletTransaction.process(GlideServletTransaction.java:31)
com.glide.sys.Transaction.run(Transaction.java:2038)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)

 

Any thoughts?

Did you get the code from the Preview Script Usage of the Rest Message Method? It should look something like this, it just the ootb example for Infoblox

 try { 
 var r = new sn_ws.RESTMessageV2('Infoblox Rest Message', 'put');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.getMessage();
}

 

So you so specify the Rest Message and the method when you declare r and then you can set the variables you have defined in the rest message

Thanks Simon!

 

Seems like we're up and running now.