The CreatorCon Call for Content is officially open! Get started here.

Populate variables in cart api

HuChin
Tera Contributor

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!

 

1 ACCEPTED SOLUTION

Oscar Lopez
Mega Guru

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

find_real_file.png

 

 

find_real_file.png

 

find_real_file.png

 

 

find_real_file.png

Cheers,

Oscar Lopez

Please mark this as helpful.

View solution in original post

5 REPLIES 5

Oscar Lopez
Mega Guru

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

find_real_file.png

 

 

find_real_file.png

 

find_real_file.png

 

 

find_real_file.png

Cheers,

Oscar Lopez

Please mark this as helpful.

Akshay H Mulky
Kilo Guru

Hi,

Did you try this:

https://community.servicenow.com/community?id=community_question&sys_id=e5896703dbcffb4023f4a345ca96196a

Kieran Anson
Kilo Patron

I personally find the newer scoped API is easier to use.

sn_sc.CartJS

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

Willem
Giga Sage
Giga Sage

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"});