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

Jotiram Yadav
Tera Expert

var cart = new Cart();


var item = cart.addItem(sysid, quantity);


cart.setVariable(item, 'variableName', value);


.


.


.


.




var item1 = cart.addItem(sysid, quantity);


cart.setVariable(item1, 'variableName', value);


.


.


.


var rc = cart.placeOrder();






Check if this works for you.




Thanks.


Dear Jotiram,



Thanks for the response.


Below is the scenario i am working on:



WebService populates multiple records at a time on the custom table"u_abc". Now when ever the record is inserted a business rule runs which help the records to be added in cart.



If I use "var cart = new Cart();" a new cart will be initialized at the run of business rule. So suppose if 10 records are inserted the business rule will initialize 10 different cart and the items wont get added to the first initialized cart..


I need to attach all the records to one cart with variable information..


I have been able to achieve the addition of items to one cart but the Variables/options for all the items is not getting added..


I need the help on this part...



Thanks.


Hi Anurag,



The best way to add the items is through the Catalog API, but I understand what you're saying that in that case you would have 10 different carts (creating 10 different requests), and you would rather have all the items and variables grouped under one request on the back end. I don't have a great answer for you, but I've seen a couple of different things here before.



The first would be instead of running you script in an insert business rule, you could run it in a scheduled job. That would give you the opportunity to initialize a new cart and then iterate through each of the items and add them to your cart in the same script.



The other would be to allow ServiceNow to create new requests for all of your items and then run something on the back end that associates the newly created items with the request you want and deletes the empty requests. You could run into some issues with auditing and numbering in this case, though.



You could also populate a parent record on your new table and associate all the children with the parent and then run something on the parent.



Again, if I'm understanding you correctly none of these is exactly what you're looking for, but you do have some options.


Brad, thanks for the reply and suggestions..


The suggestions are logical and can be implemented... but the problem arises is that no soon the records are inserted the Person will be redirected from one tool to ServiceNow to check the cart.. Now this action will take place in some few seconds and writing a scheduled job is not feasible..


What i'm thinking is to club the records with one unique id and then do a gliderecord to the table created for the records with similar unique id's .. here i may be able to initialize the cart at top while I can run the While loop and start inserting the variable item...




I have not implemented it yet, but will be doing soon. Once I accomplish my requirement i will update the post...