Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Creating multiple Request Items based on number selected

dave_m13
Kilo Contributor

Hi

I have a UI Action that creates an event, script action then calls a script include to add a Catalog item and fill in various variables then places the order to create 1 Request and 1 Request Item automatically. This is all fine and works as it should.

I'm struggling with the following - On the UI Action table there is a quantity field, if this quantity is say 5, I want it to create 5 RITMS for the same catalog item, same field data and variables. Ideally they would sit under the 1 Request, however it could be that we have 5 separate requests with 1 RITM under each, this is how it's done currently but manually using the order guide.

Has anyone done anything similar or have an hints how I may achieve this.

Thanks
Dave

7 REPLIES 7

adiddigi
Tera Guru

Refer to this URL: https://wiki.servicenow.com/index.php?title=Service_Catalog_Script_API

Now, I'm not sure of the code that you are using in the Script Include, but to create a Request item properly you should first create a cart, add items to cart and submit the cart which creates corresponding request and requested items. The cart will be translated to a request and the cart items you add to this cart will become requested items.

Here is the code :



var cart = new Cart();
var item = cart.addItem('e46305bdc0a8010a00645e608031eb0f');
cart.setVariable(item, 'os', 'Linux Red Hat');
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);


Step1 : Creates a Cart programatically.
Step2 : Adds an item(Catalog Item) with sys_id as e4....0f
Step3: Sets a variable on the item you just created.
Step4: Orders the Cart, Which will be created into a requested item and a corresponsing request.

Now the scenario where you will have to add more requested items to a single request:




var cart = new Cart();
var item = cart.addItem('e46305bdc0a8010a00645e608031eb0f');
cart.setVariable(item, 'os', 'Linux Red Hat');
var item2 = cart.addItem('e46305bdc0a8010a00645e608031eb0f');
cart.setVariable('item2','os','Windows Server');
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);


will create a new item(item2) under the same cart and hence under the same request. You now have two requested items one with os RedHat and the other a Windows Server and one request for both.

Let me know if you need anything else.


Hi , 

 

I am trying to use this code but its not working for me 

i am using it in the Run script of the workflow

Please tell me the mistake i am doing over here

Do i have to pass the backend value of the group name of the question name ??

Please help

find_real_file.png

dave_m13
Kilo Contributor

Thanks Abhiram, yes this is the code I'm using.

Only issue I have with this, is that quantity could be anything 40, 50 or more.

Also I have 26 variables.

Thanks
David




var cart = new Cart();
while(i < 50){

var item2 = cart.addItem('e46305bdc0a8010a00645e608031eb0f');
cart.setVariable('item2','os','Windows Server');
cart.setVariable('item2','port','80');
cart.setVariable('item2','disksize','300');
... all 50 variables
i++
}

var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);