Sunday, July 21, 2019

Working with Assignment Rules

Assignment Rules in ServiceNow:

Assignment rules are configurable feature in ServiceNow to drive the task assignment.  Based on attributes of the task, we can configure rules to assign that task to a group.   That is all good until I find myself in a situation where the assignment rule that I have configured not working and I could not figure for a while why  ?


So,   Below is the example set up that I have created dev environment to describe the issue.


Workflow:


Assignment Rule Configuration:







That set up fairly simple and I was expecting to work. i.e When change request type of Normal gets created the change task of it will be assigned to Change Management Group. But It did not. The created change task remained unassigned and I have got work in hand to debug why is that the case.


After bit of time, I have realised its the execution order. The workflow engine here is running ahead of the change request insertion into the database. So, when the assignment rule gets executed the condition in it will not get satisfied as the parent change request type is going to undefined by then.

Work Around:

I have created my custom script include to exactly mimic, what out of the box assignment rules is supposed to do. i.e I have glide recorded the assignment rule table and evaluated the matching condition and returned the assignment group. Since, this is my custom utility I had the control of injecting missing values on to the change request reference.


Here is the script  that did the trick.


var WorkflowUtils = Class.create();
WorkflowUtils.prototype = {
initialize: function() {
},

getAssignmentGroup: function (task) {
gs.log('running');
var rules = this.getRecord('sysrule_assignment', 'table=' + task.getTableName() + '^ORDERBYorder');
var filter = GlideFilter;
while (rules.next()) {
if (filter.checkRecord(task, rules.condition + '')) {
return rules.group + '';
}
}
},

getRecord: function (table, query, limit) {
var log = [];
var gr = new GlideRecord(table);
gr.addEncodedQuery(query);
if (!global.JSUtil.nil(limit))
gr.setLimit(limit);
gr.query();
log.push('table ' + gr.getTableName());
log.push('encodedQuery ' + gr.getEncodedQuery());
log.push('count ' + gr.getRowCount());
gs.info(log.join('\n'));
return gr;

},

mapParent: function (ge, parent) {
ge.type = parent.type;
return ge;
},


type: 'WorkflowUtils'
};


I have called the above script from the workflow CreateTask Activity.




No comments:

Post a Comment

ITIL V4 foundation exam pattern

What is the purpose of ITIL V4 foundation? The purpose of ITIL V4 foundation is to introduce readers to the management of modern IT-enable...