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

(function onAfter(current, previous /*null when async*/) {
	var obj = new utsw_AJAXAssignmentRouting();
	
	obj.incidentRouting(current.u_unit,current.u_contract,current.u_work_area,current.u_issue,current.cmdb_ci,current.location);
		
})(current, previous);

Still no luck.

 

(function onAfter(current, previous /*null when async*/) {
	var obj = new utsw_AJAXAssignmentRouting();
	
	obj.incidentRouting(current.u_unit,current.u_contract,current.u_work_area,current.u_issue,current.cmdb_ci,current.location);
		
})(current, previous);

Smorri, Now that you have script that is supposed to work, but its not. And i dont have access to your instances to see whats actually happening. Its time for you to start debugging the script. Can you please put log statements in the script include function, and let us know what the results are? You can use gs.log statements and check the results in script log statements module.

Tim Woodruff
Mega Guru

Please use code formatting in your posts if you're going to post code. It makes it much, much easier to read them. 

find_real_file.png

smorri
Giga Expert

Hi Aman,

 

Still no luck.  Below is my new Business Rule.  Any other ideas?

 

(function onAfter(current, previous /*null when async*/) {
	var obj = new utsw_AJAXAssignmentRouting();
	
	obj.incidentRouting(current.u_unit,current.u_contract,current.u_work_area,current.u_issue,current.cmdb_ci,current.location);
		
})(current, previous);