- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2020 12:58 AM
Hi all,
I'm tring to populate variables in cart api,
Below is my coding:
var cartId = GlideGuid.generate(null); var cart = new Cart(cartId); var item = cart.addItem('e46305bdc0a8010a00645e608031eb0f'); cart.setVariable(item,'os','Linux Red Hat'); var rc = cart.placeOrder(); gs.addInfoMessage(rc.number);
My question is how to populate a variable in variable set using cart.setVariable() method, thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2020 10:49 AM
Use the same approach, when a VarSet is added to a Catalog Item its variables are accessible in the same way as its own.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('9aa4f8f1db002010189f2706ca9619ce');
cart.setVariable(item,'process','Process 123');
cart.setVariable(item,'source','Source 123');
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);
See the following screenshots
Cheers,
Oscar Lopez
Please mark this as helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2020 10:49 AM
Use the same approach, when a VarSet is added to a Catalog Item its variables are accessible in the same way as its own.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('9aa4f8f1db002010189f2706ca9619ce');
cart.setVariable(item,'process','Process 123');
cart.setVariable(item,'source','Source 123');
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);
See the following screenshots
Cheers,
Oscar Lopez
Please mark this as helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2020 12:00 PM
Hi,
Did you try this:
https://community.servicenow.com/community?id=community_question&sys_id=e5896703dbcffb4023f4a345ca96196a

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2020 01:14 PM
I personally find the newer scoped API is easier to use.
var cart = new sn_sc.CartJS();
var item = {
'sysparm_id': '399ab6812f80ac101c43bed72799b6fa', //sys_id of cat item
'sysparm_quantity': '1', //Only order 1 RITM
//Example of how you can populate variables
'variables':{
//Left = Variable name
//Right = Variable value
'Change': current.getUniqueValue(),
'Date': new GlideDate().getDisplayValue()
}
};
cart.addToCart(item); //Add the JSON item above to the cart
var b = cart.checkoutCart(); //Submit the cart
gs.print(JSON.stringify(b)); //the checkout process returns a JSON value containing sys_id and REQ number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2020 08:58 PM
If you want to populate a Multi Row Variable Set, you can try this:
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('e46305bdc0a8010a00645e608031eb0f');
cart.setVariable(item,'os','Linux Red Hat');
//Set variables in Multi Row Variable Set
var mrvsArr = [];
//replace question_a with the first variable question_b the second (technical) name of your variable
mrvsArr.push({"question_a" : "answer 1", "question_b" : "answer 1"});
mrvsArr.push({"question_a" : "answer 2", "question_b" : "answer 2"});
var mrvsArrJson = JSON.stringify(mrvsArr);
//change mrvs to the (technical) name of your multi row variable set
cart.setVariable(item, 'mrvs', mrvsArrJson);
var rc = cart.placeOrder();
gs.addInfoMessage(rc.number);
To add more you just repeat the line, with different values:
mrvsArr.push({"question_a" : "answer n", "question_b" : "answer n"});