Trigger Workflow Based on Catalog Item Selection in Order Guide

jmiskey
Kilo Sage

I have kind of a tricky situation which I am trying to figure out how to approach.

We have an Order Guide with three Catalog Items to choose from.  The Order Guide itself does not have a workflow attached to it.  2 of the 3 Catalog Items in my Order Guide do.  They are pretty straight forward, creating a single RITM, so those Workflows run off of sc_req_item and are available to be selected from the Workflow field on the Catalog Item.

However, my third one is a bit trickier.  It needs to create multiple RITMs, so the Workflow needs to run against sc_request so we can create multiple RITMs within that workflow.  However, when we create a workflow based on sc_request, it is not available to be selected from the Workflow field on the Catalog Item.  

I see from Workflow -> Properties -> Conditions, I could try to set a condition of which the workflow should run.  We have done this before, based on the Order Guide value.  However, that Order Guide was pretty straight forward and didn't have three different Catalog Items, each with their own workflow, listed underneath it.  So the Order Guide field,in and of itself, does not contain enough information regarding when the workflow should be called.  It is dependent upon the Catalog Item selected in the Order Guide (have a variable for that selection).

So, how can I do this?  How can I set up the Workflow for this third option to run off of sc_request and get it to be called when that third option is selected?

Thanks

1 ACCEPTED SOLUTION

This is what I have used to trigger new items from an initial item and added to the same request.

var reqHelper = new GlideappCalculationHelper();
reqHelper.addItemToExistingRequest(current.request,<sysID of the item>, 1); // 1 is the qty
reqHelper.rebalanceRequest(current.request);

My use case:

We had an item that collected multiple share file locations, but needed individual approvals for each location.  My setup was an initial item that runs a script with this code in it to generate the individual items under the same REQ.  The original item closes automatically after the fact, so one 'item' spawns x number of individual items (dependent upon how many share file locations they request), all on the same REQ.

 

https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=GlideappCalcHelperAPI

View solution in original post

35 REPLIES 35

Thanks for the reply.  Good thoughts, unfortunately, none of them seemed to work.

I tried adding the GlideAppCalculationHelper code in various places in my code (inside my loop, at the end of my code, before the delete RITM step off the code), but none of them worked.

I also tried adding this code to update the request:

current.short_description = 'Role Maintenance Request';
current.description = 'Role Maintenance Request';
current.update();

but that didn't work (not only did it not trigger, it actually didn'y update my fields - not sure why).

To answer your other question, the only thing under Related Links for each RITM is "Force to Update Set".

 

 

OK, I figured out why my Request wouldn't update.  I forgot that I was on the RITM, not the REQ, so "current" wouldn't work.  

I updated that part of the code to:

//update current request
var req = new GlideRecord('sc_request');
req.addQuery('sys_id', orig_req);
req.query();

while (req.next()) {
	req.short_description = 'Role Maintenance Request';
	req.update();
}

and that did update the fields on my Request then. 

Unfortunately, it still did not add/trigger the workflow on the new RITMs!

It looks like you are just inserting records into the sc_req_item table.  I don't think you can do that and trigger a workflow.  You have to script it thought the API and it will create a new request with the appropriate request items under it lunching there individual workflows.

var cart = new Cart(null, gs.getUserID()); //Create the cart as the current user
var item = cart.addItem("sys_id of catalog item");
cart.setVariable(item, 'var name', 'value');
cart.setVariable(item, 'var name 2','other value');
var rc = cart.placeOrder();

I am not quite sure what you mean by "script it through the API".  I tried adding code like you have above in my "Run Script" action, and it did indeed create new RITMs, and they did have the workflows attached to them, but it put each RITM in its own REQ instead of listing them all under the same REQ.

Is there something different that needs to be done to get them all added under the same REQ?

The code I gave you is the API.  You should be able to add multiple items to the same cart which should make it under the same request.

var cart = new Cart(null, gs.getUserID()); //Create the cart as the current user
var item = cart.addItem("sys_id of catalog item");
var item1 = cart.addItem("sys_id of catalog item");
var item2 = cart.addItem("sys_id of catalog item");
cart.setVariable(item, 'var name', 'value');
cart.setVariable(item, 'var name 2','other value');
cart.setVariable(item1, 'var name', 'value');
cart.setVariable(item1, 'var name 2','other value');
cart.setVariable(item2, 'var name', 'value');
cart.setVariable(item2, 'var name 2','other value');
var rc = cart.placeOrder();