- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2015 04:01 PM
I need to add an item to an existing request.
The workflows require that certain variables exist and I'll need them to be visible in later tasks.
Some time ago (Calgary) I attempted to create them manually. However, this caused a problem in that, while I could access the variables, I could not display them in tasks.
The only solution that was proposed was creating a request item by using the Cart API to create a new cart and request.
The Cart API doesn't appear to have any options for adding to an existing request, and the only "solution" I've found in the community, is to create the Request item in this way, then delete the request.
Obviously this is a really ugly solution, especially since it kicks off tasks and workflows before I have a chance to delete it.
Is there a better solution that I'm just not finding?
Thanks,
-Stephen
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2015 08:54 AM
Hi Stephen,
Have you tried to create a new RITM within the same REQ using GlideappCalculationHelper? I found it awhile back, either here on the Community or some other site, but it's helped us to create an item in the same REQ. Unfortunately, I can't remember where I found it so I couldn't really explain how it works. It's not pretty, but at least you don't have to delete a REQ.
function addItem() {
// add item to current request
var reqHelper = new GlideappCalculationHelper();
reqHelper.addItemToExistingRequest(current.request, 'sys_id of item', 1); // 1 is the qty
reqHelper.rebalanceRequest(current.request);
// update/add values to variables on item
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.addQuery('request', current.request);
grReqItem.addQuery('cat_item','sys_id of item');
grReqItem.query();
if(grReqItem.next()) {
grReqItem.variables.some_true_false_var = true;
grReqItem.variables.some_other_var = 'Yes';
grReqItem.parent = current.sys_id;
grReqItem.variables.comments = "Add any comments here.";
grReqItem.update();
}
}
Also, have you tried adding the variables needed to both the current item and the item being added? If you have the workflow from Item A create Item B, making sure you set the "parent" of Item B as the sys_id of Item A, you could have Item B refer back to Item A by using the "parent" field and updating the variables that way (query for Item A using "parent" and then set the variables).
We do something like this in some other workflow, but this might be more complicated than necessary.
Anyway, hope some of this helps!
Barb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 07:33 AM
the second part of the script finds the item and populates the variables...
- var grReqItem = new GlideRecord('sc_req_item');
- grReqItem.addQuery('request', current.request);
- grReqItem.addQuery('cat_item','sys_id of item');
- grReqItem.query();
- if(grReqItem.next()) {
- grReqItem.variables.some_true_false_var = true;
- grReqItem.variables.some_other_var = 'Yes';
- grReqItem.parent = current.sys_id;
- grReqItem.variables.comments = "Add any comments here.";
- grReqItem.update();
- }
did you update this script with the variables that are in your list collector by looping through the collector?? i would add some logging to this script to ensure it is executing correctly ..
lines 6 and 7 replace with a loop to go through each item in the list collector... this is an example script where i did that...notice i looped the entire thing in a fore loop... and split out my list collector into an array..
list = current.variables.v_santa_additional_secured_area_acc.toString();
var list_split = list.split(',');
var secure_area = 'e891c69d13a1fe008e9c7e276144b03c'; //sid for the wecure item access
list = current.variables.v_santa_additional_secured_area_acc.toString();
var list_split = list.split(',');
var secure_area = 'e891c69d13a1fe008e9c7e276144b03c'; //sid for the wecure item access
for(var counter = 0 ; counter< list_split.length; counter++){
//create a new item in the request
var reqHelper = new GlideappCalculationHelper();
reqHelper.addItemToExistingRequest(current.request, secure_area, 1); // 1 is the qty
reqHelper.rebalanceRequest(current.request);
//find the item and update itsvariables
var grReqItem = new GlideRecord('sc_req_item');
grReqItem.addQuery('request', current.request);
grReqItem.addQuery('cat_item',secure_area);
grReqItem.addQuery('parent','');
grReqItem.query();
if(grReqItem.next()) {
//gs.addInfoMessage('Found item ' + grReqItem.number);
grReqItem.variables.requested_for = current.variables.requested_for;
grReqItem.variables.v_area = list_split[counter];
grReqItem.parent = current.sys_id;
grReqItem.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 07:49 AM
Hi Raymond, Yes I am doing that in the script. Here is my full script, I am just adding 'script' as a comment for all child request items. And as I have mentioned earlier, these updated variables in the child request items are showing up fine in the UI when I am clicking on a child request item.
Also I am attaching the screenshot of a request item below just to provide you with some more information. Somehow, I am not able read the same variable in the workflow using 'current.variables.u_additional_comments' variable or by any other way.
var locations= current.variables.u_door_location.toString();
var locationsArray = locations.split(",");
for(var i=0; i < locationsArray.length; i++)
{
var reqHelper = new GlideappCalculationHelper();
reqHelper.addItemToExistingRequest(current.request, current.cat_item, '1');
reqHelper.rebalanceRequest(current.request);
grReqItem.parent = current.sys_id;
grReqItem.variables.u_requested_by = current.variables.u_requested_by;
grReqItem.variables.u_requested_for = current.variables.u_requested_for;
grReqItem.variables.u_action = current.variables.u_action;
grReqItem.variables.u_job_function = current.variables.u_job_function;
grReqItem.variables.u_door_location = locationsArray[i];
grReqItem.variables.u_additional_comments = "script";
grReqItem.update();
}
Thank you in Advance!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 07:55 AM
additional comments <oob field> is a journal entry field and as such is always empty... to get the latest additional comment you use
getJournalEntry(-1) <this gets all journal entries>
this has a pretty good write up on journal fields http://wiki.servicenow.com/index.php?title=Using_Journal_Fields#gsc.tab=0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2017 08:17 AM
Thank you for that info! I have attached the screen shot of "comments" just to show that I am not able to read the Request Item variables in the work flow for all the Request Items that I created through script. Here is the screen shots of parent and child request items work flow activity. You can clearly see that the parent request item is logging all the variable info when going through the run script in workflow, where as child request item is not able to log any info other than the request number. Here is the logging script.
workflow.info('Logging request item number {0}', current.number);
workflow.info('Logging Comments {0}', current.variables.u_additional_comments);
workflow.info('Logging Door Info {0}', current.variables.u_door_location.toString());
workflow.info('Logging Job Function {0}', current.variables.u_job_function);
workflow.info('Logging UI Action Add/Remove {0}', current.variables.u_action);
workflow.info('Logging Requested By {0}', current.variables.u_requested_by);
workflow.info('Logging Requested For {0}', current.variables.u_requested_for);
workflow.info('Logging Requested For ID {0}', current.variables.u_req_for_corp_id);
Parent Request Item work Flow activity
Child Request Item work Flow activity
THANK YOU!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2017 09:02 PM
After troubleshooting this issue further, I have figured out that the Workflow is not able to pull the RITM varibales on the current child RITM the Workflow is running.
I have created the following run script in the workflow to log all RITM variables in a request and noticed that the RITM variables are always coming as null on the current child RITM on which the workflow is running, where as it is able to pull the RITM variables from a parent RITM and also a child RITM that was created prior to the current RITM. I have also attached the screenshot of Workflow log activity for better understanding of this problem.
Any ideas or suggestions to get around this problem?
Script used in Workflow Run Script:
var grReqItem = new GlideRecord('sc_req_item');
// Querying on request, using sysid of request
grReqItem.addQuery('request','=', current.request);
grReqItem.query();ating through all the ritms in a request
while(grReqItem.next()) {
workflow.info('**************************************************');
workflow.info('Item Number: '+grReqItem.number);
workflow.info('Comments: '+grReqItem.variables.u_additional_comments);
workflow.info('**************************************************');
}