Business Rule to Scripted Rest API

Arun Kumar P1
Kilo Contributor

Existing scenario: Client is calling Server with server.get method. Servicenow responds the call with a JSON which contains a lot of information including the $$uiNotifications. Client then parses the $$uiNotifications array and displays all the error messages.

I need to change this communication channel to REST API. So now, client will call the API and API will respond with whatever data is required. How can I listen to the $$uiNotifications from the API and send it to the client?

 

In the existing scenario, I believe, whatever errors Business Rules throws, it will be present in the $$uiNotifications array. Likewise, I need the same stuff from API. 

6 REPLIES 6

Weston Wilson
Tera Expert

If I'm understanding correctly, it sounds like you may need a new table to capture the business rule errors. When that table is set up, you can have the business rule insert a new record on the table and allow the client access to the table via REST API. Are you using ServiceNow as the endpoint for the REST call? You could also do a scheduled job to push the notifications to an endpoint using sn_ws.RESTMessageV2().

Thank you Weston Wilson for your reply. 

So, what you are suggesting is to add a new table just for storing the business rules errors, right? I don't know whether this would be an effective way around. 

What I need is to listen to errors thrown by Business Rules from Scripted REST API. Or some method where I can store the error messages in session or global variable kind of stuff which I can use it in Scripted REST API. 

There is a table that stores REST messages requests and responses in ServiceNow (sys_outbound_http_log). If you are only looking for REST message errors processed by ServiceNow, you can query this table or create a new business rule on the table to get any non-200 responses.

If you're just looking for errors logged by business rules, you could do a query on the syslog table and search for a specific source that you set in the business rule. This assumes the error is logged by you and is not a runtime error. 

gs.log('This is a log', 'source');

If this data all lives within ServiceNow on an existing table like syslog or sys_outbound_http_log, you could just query that table for the error and send it through the scripted REST API. It all depends on permission levels and how you would like to store the data. Using an additional table could be helpful if additional users need to see the data and you need more than just the error stored. It sounds like just using the existing tables would work in this case. 

Arun Kumar P1
Kilo Contributor

The solution I came up with is :

Passing error messages from BR via gs.addErrorMessage() and catching that up in API with gs.getErrorMessages()

 

Thank you!