How can I have a Business Rule call a Script Include?

smorri
Giga Expert

I have a Business Rule that takes part of the short description (a location), and puts it in our location field.  This works from an email with subject   Button Press - Laptop Laptop - UH07311.  The Business Rule works.  Further below is a script include that handles all of our routing for different Desktop Technicians depending on location, contract, etc...  The script include is called by client scripts.  Is there away to call it on the server side as well?

(function executeRule(current, previous /*null when async*/) {
var str = current.getValue('short_description');
var loc = str.substr(30);
if(str.indexOf("Button Press - Laptop Laptop -") == 0)
{
current.location.setDisplayValue(loc.trim());
current.cmdb_ci.setDisplayValue('Desktop MW');
current.u_incident_type.setDisplayValue('Fault/Failure');
current.u_issue.setDisplayValue('Other');

}

})(current, previous);

Script Include below:

var utsw_AJAXAssignmentRouting = Class.create();
utsw_AJAXAssignmentRouting.prototype = Object.extendsObject(AbstractAjaxProcessor, {
incidentRouting : function(){
//couldn't get the object to pass in, so rebuilding object to keep flow.

var json = new JSON();

var routingInfo = {};
routingInfo.ci = this.getParameter('sysparm_ci');
routingInfo.location = this.getParameter('sysparm_location');
routingInfo.issue = this.getParameter('sysparm_issue');
routingInfo.unit = this.getParameter('sysparm_unit');
routingInfo.contract = this.getParameter('sysparm_contract');
routingInfo.workarea = this.getParameter('sysparm_workarea');

var incRouting; //setup var in global space to be used for GlideRecord to incident routing table

for(x in routingInfo){
gs.log("routinInfo --> " + x + " : " + routingInfo[x], 'utsw_AJAXAssignmentRouting');
//jslog("routinInfo --> " + x + " : " + routingInfo[x], 'utsw_AJAXAssignmentRouting');
}

// if CI has unit, find match and return value. This overrides all other assignment rules.
if(routingInfo.unit != ''){
var ciUnit = new GlideRecord('u_incident_routing');
ciUnit.get('u_unit', routingInfo.unit);
return json.encode([ciUnit.u_assignment_group.sys_id.toString(), ciUnit.u_assignment_group.name.toString()]);
}
// if contract is filled, send to different assignment group
if(routingInfo.contract != ''){
var ciUnit = new GlideRecord('u_incident_routing');
if (ciUnit.get('u_contract', routingInfo.contract)) {
return json.encode([ciUnit.u_assignment_group.sys_id.toString(), ciUnit.u_assignment_group.name.toString()]);
}
}
// if workarea is filled, send to different assignment group
if(routingInfo.workarea != ''){
var ciUnit = new GlideRecord('u_incident_routing');
if (ciUnit.get('u_work_area', routingInfo.workarea)) {
return json.encode([ciUnit.u_assignment_group.sys_id.toString(), ciUnit.u_assignment_group.name.toString()]);
}
}
// if we have an issue, use it for assignment
if(routingInfo.issue != ''){
incRouting = new GlideRecord('u_issue_routing');
incRouting.addQuery('u_issue', routingInfo.issue);
incRouting.addQuery('u_configuration_item', routingInfo.ci);
incRouting.query();
if(incRouting.next()){
return json.encode([incRouting.u_assignment_group.sys_id.toString(), incRouting.u_assignment_group.name.toString()]);

}
}

//grab Service Desk group so we can use it
var serviceDesk = new GlideRecord('sys_user_group');
serviceDesk.get('Service Desk');

// grab CI so we can work with it on subsequent checks.
var ci = new GlideRecord('cmdb_ci');
ci.get(routingInfo.ci);
//gs.log('Configuration Item: ' + ci.name + ", Special: " + ci.u_special, 'utsw_AJAXAssignmentRouting');

// get Location record so we can work with it.
var location = new GlideRecord('cmn_location');
location.get(routingInfo.location);

// see if CI is special and assign accordingly
if(ci.u_special == true){
gs.log("CI SPECIAL: " + ci.name + ", Special: " + ci.u_special + ", Support Group: " + ci.support_group.name, 'utsw_AJAXAssignmentRouting');
//jslog("CI SPECIAL: " + ci.name + ", Special: " + ci.u_special + ", Support Group: " + ci.support_group.name, 'utsw_AJAXAssignmentRouting');
//see if there is a location that we need to use.
incRouting = new GlideRecord('u_incident_routing');
incRouting.addNotNullQuery('u_location');
incRouting.query();

// if we find a location matching return assignment group
while(incRouting.next()){
//gs.log('In while loop looking for location' + incRouting.sys_id, 'utsw_AJAXAssignmentRouting');
if(location.name.indexOf(incRouting.u_location) > -1){
return json.encode([incRouting.u_assignment_group.sys_id.toString(), incRouting.u_assignment_group.name.toString()]);
}
}
// if there is no issue value or matching location, look to the support group of the CI and use it
if(ci.support_group != ''){
//gs.log('CI Special: Configration Item: ' + ci.name + ' Support Group: ' + ci.support_group.name + ' : ' + ci.support_group.sys_id, 'utsw_AJAXAssignmentRouting');
return json.encode([ci.support_group.sys_id.toString(), ci.support_group.name.toString()]);
// if there is no support group use the service desk
} else {
return json.encode([serviceDesk.sys_id.toString(), serviceDesk.name.toString()]);
}
//if CI not special, return support group if exist, and then service desk
} else {
if(ci.support_group != ''){
//gs.log('CI NOT Special: Configration Item: ' + ci.name + ' Support Group: ' + ci.support_group.name + ' : ' + ci.support_group.sys_id, 'utsw_AJAXAssignmentRouting');
return json.encode([ci.support_group.sys_id.toString(), ci.support_group.name.toString()]);
} else {
return json.encode([serviceDesk.sys_id.toString(), serviceDesk.name.toString()]);
}
}
}
});

