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

Lisa Silvaroli
Tera Guru

Does anyone know how I can get this to update a specific RITM?


Say I have a field in   in a from in the service catalog asking for the Request Item # - and then I want the information filled out to be added to that already existing RITM?


Hi Lisa,



You shouldn't need to use the GlideappCalculationHelper to update an existing RITM. The solution to what you want to do would actually be simpler.



You would pretty much just take the 2nd part of the code in the first post where the RITM is being updated.



In the query, you would query for the RITM you want to update using the value of the reference field.



You could use a get, since it's one record. It would be something like:



var gr = new GlideRecord('sc_req_item');


gr.get(fieldName); //name of reference field


gr.variables.variableName = current.variable.variableName //for each variable or field you want   to update.


gr.update();


Thank you Thomas!



I think I am missing something though -


I want the fields in this form:


RATForm.JPG


to be displayed on this form when submitted:


RTIM.JPG


Similarly to the Variables that are already listed... is that possible? I am not seeing how to get them to display. OR if you have any suggestion on another way to achieve this...


I was able to generate a RITM but the variable layout is all over the place. Any ideas what's causing this? I'm using only variable sets, ex: If I use 2 variable sets the layout is normal but if I have more than two it's all over the place.  



find_real_file.png


if you go into the item from the service catalog does it lay it out correctly?



sounds like you may have the order messed up in the item itself.... also if not please post what version you are on and any other information.. drawing the RITM form variable section is based on the item itself from maintain items.