Blog

Request Generation Methods

ServiceNow can build relationships between records and create new records very easily using Related Lists. That works in most situations, however sometimes you may want to generate records using Inbound Actions or  Business Rules.

Generating Incidents is pretty easy, since it is one record.  Requests can be more difficult, because Requests can contain more than three records, Request, Requested Item, and Catalog Tasks. 

Here are some example ways to generate requests:

Example 1: Create Request from UI Action

UI Action: Create Request
Table: <your_table>
Form Context Menu: true
Condition: <your_condition_here>
Script:
//Update saves record before going to the catalog homepage
current.update();
gs.addInfoMessage('You will need to navigate back to record ' + current.number + ' upon request completion. www.servicenowelite.com');

var url = "catalog_home.do?sysparm_view=catalog_default&sysparm_processing_hint=setfield:request.parent="
url += current.sys_id;

action.setRedirectURL(url);

Example 2: Create Requested Items from Inbound Action using Cart API

Inbound Action: Create Requested Item
Table: sc_req_item
Condition: <your condition here>
Script:
createRequest();
function createRequest() {
var cart = new Cart();
// add in cart, substitute your cat item sys_id
var item = cart.addItem('00000000000000000000000000000000');
// set requested for, substitute your requested for
//Set Variables in your Cart Item
cart.setVariable(item, 'requested_for', '00000000000000000000000000000000');
cart.setVariable(item, 'request_short_description', email.subject.toString());
cart.setVariable(item, 'request_description', email.body_html);
cart.setVariable(item, 'request_type', 'others');
var cartmsg = "received from: " + email.origemail + "\n\n" + email.body_text;
cart.setVariable(item,'comments',cartmsg);
var rc = cart.placeOrder();
}

Example 3: Create Requested Items from Inbound Action without Cart

Inbound Action: Create Requested Item
Table: sc_req_item
Condition: <your condition here>
Script:
createRequest();
function createRequest() {
//Create Request
var grRequest = new GlideRecord ("sc_request");
grRequest.initialize();
//substitute your requested for
grRequest.requested_for = '00000000000000000000000000000000';
grRequest.short_description = email.subject.toString();
grRequest.description = "received from: " + email.origemail + "\n\n" + email.body_text;
var requestSysId = grRequest.insert();
//Create Request Item, substitute your requested for
current.requested_for = '00000000000000000000000000000000';
current.short_description = email.subject.toString();
current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
//substitute your cat item
current.cat_item = '00000000000000000000000000000000';
current.parent = requestSysId;
current.request = requestSysId;
current.insert();
//Workflow Trigger
//More information http://wiki.servicenow.com/index.php?title=Workflow_Script
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Put your workflow name here");
w.startFlow(wfSysId, current, current.operation());
}

Example 4: Create Requested Item from Business Rule

Business Rule
When: after
Insert/Update/Delete: true
Condition: <your_condition_here>
createRequest();

function createRequest() {
//Create Request
var cart = new Cart();
//substitute your cat item
var item = cart.addItem('00000000000000000000000000000000');

//Set Variables in your Cart Item
//substitute your req for
cart.setVariable(item, 'requested_for','00000000000000000000000000000000');
cart.setVariable(item, 'request_short_description', 'Created from Business Rule');
cart.setVariable(item, 'request_description', 'Created from Business Rule');
cart.setVariable(item, 'request_type', 'others');

var rc = cart.placeOrder();
cart.description = 'servicenowelite.com';
var rc = cart.placeOrder();
gs.addInfoMessage('Request Item Created: ' + rc.number);
}

Example 5: Create Requested Item from Transform Script

When: onBefore
Script:
var cart = new Cart();
// add in cart, substitute your cat item sys_id
var item = cart.addItem('00000000000000000000000000000000');

//Set Variables in your Cart Item
var cartmsg = "Import Set: " + source.sys_import_set.table_name +', Datasource:'+ source.sys_import_set.data_source;
cart.setVariable(item,'comments',cartmsg);

var rc = cart.placeOrder();