Using Cart API() to add multiple cat items
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 06:49 AM
Hi,
Has anyone successfully added multiple items to a cart and placed order using Cart API()? I am having a hard time doing that.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('my sys id');
for(var i=0;i<my Variable.length; i++){
cart.setVariable(item,'requested_for',requestedFor);
cart.setVariable(item,'requested_by',requestedBy);
cart.setVariable(item,'model',model);
cart.setVariable(item,'quantity',quantity);
cart.setVariable(item,'device_model_name',asset);
cart.update();
}
var rc = cart.placeOrder();
gs.log("Request is "+rc.number);
It only creates a one RITM attached to the REQ. Any clues?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 07:17 AM
Hi Alan,
I will give CartJS a try. Looks promising to me.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2020 10:15 AM
Hi
It's been about 2 weeks. Did you end up using the CartJS as mentioned by me above? I hope it turned out well for you.
If so and/or my post helped guide you correctly, please mark it as Helpful/Correct.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2020 03:05 AM
Hi Allen,
I referred to CartJS. But that could not be applied in my case as the requested for user need not be logged in user.
So, I just tweaked my script and added items to cart only inside the for loop and then outside the loop, placed the order. That solved the puspose.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2020 03:27 AM
Hi,
Glad to hear.
Saying this, I will appreciate if you close this thread, as answered.
Have a great rest of the day
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2020 05:47 AM
Ah, ok.
I'm glad you got it resolved. There's also the Cart API, which you were already using...but perhaps accidentally incorrectly?
Doesn't require the user to be logged in as CartJS.
Please mark reply as Helpful/Correct, if applicable.
Take care!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!