The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Adding multiple items to the Cart via Script.

PrudeAnurag
ServiceNow Employee
ServiceNow Employee

Hello Everyone,

 

I'm trying to implement a virtual cart which will collect the records from webservice and add all the records as a Item in the cart.

 

In the Example at wiki only one item is added via script. while I am trying for multiple item.

 

The problem i am facing here is I'm not able to get the Options(Variable) for the Items populated when checked under sc_cart.list

 

I am able to add the Items but the Options is not getting added as the below code from Order a Service Catalog Item from a Script - ServiceNow Wiki is not able to populate the variable in 'sc_item_option' related list.

while(options.next())

{ var gr = new GlideRecord('sc_item_option'); gr.initialize();

if(options.question_text == "Is this a replacement for a lost Blackberry device  ?")

{

gr.item_option_new.setValue(options.sys_id); gr.value = "No"; }

else if(options.question_text == "If it is a replacement, what was the original phone number  ?")

{

gr.item_option_new.setValue(options.sys_id);

gr.value = "(858)345-1418"; }

else if(options.question_text == "Special Requirements")

{ gr.item_option_new.setValue(options.sys_id);

gr.value = "Need UK charger attachment.";

} gr.cart_item.setValue(kids.sys_id);

gr.insert();

}

The highlighted line in red only initializes but the sc_item_option but the actual variables from the items are not added.

Need some help so that my options/variables get automatically populated in the table.

7 REPLIES 7

sumeet_n
Kilo Guru

Chandan23
Tera Expert

Go through the script given in the link:


Modify it according to your requirement:



Order a Service Catalog Item from a Script - ServiceNow Wiki


Kalaiarasan Pus
Giga Sage

this might help ...



var cart = new Cart();


var data = ['requirement 1','requirement 2'];


for(var i=0;i<data.length;i++)


{


var item = cart.addItem('e2132865c0a8016500108d9cee411699');


cart.setVariable(item, 'replacement', 'No');


cart.setVariable(item, 'requirement', data[i]);


}                        


var rc = cart.placeOrder();


gs.log('KKK-'+rc.number);