Parsing an Inbound Email action to set variables in a Service Request

Bill_Collins
Mega Guru

I need help setting up an an inbound email action which will parse the content to create a Service Catalog Request and set variables in the same Request. I am able to create the Request, but have not been able to set the variables. I would like to know how to set both variables and variable sets. Following is what I have tried.

gs.include('Cart');
var cart = new Cart();
var item = cart.addItem('478d5f3e0a0a3c0d378e4423fbd5fec8');
if (email.body.RequiredBy!= undefined)
{
cart.setVariable(item, 'Required By', email.body.RequiredBy);
}
if (email.body.Options!= undefined)
{
cart.setVariable(item, 'Options', email.body.Options);
}
if (email.body.TV2!= undefined)
{
cart.setVariable(item, 'TV2', email.body.TV2);
}
if (email.body.Accounts!= undefined)
{
cart.setVariable(item, 'User Accounts Request', email.body.Accounts);
}
// if (email.body.lts!= undefined)
// {
// cart.setVariable(item, servicelist.lts, email.body.lts);
// }
var rc = cart.placeOrder();

Thanks, Bill

7 REPLIES 7

jamie_caruvana
ServiceNow Employee
ServiceNow Employee

Bill, I don't believe you're going to get away that easy. 🙂 You will need to code the answers to the questions much in the way you have to target the item by sys_id. There is an article on the wiki that shows ordering an item via script (http://wiki.service-now.com/index.php?title=Useful_Scripts#Order_a_Service_Catalog_Item_From_Script) that might help demonstrate this.

Hope this helps.


Jamie, There is also this article http://wiki.service-now.com/index.php?title=Service_Catalog_Script_API. I reviewed them both in my rule creation attempt. I'll look at your suggested wiki article again, but may need more detailed direction. Thank you. -Bill


Bill_Collins
Mega Guru

I have been working with Jamie on a business rule to create a request on an inbound email.

I have all my variables passing to an approved request with an associated item. The item shows approved and shows the work flow or assigned tasks. However, the execution plan tasks are not initiating nor are they appearing on the item for related lists. If I direct submit the same request, i.e., not via an email the tasks kick off properly. I have turned off all related business rules which are not out of the box. We are not using a shopping cart. Following is the Global rule. Can you help?

// This script is placed on the Global table so it can be called from a number of
// different actions. It will place an order for the Generic catalog item. There
// are several pieces that need to be addressed across several tables. Comments
// are provided in each section of the script.
// This script is a modification of a script from Jamie Caruvana by Bill Collins

function orderGeneric(endDate, location, user, comments) {

// Insert a new request record and populate the request for with the caller field
var newRequest = new GlideRecord("sc_request");
newRequest.requested_for = user;
newRequest.requested_date = endDate;
// newRequest.approval = 'requested';
newRequest.insert();

// Insert a new item record. Set the catalog item to the generic item system id and
// the relationship with the above request by setting the request number on the item
var item = new GlideRecord("sc_req_item");
item.cat_item = "4266f9620a0a3c0d592d3e127a10dde3";
item.request = newRequest.sys_id;
item.insert();
// The var below will carry the new item's system id for later use
var ritmNo = item.sys_id;

// Now we have to setup the item options or variables. We are using the system id
// of the question. This will allow the text to be changed without affecting the
// script. We are carrying the assignment group from the form using the system id
// as well as grabbing the system id of option we're setting up. We need that in
// order to establish a relationship in the funtion call to the mtom table.
// We also set the order for the variable for display purposes on the item view.
// We need to do an insert and function call for each variable on the item.
var options = new GlideRecord('sc_item_option');
options.initialize();
options.cart_item = "4266f9620a0a3c0d592d3e127a10dde3";

// Below is the system id for "End Date";
options.item_option_new = "8acacbbd0a0a3c0d0d267dc71c48cfac";
options.value = endDate;
options.order = '1';
opQuestion = options.sys_id;
options.insert();
// Now call the mtom function
itemMTOM(ritmNo, opQuestion);

// Below is the system id for "User Location";
options.item_option_new = "8acc10f40a0a3c0d2598763e15c99301";
options.value = location;
options.order = '2';
opQuestion = options.sys_id;
options.insert();
// Now call the mtom function
itemMTOM(ritmNo, opQuestion);

// Below is the system id for "User";
options.item_option_new = "8acd23fc0a0a3c0d2bb4b68e109963ea";
options.value = user;
options.order = '3';
opQuestion = options.sys_id;
options.insert();
// Now call the mtom function
itemMTOM(ritmNo, opQuestion);

// Below is the system id for "Comments";
options.item_option_new = "8acdd0cb0a0a3c0d2a456db8a9b44cd3";
options.value = comments;
options.order = '4';
opQuestion = options.sys_id;
options.insert();
// Call the mtom function again for the next question or variable
itemMTOM(ritmNo, opQuestion);

// Set the state to approved to trick the Cascade business rule off to attach the workflow
newRequest.approval = 'not requested';
newRequest.update();

// The code below will place a message at the top of the panel
// as well as redirect to the new request record opened
gs.addInfoMessage("Request " + newRequest.number + " created");
action.setRedirectURL(newRequest);
}

// The function below is needed to establish a relationship between the new
// item and the variables (with answers) in order for those variables to be
// present on the item and task view. We are just setting the requested item
// to the item system id number and the option to the variable system id number
function itemMTOM(ritmNo, opQuestion) {
var sipm = new GlideRecord('sc_item_option_mtom');
sipm.initialize();
sipm.request_item = ritmNo;
sipm.sc_item_option = opQuestion;
sipm.insert();
}


Bill_Collins
Mega Guru

I have made progress. I can create the request, the item, and kick-off the execution plan tasks. I am also able to see the email content in the variable editor of the request item and execution plan tasks. The business rule follows.

***I still need to set the Requested_for and Requested_date on the Request form. I am happy to do this with another business rule if needed since I will have some direct submissions (not email) as as well. Any suggestions?***

function orderGeneric(endDate, location, termUser, comments) {

// nuke the current cart
nukeCart();

// new cart
var realCart = getCart();
var cartID = realCart.sys_id;

// Submit an offboarding request
addToCart(cartID, "4266f9620a0a3c0d592d3e127a10dde3", 1);
addOptions(cartID);
doOrder();
function addToCart(cartid, cat_item, quantity) {
var gr = new GlideRecord('sc_cart_item');
gr.initialize();
gr.cart = cartid;
gr.cat_item = cat_item;
gr.quantity = quantity;
gr.insert();
}
function addOptions(cartID) {

// Get the Cart Item
var kids = new GlideRecord('sc_cart_item');
kids.addQuery('cart', cartID);
kids.query();
if (kids.next()) {

// Look up the options for the item in the cart
var options = new GlideRecord('item_option_new');
options.addQuery('cat_item', kids.cat_item);
options.query();

// Add the Item Options
{
var gr = new GlideRecord('sc_item_option');
gr.initialize();
gr.insert();

// (Variable) Below is the system id for "End Date";
gr.item_option_new = "8acacbbd0a0a3c0d0d267dc71c48cfac";
gr.value = endDate;
gr.order = '1';
gr.cart_item.setValue(kids.sys_id);
gr.insert();

// (Variable) Below is the system id for "User Location";
gr.item_option_new = "8acc10f40a0a3c0d2598763e15c99301";
gr.value = location;
gr.order = '2';
gr.cart_item.setValue(kids.sys_id);
gr.insert();

// (Variable) Below is the system id for "User";
gr.item_option_new = "8acd23fc0a0a3c0d2bb4b68e109963ea";
gr.value = termUser;
gr.cart_item.setValue(kids.sys_id);
gr.order = '3';
gr.insert();

// (Variable) Below is the system id for "Comments";
gr.item_option_new = "8acdd0cb0a0a3c0d2a456db8a9b44cd3";
gr.value = comments;
gr.order = '4';
gr.cart_item.setValue(kids.sys_id);
gr.insert();
}
}
}
}
function nukeCart() {
var cart = getCart();
var id = cart.sys_id;
var kids = new GlideRecord('sc_cart_item');
kids.addQuery('cart', cart.sys_id);
kids.deleteMultiple();
}
function getCart() {
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
// we already have a cart all is well
}
else {
cart.initialize();
cart.user = userid;
cart.insert();
}
return cart;
}
function doOrder() {
var req = new Packages.com.glideapp.servicecatalog.RequestNew();
req.copyCart();
}