Testing a Business Rule in ServiceNow can be frustrating due to the need to constantly update a record from a form. To see if the rule is working, you have to execute a UI Action, such as Update, Save, or Submit, and then wait for the form to reload. After that, you need to examine the log or data to determine if the Business Rule was properly executed. For years, I have followed a rule and a process for every business rule I write.
- My business rules are always one line, which calls a Script Include. Even if the Script Include is a multi-use utility Script Include, it makes this infinitely more testable.
- Use Background - Scripts, ATF, or Background Scripts in VS Code to test the Script Include.
- Use the same methods as #2 to test the Business Rule ( doing the operation on the record through a script instead of the form).
Example:
Business Rule
(function executeRule(current, previous /*null when async*/) {
(new ITSMBRUtil()).updateRecord(current);
})(current, previous);
Script Include
var ITSMBRUtil = Class.create();
ITSMBRUtil.prototype = {
initialize: function() {
},
updateRecord: function(current){
//Do some updates to current
},
type: 'ITSMBRUtil'
};
Test
var ITSMBRUtil_Test = Class.create();
ITSMBRUtil_Test.prototype = {
initialize: function() {
},
updateRecord_Test: function(sys_id){
var grITSMRecord = new GlideRecord("<TABLE_NAME>");
grITSMRecord.get(sys_id);
(new ITSMBRUtil()).updateRecord(grITSMRecord);
},
type: 'ITSMBRUtil_Test'
};