Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule to Close INC and Create RITM

Tyler Jones
Tera Contributor

Hello all,

 

I'm very new to scripting, but I'm trying to put together the following:

 

Expectation:

If a user is in a group called MXUsers and they submit an INC, it should be closed and a REQ should be created and trigger the workflow for a specific catalog item.

 

Current Status:

The INC gets closed with all the fields I've defined in the script. The REQ gets created with the correct RITM.

 

Roadblock:

The workflow does not trigger. The RITM should be approved by one of our internal IT groups and create a Task.

 

I have validated the workflow and tested straight from the Service Catalog which works as expected. Below is the script I'm using:

 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
//If the user is in the MXUsers group, this script should run
var incidentCallerID = current.caller_id;
var specificGroup = 'MXUsers';

var groupMember = false;
var userGR = new GlideUser();
userGR.get(incidentCallerID);
var userGroups = userGR.getMyGroups();
while (userGroups.next()) {
if (userGroups.name == specificGroup) {
groupMember = true;
break;
}
}

//Create REQ
var requestGR = new GlideRecord('sc_request');
requestGR.initialize();
requestGR.requested_for = current.caller_id;
requestGR.short_description = current.short_description;

var requestSysID = requestGR.insert();

//Create RITM
var requestItemGR = new GlideRecord('sc_req_item');
requestItemGR.initialize();
requestItemGR.request = requestSysID;
requestItemGR.cat_item = '0ef92e0d1b3f91105bc24002dd4bcb7b';
requestItemGR.short_description = current.short_description;
requestItemGR.insertWithReferences();

// var ritmSysID = requestItemGR.insert(); This was line 33 at first //

//Set fields on INC
current.state = 7;
current.assigned_to = '60fc7e111b1745505bc24002dd4bcbd5'; //Assign to Glenn
current.assignment_group = '46f9f7c41bb1e090e0f5eac9bc4bcbeb'; //Assign to Helpdesk
current.close_code = 'Not Solved (Raised Request)';
current.u_category = 'Inquiry / Help';
current.u_subcategory = 'Ticket Routing';

//Get new RITM number to show in Resolution Notes
var ritmNumber = '';
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('request', requestSysID);
ritmGR.query();
if (ritmGR.next()) {
ritmNumber = ritmGR.number;
}

var closeNotes = 'Raised Request - ' + ritmNumber;
current.close_notes = closeNotes;
current.update();

}

)(current, previous);

1 ACCEPTED SOLUTION

Replace the part of your code //Create REQ and //Create RITM by Cart code and if you want to change the short description of your RITM you need to query .
it can be something like this : 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
//If the user is in the MXUsers group, this script should run
var incidentCallerID = current.caller_id;
var specificGroup = 'MXUsers';

var groupMember = false;
var userGR = new GlideUser();
userGR.get(incidentCallerID);
var userGroups = userGR.getMyGroups();
while (userGroups.next()) {
if (userGroups.name == specificGroup) {
groupMember = true;
break;
}
}

 

//Create your Request

var cart = new Cart(null, gs.getUserID());
var item = cart.addItem("0ef92e0d1b3f91105bc24002dd4bcb7b");
cart.setVariable(item, 'var1', 'value1');
cart.setVariable(item, 'var2','value2');
var rc = cart.placeOrder();
 
//Query for RITM Created
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request',rc.sys_id);
ritm.query();
if(ritm.next()){
ritm.short_description = current.short_description;
ritm.update();

}

//Set fields on INC
current.state = 7;
current.assigned_to = '60fc7e111b1745505bc24002dd4bcbd5'; //Assign to Glenn
current.assignment_group = '46f9f7c41bb1e090e0f5eac9bc4bcbeb'; //Assign to Helpdesk
current.close_code = 'Not Solved (Raised Request)';
current.u_category = 'Inquiry / Help';
current.u_subcategory = 'Ticket Routing';

var closeNotes = 'Raised Request - ' + ritm.number;
current.close_notes = closeNotes;
current.update();
}

)(current, previous);

 

View solution in original post

5 REPLIES 5

Othman Arkab
Tera Expert

Hi Tyler,

You can use Cart API to create your request, that will help you to trigger the workflow
you can do something like this :

var cart = new Cart(null, gs.getUserID());
var item = cart.addItem("cat_item_SysID");
cart.setVariable(item, 'var1', 'value1');
cart.setVariable(item, 'var2','value2');
var rc = cart.placeOrder();

Where would I put that? 

Replace the part of your code //Create REQ and //Create RITM by Cart code and if you want to change the short description of your RITM you need to query .
it can be something like this : 

(function executeRule(current, previous /*null when async*/) {

// Add your code here
//If the user is in the MXUsers group, this script should run
var incidentCallerID = current.caller_id;
var specificGroup = 'MXUsers';

var groupMember = false;
var userGR = new GlideUser();
userGR.get(incidentCallerID);
var userGroups = userGR.getMyGroups();
while (userGroups.next()) {
if (userGroups.name == specificGroup) {
groupMember = true;
break;
}
}

 

//Create your Request

var cart = new Cart(null, gs.getUserID());
var item = cart.addItem("0ef92e0d1b3f91105bc24002dd4bcb7b");
cart.setVariable(item, 'var1', 'value1');
cart.setVariable(item, 'var2','value2');
var rc = cart.placeOrder();
 
//Query for RITM Created
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request',rc.sys_id);
ritm.query();
if(ritm.next()){
ritm.short_description = current.short_description;
ritm.update();

}

//Set fields on INC
current.state = 7;
current.assigned_to = '60fc7e111b1745505bc24002dd4bcbd5'; //Assign to Glenn
current.assignment_group = '46f9f7c41bb1e090e0f5eac9bc4bcbeb'; //Assign to Helpdesk
current.close_code = 'Not Solved (Raised Request)';
current.u_category = 'Inquiry / Help';
current.u_subcategory = 'Ticket Routing';

var closeNotes = 'Raised Request - ' + ritm.number;
current.close_notes = closeNotes;
current.update();
}

)(current, previous);

 

That triggered the workflow! Thank you.