Unable to make async rest request from a before business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2015 09:44 AM
I have a business rule that runs on insert and update on a app scoped table.The business rule calls a restful webservice and based on the response the operation should either be aborted or continue.
When I run this as a before rule I get an error stating Use an async business rule to perform outbound HTTP requests. I have tried switching the webservice to use executeasync and that has caused more errors with truncation of the endpoint. I have also tried switching the business rule to async but that doesn't allow for me to stop the current operation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2018 11:58 AM
@peg612 - Yes, executeAsync() can be used without a MID Server. If you do not specify a MID Server to be used then one will not be used. There are two places where a MID Server will be specified, either directly in the javascript via the RESTMessageV2.setMIDServer(string) method, or through the use_mid_server field in the sys_rest_message_fn table if you are defining your REST Message methods that way. However, whether you specify a MID Server or not, the system will always use the ECC Queue (ecc_queue table). If you do not specify the MID Server, then the ecc_queue record will trigger a "ASYNC: RESTClient" scheduled job that runs directly on the ServiceNow instance instead of being remotely executed on the MID Server. This will always incur additional time since there is a lag between the creation of the ecc_queue record and the execution of the scheduled job. For this reason it is almost always better to not use executeAsync() when you are not using a MID Server. The only time you would use executeAsync() without a MID Server is if you didn't mind incurring the additional 10 seconds of execution time and you either (1) don't need to process the results or (2) you are willing to write a custom ECCTopic probe as described in KB0563615 - "RESTMessageV2 API EccTopic Support" to handle the processing. In all other cases, it is better to simply use a direct REST call via the execute() method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2018 06:18 AM
@Mwatkins - Thanks a lot for the additional details. This is extremely helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2019 06:15 AM
Hi
I am doing outbound rest api call from business rule and it should be sync call and if call fails then i should abort the business rule (before business rule) and display error in UI.
But whenever i am trying to do same, i get below error
r.setStringParameterNoEscape('PONumber', current.u_ponumber);
r.setStringParameterNoEscape('SourceID', current.company.u_imsourceid);
r.setStringParameterNoEscape('LatestDate',current.latest_date);
r.setStringParameterNoEscape('LocationToPalletize',current.location_palletize);
r.setStringParameterNoEscape('LocationID', current.u_location.x_inmis_im_itad_im_locationid);
r.setStringParameterNoEscape('password',gs.getProperty('x_inmis_im_itad.Password'));
r.setStringParameterNoEscape('username', gs.getProperty('x_inmis_im_itad.Username'));
r.setStringParameterNoEscape('TimeOfDay', current.time_of_day==''?0:current.time_of_day);
r.setStringParameterNoEscape('FeedExecutionDate', getCurrentDateTime);
r.setStringParameterNoEscape('WorkOrderID', current.number);
r.setStringParameterNoEscape('WorkOrderType', current.u_wotype);
r.setStringParameterNoEscape('EarliestDate', current.earliest_date);
r.setStringParameterNoEscape('EquipmentLocation', current.equipment_location);
r.setStringParameterNoEscape('DeliverySentDate', current.delivery_sent_date);
r.setStringParameterNoEscape('EstimatedArrivalDate', current.estimated_arrival_date);
r.setStringParameterNoEscape('TrackingNumber', current.tracking_number);
r.setStringParameterNoEscape('EquipmentNotes', current.u_equipment_notes);
r.setStringParameterNoEscape('SourceOfRequest','ServiceNow');
r.setStringParameterNoEscape('vWOSourceKey',"");
r.setStringParameterNoEscape('SerializedAssetLevelInformation',JSON.stringify(SerializedAssetLevelInformation));
var response = r.execute();
var responseBody = response.getBody();
var readResponse = JSON.parse(responseBody);
var httpStatus = response.getStatusCode();
if(!response.haveError()){
//Success logic
gs.info('No Error:'+JSON.parse(responseBody));
current.im_jobid = readResponse.JobID;
}else{
//Failure logic
current.setAbortAction(true);
gs.addErrorMessage('Error occurred:'+response.getErrorMessage());
}