Using Cart API() to add multiple cat items

mdash
Giga Guru

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

14 REPLIES 14

Hi Alan,
I will give CartJS a try. Looks promising to me.

Hi @mdash 

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!

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.

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ah, ok.

I'm glad you got it resolved. There's also the Cart API, which you were already using...but perhaps accidentally incorrectly?

https://docs.servicenow.com/bundle/orlando-application-development/page/script/server-scripting/refe...

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!