How to use an UI Action to call/include a business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2018 06:52 AM
Hi there,
I am new to ServiceNow an got stuck with with an implementation.
My Problem in described short words is, that I don't know how i can call or include a business rule to an UI Action, so that the BR is executed once the user presses the Button.
I use an outbound REST message to get an array of similar objects, which should be imported into a custom table.
I further created a business rule, including the script of the REST message, and added the functionality to insert each object into the table using a GlideRecord (see code below):
(function executeRule(current, previous /*null when async*/) {
// Add your code here
try {
var r = new sn_ws.RESTMessageV2('x_211456_mobee3.Selection_Options', 'GET Options');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var options = JSON.parse(responnseBody);
var caption='';
var value='';
for(i=0; i<options.length; i++) {
var emp = new GlideRecord('x_211456_mobee3_employee_list');
emp.initialize();
emp.caption = options[i].caption;
emp.value = options[i].value;
emp.icon = options[i].icon;
emp.insert();
}
}
catch(err) {
gs.info("Uncaught error: "+ err);
}
})(current, previous);
The code of the business rule should probably not be the problem, I tried the code snippet of the loop with test values in another BR and it works fine.
In order to call this BR with a button in the List view of the employee_list table I tried to use a UI Action.
But here is where I got stuck.
I don't know how I can call or include the business rule in the script of the action.
I found some solutions with gs.include() but then I don't know how the Business rule should be passed.
Can anyone give me a hint or solution?
Thank you and kind ragards.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2018 11:52 AM
Hello RSchmit,
The best option is to create a Script Include and then call it from the UI action. More info on the Script Includes in the below links.
https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html
https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_scripting_jakarta_server_side_scripting/app_store_learnv2_scripting_jakarta_script_includes?v=jakarta
Thanks,
Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2018 01:55 AM
Thank you for your reply.
I created a script include as you suggested and added a function which can be called from the ui action script.
// Script Include
var OptionsInclude = Class.create();
OptionsInclude.prototype = {
initialize: function() {
},
getOptions: function() {
// call business rule?
// or insert code of business rule here?
},
type: 'OptionsInclude'
};
The script of the ui action looks like this:
//ui action
var options = new OptionsInclude();
options.getOptions();
Is this the right way to enable the connection between ui action and script include?
The scripting exercise helped me but I am still not sure how to call the business rule inside the script include. The tutorial only describes the other way around -> how the business rule calls the script include. That is what I used in the action.
Can I simply copy paste the whole code from the business rule?
Or is there another way to call a specific business rule, e.g. using gs.include(<NameOfBusinessRule>)? -> If yes, where can I find the name I need to pass?
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2018 08:33 PM
Does the UI Action modify the original record? If so, you could use an after update
rule and trigger based on the value mutation. But, Pradeep's solution is much neater and probably more reliable.