Using a Variable as a condition on a business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 05:53 AM
I have been trying to create a business rule using a variable from a catalog form as one of the conditions. We have a field "employee number" on one of our catalog forms that I need to have mandatory on a task. But . . . we only want the field to be mandatory for this specific form and only if the field is empty and when they are closing the task.
Have been able to get it to recognize that the business rule is for a specific form and only if the condition of state changes to closed complete - testing this on other forms where the field is also located works fine. I can update a task, close the task with no problem, but it does not recognize the condition of "variable" that I found in the related list where you can select what form the "employee number" is on and then set the field to "is empty" (see image below). Has anyone had any luck using variables from catalog forms on business rules or am I going to have to write an advanced script for this? Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 06:28 AM
Hi
I don't think that will work when the business rule are running on the sc_task table. The variables are related to the sc_req_item record, so you would need to do an advanced script to check the value of this. But you can keep the rest of your normal condition:
var item = new GlideRecord('sc_req_item');
item.get(current.request_item);
if (item.variables.employee_number == '') {
current.setAbortAction(true);
gs.addErrorMessage('Employee number must be filled out');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 06:58 AM
Lars — thank you for responding
I did try your script, but it still does not recognize if the employee number field is filled out or empty
When I check the box for advanced script I did get the following standard script already included and I applied yours in the section that noted "your script here"
This is what the script looks like now
(function executeRule(current, previous /null when async/) {
var item = new GlideRecord('sc_req_item');
item.get(current.request_item);
if (item.variables.employee_number == '') {
current.setAbortAction(true);
gs.gs.addErrorMessage('Employee number must be filled out');
}
})(current, previous);
Thanks in advance
Nona Johnson

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 07:25 AM
Dear Nona Johnson,
What ever Lars Tange has given that is current. I have tried the same but different manner as shown in the below.
Business rule :
var ojec = new checkVariablevalues2();
var SIresult = ojec.checkVariable(current.request_item.sys_id);
if(SIresult == true){
current.update();
}
else
current.setAbortAction(true);
Script Include :checkVariablevalues2
var checkVariablevalues2 = Class.create();
checkVariablevalues2.prototype = {
checkVariable:function(ItemID){
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", ItemID);
item.query();
if(item.next()){
gs.log(item.variable_pool.employee_number == '');
if(item.variable_pool.employee_number == '' ){
return true;
}
}
return false;
},
type: 'checkVariablevalues2'
};
I am able to restrict the updating the record.
If your are able to save the form means, there is some value stored in employee fields. try to get some logs to to get the value and check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-05-2017 08:25 AM
Hi Nona,
I tried in background script and it is working fine. Can you please check it again?
U can also try with JSUtil
var x = "the quick brown fox";
var y = "";
var z;
gs.print("x = '" + x + "', JSUtil.notNil(x) = " + JSUtil.notNil(x));
gs.print("y = '" + y + "', JSUtil.notNil(y) = " + JSUtil.notNil(y));
gs.print("z = '" + z + "', JSUtil.notNil(z) = " + JSUtil.notNil(z));
*** Script: x = 'the quick brown fox', JSUtil.notNil(x) = true
*** Script: y = '', JSUtil.notNil(y) = false
*** Script: z = 'undefined', JSUtil.notNil(z) = false