- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2018 08:02 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2018 12:47 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2018 01:06 PM
Because you are calling a script that creates a shopping cart if I remember correctly it will create a request with a RITM under it just like if a user went in and submitted the something thought the interface. I do not have access to that instance anymore since I no longer work for that company.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2018 01:14 PM
Correct.
In my case, this was a 'dummy' item (item A) that then spawned the needed number of individual items of the same item (Item B). Once the item B's are created, Item A just completes (and a comment added explaining the reason for the closure).
I've added the item A workflow.
Notice I delay a few seconds just to make sure the REQ is actually there before I proceed.
I then create the items, and with separate script activities populate the variables. Users have a choice of read/write or read only lists, so I have them split out in the variable association part based on that.
In the last activity for Item A, I add this for the user:
current.additional_comments = 'A new requested item has been created for each file location. This parent item has been closed.';
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-27-2018 01:18 PM
Thanks. I am getting ready to leave for the week, but will try playing around with the idea next week and see I can get it to do what I want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2019 11:17 AM
So, I created some code that does MOST of what I want. It spawns off new RITMs, based on the selections, and deletes the original one (this is actually one script of three different ones; they are dependent upon choices, and the other ones are a little more complex).
Here is what the code looks like:
//capture key fields from original ritm
var orig_sys_id = current.sys_id;
var orig_req = current.request;
var orig_ritm = current.number;
var orig_submitter = current.opened_by;
var orig_bene = current.u_beneficiary;
var orig_short_des = current.short_description;
var orig_cat_item = current.cat_item;
var orig_due_date = current.due_date;
//capture key fields off of catalog item
//var role_owner = current.variables.facets_add_role_owner;
var applications = current.variables.facets_applications_role_needs_access_to; //list collector
//split applications list collector
var ind_app = applications.toString().split(',');
//loop through each application and create a new ritm
for(var i = 0; i < ind_app.length; i++){
var app = ind_app[i];
//get facets application name
var app_name= '';
var a = new GlideRecord('u_facets_application_owners');
a.addQuery('sys_id', app);
a.query();
while (a.next()) {
app_name = a.u_application;
}
//create new ritm
var ritm = new GlideRecord('sc_req_item');
ritm.newRecord();
ritm.request = orig_req;
ritm.cat_item = orig_cat_item;
ritm.request = orig_req;
ritm.requested_for = orig_bene;
ritm.u_beneficiary = orig_bene;
ritm.due_date = orig_due_date;
ritm.opened_by = orig_submitter;
ritm.short_description = orig_short_des + ': Add New Role to ' + app_name + ' Application';
//update u_sub_ritm so subritm doesn't call more subritm (avoid inifinite loop)
ritm.u_sub_ritm = true;
ritm.insert();
//copy variable section from original ritm to new ritms
var scvar = new GlideRecord('sc_item_option_mtom');
scvar.addQuery('request_item.sys_id', orig_sys_id);
scvar.query();
while (scvar.next()) {
var vown = new GlideRecord('sc_item_option_mtom');
vown.newRecord();
vown.sc_item_option = scvar.sc_item_option;
vown.request_item = ritm.sys_id;
vown.insert();
}
}
//delete original ritm
var ritm2 = new GlideRecord('sc_req_item');
ritm2.addQuery('number', orig_ritm);
ritm2.query();
ritm2.next();
ritm2.deleteRecord();
The only issue is that there are no workflows attached to the newly spawned off RITMs, even though they have the same cat_item value as the original RITM (which I thought was the field that was used to determine whicj workflow to attach to it).
How can I amend my code to get the new RITMs to have the same workflow attached to them that the original RITM had?
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-02-2019 11:39 AM
My guess is since you are creating the RITMs via your script and not using the
new GlideappCalculationHelper(), it is probably not triggering the workflow engine. Not sure exactly how it works, but that function creates the RITM without having to code it. My assumption is it triggers the same process that a submit would do.
One thought you might try... Update the original REQ record. Maybe that might trigger the workflows with an update of the REQ record. Otherwise, you might have to trigger them via script.
Does the RITM's have the 'Start Workflow' UI action in the related links?? Might be able to use that to cycle through your created RITMs to trigger them.
You might also try just using the rebalance call from that method.
var reqHelper = new GlideappCalculationHelper();
reqHelper.rebalanceRequest(orig_req);