1 ACCEPTED SOLUTION

Tim Woodruff
Mega Guru

Your Script Include is a client-callable SI. It doesn't work the same when called server-side, if I'm not mistaken. Try making a non-client-callable version, and have the client-callable one invoke it for GlideAjax. 

View solution in original post

10 REPLIES 10

Coleton
Kilo Guru
function onAfter(current, previous) {
       var scriptInclude = new nameOfScriptInclude();

       scriptInclude.scriptIncludeFunctionName();
}

ARG645
Tera Guru

Its absolutely possible to call a script include from a Business rule.

If you are making a call from the server side then parameter passing is different than GlideAjax. 

So change the starting part of your script include to the below lines of code

incidentRouting : function(ci,location,issue,unit,contract,workarea){
		//couldn't get the object to pass in, so rebuilding object to keep flow.
		
		var json = new JSON();
		
		var routingInfo = {};
// This way you use GlideAjax from Client side and Call this function from Server side too
			routingInfo.ci = ci || this.getParameter('sysparm_ci');
			routingInfo.location = location || this.getParameter('sysparm_location');
			routingInfo.issue = issue || this.getParameter('sysparm_issue');
			routingInfo.unit = unit || this.getParameter('sysparm_unit');
			routingInfo.contract = contract || this.getParameter('sysparm_contract');
			routingInfo.workarea = workarea || this.getParameter('sysparm_workarea');

 

The way you will call this function from a Business Rule would be 

var obj = new utsw_AJAXAssignmentRouting();

obj.incidentRouting(your parameters);

smorri
Giga Expert

Hi Aman,

I made the changes to the script include and added a second business rule as below, but the second business rule below is not firing off.  What am I missing?

When - after  insert/update

Business Rule

(function onAfter(current, previous /*null when async*/) {
var obj = new utsw_AJAXAssignmentRouting();
obj.incidentRouting('sysparm_location');

})(current, previous);

 

Okay. I should have been more specific about the function call. Below is the mistake you are doing 

Correct: var obj = new utsw_AJAXAssignmentRouting();
Wrong : obj.incidentRouting('sysparm_location');

The function in the script include expects 6 parameters to be passed in an order as described below

incidentRouting : function(ci,location,issue,unit,contract,workarea){

You should change your script to something below to pass the parameters (this is just an example and the below script will not work)

(function onAfter(current, previous /*null when async*/) {
 var obj = new utsw_AJAXAssignmentRouting();
//Pass your parameters in an order as your scriptinclude function expects them. 
//I made up the colun names, please replace them with the correct names.
//Also, if a field is a reference field, current.u_coulmn will give you the sys_id, use dotwalking if you need to get into specific values of the referenced field 
 obj.incidentRouting(current.cmdb_ci,current.location,current.u_issue,current.u_unit,current.u_contract,current.u_workarea);
 
})(current, previous);