Multiple Request Item in one Request

ceraulo
Mega Guru

Hello!

I have the following variables in my catalog item.

Number of Users [1, 2, 3, 4, 5 or more]
Variable 1a
Variable 1b
Variable 1c
Variable 2a
Variable 2b
Variable 2c
Variable 3a
Variable 3b
Variable 3c 
and so on...

The requirement. is to create multiple Request Items based on the number of users.
For example, of Number of Users is selected 3 Request Items will be created. 3 Catalog Tasks will also be created. One for each Request Item.


The first RITM and SCTASK will contain Variable 1a, Variable 1b, Variable 1c
The first RITM and SCTASK will contain Variable 2a, Variable 2b, Variable 2c
The first RITM and SCTASK will contain Variable 3a, Variable 3b, Variable 3

Please help!

1 ACCEPTED SOLUTION

If you want to create two items under one Request, you would do something like this:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);

var item = cart.addItem('sys if of item');
cart.setVariable(item,'on_behalf_of', userOne);

var item_two = cart.addItem('sys if of item');
cart.setVariable(item_two ,'on_behalf_of', userTwo);

var rc = cart.placeOrder();

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

9 REPLIES 9

I'm not sure what you mean by 'show' but you can populate different variables specific to the item required.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi Paul,

I tried using the Cart API for Number of Users = 2, meaning 2 Request Items will be created. 

In the first RITM, all the variables are displayed in the Variables tab. The Requested for field is also correct.

However, in the second RITM the Variables tab is empty and the Requested for value is Guest.

This is my script for now.

createRequest();

function createRequest() {


var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('sys if of item');
cart.setVariable(item,'on_behalf_of', gs.getUserID());
var rc = cart.placeOrder();

}

function updateRITM(req) {

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', req);
ritm.query();

while (ritm.next()) {

ritm.u_requested_for = current.variables.requested_for; 
ritm.update();


}
}

Thank you.

 

 

If you want to create two items under one Request, you would do something like this:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);

var item = cart.addItem('sys if of item');
cart.setVariable(item,'on_behalf_of', userOne);

var item_two = cart.addItem('sys if of item');
cart.setVariable(item_two ,'on_behalf_of', userTwo);

var rc = cart.placeOrder();

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

This worked!

Thank you.

Simm
Tera Contributor

Mega Sage, 
where do i put that code to put in action ?