How to add a new item to an existing request with catalog variables

Stephen W_
Giga Guru

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

1 ACCEPTED SOLUTION

barbsies
Tera Expert

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


View solution in original post

29 REPLIES 29

barbsies
Tera Expert

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


This works beautifully.   One change I made however, was to grab the most recent record, rather than filter by the catalog item, as there could be more than one line with this item.



grReqItem.addQuery('cat_item','sys_id of item');


Becomes


grReqItem.orderByDesc('sys_created_on');


Good deal!  


Hello Stephen and Barbara,



First, thank you very much for posting this, I have been trying SEVERAL different things without much luck. This seems to work, but for some reason my SN Fuji instance behaved strangely once I added this code to my Workflow. After I submitted the Catalog Item 'order' the instance lagged for about 169 secs and then the Order Summary screen came up. Seems that the Workflow for some reason created 66 RITMs under the same Request.



I double checked my code and I do not have any loops that would cause this, in fact I tried applying this almost verbatim with the same result. Any idea on what may be happening here? If I dont use the code above, only one Request and one RITM are created. Any information would be greatly appreciated, thank you in advance!