- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2016 11:14 AM
Hey all,
I've been beating my head against a wall on this for a few days now and haven't been able to find a solution.
I have an inbound action that will create a request and requested item(s) based on the email coming in. I have the logic working outside of the cart API to find the appropriate text in the email that should trigger ordering a particular item and kick off the workflow. However, I get an extra, mystery RITM that gets created with no catalog item associated (or anything else).
Here's the code where the RITM gets created:
function createRequestItem() {
var grRequestItem = new GlideRecord ("sc_req_item");
gs.log("RITM Created", "EMAIL." + sys_email.sys_id);
grRequestItem.short_description = subject;
grRequestItem.description = "received from: " + email.origemail + "\n\n" + email.body_html;
grRequestItem.request = current.sys_id;
grRequestItem.cat_item = 'Deployment Request'; //Both of these seem to be required to set the item properly
grRequestItem.cat_item.setDisplayValue('Deployment Request');
grRequestItem.insert();
//Trigger the workflow for the catalog item
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Deployment Tasks");
w.startFlow(wfSysId, current, current.operation());
}
I cannot figure out why a second RITM is being created that is essentially empty. I've tried using grRequestItem.initialize(); before setting any values and that doesn't seem to make a difference.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-26-2016 08:26 AM
Thanks for the help everyone! I was able to figure it out. It seemed to be a combination of how I was setting the catalog item (using the setDisplayValue and a regular assignment vs using the sys_id of the item being ordered) and the how I was kicking off the workflow.
Using the sys_id to set the item stopped the extra RITM from showing as "catalog item removed" and then getting rid of the workflow stopped it being created.
I'm still a bit perplexed as to why I was getting an extra RITM with this and not other places where I do the same thing to start the workflow. The best I can figure is that the workflow was triggering once with the setDisplayValue command, then again with the straight assignment, and then actually starting at the end of the function. Hopefully this will help someone in the future. I am calling this one good.
For everyone that recommended using the cart API, I have a few reasons why I don't: there's tons of information about the deployments we do that comes in the email and I would have to create extra variables for each type of deployment in the catalog (I know this is a bit lazy, perhaps I'll do it in the future), the other big reason is that this function is more reusable in the long run as I don't need to set individual variables for every catalog item when a new team wants to implement something like this or starts offering a new product.
Edit: Added some additional information on what I think was happening and why I don't use the cart API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2016 07:22 AM
Nope, the workflow is just handling some approvals and generating catalog tasks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2016 07:19 PM
If you comment out grRequestItem.insert(); Does it still create the additional RITM?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2016 07:26 AM
Just tried it, yeah it still creates the extra item.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2016 11:29 AM
I usually do this using the Cart API, which gets around these kinds of issues by ordering an item on behalf of a user just like it works through the UI when a user submits a catalog item.
An easy to follow example from http://www.servicenowelite.com/blog/2014/2/6/request-generation-methods
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-21-2016 11:53 PM
Creating a record on RITM is a bad idea. I would suggest you use cart API.