Set Multi-Row-Variable-Set using Cart API

A Elbarbary
Giga Guru

Hi Everyone,

I'm using the cart API as follows to create some orders automatically (inside some scripted REST API)

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('someCatalogItemId');
cart.setVariable(item, "someMultiRowVariableSetName", "[{\"plugin_name\":\"plugin 1\"},{\"plugin_name\":\"plugin 2\"}]");

However it's not working, the order is created with an empty multi row variable set, trying to supply a stringified JSON is not working, how can I proceed?

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abdelrhman Elbarbary 

you cannot set MRVS using Cart API.

You can update the variable post RITM creation

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('someCatalogItemId');

//fill in the variables on the request item form
cart.setVariable(item, "asset", "00a96c0d3790200044e0bfc8bcbe5dc3");
cart.setVariable(item, "multiple_choice", "Phoenix");
var rc = cart.placeOrder();

// query RITM and update that
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', rc.sys_id);
ritm.query();
if(ritm.next()){

	ritm.variables.someMultiRowVariableSetName = "[{\"plugin_name\":\"plugin 1\"},{\"plugin_name\":\"plugin 2\"}]";
	ritm.setWorkflow(false);
	ritm.update();

}

Regards
Ankur

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

View solution in original post

7 REPLIES 7

@mgcasey300 

Thank you for sharing.

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

Hi Ankur,

 

I have tried the above one, when try to hard code the values to Multi row variable set it is working, but when i try to fetch the value from the request body it is not working, Below is the script i have written and calling rest api.


var
request_body = request.body.nextEntry();

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('ed09447697e0421023d0fac6f053af9b');
cart.setVariable(item,'request_type',request_body.requestType);
cart.setVariable(item,'requested_for',request_body.requestedFor);

var rc = cart.placeOrder();
var mrvs = request_body.mvrs_demo;

gs.info("mrvsCartITem mrvs: "+ JSON.stringify(mrvs));
var mrvsString = JSON.stringify(mrvs);
var scItem = new GlideRecord('sc_req_item');
scItem.addQuery('request',rc.sys_id);
scItem.query();
if(scItem.next()){
    //Set variables in Multi Row Variable Set
    //scItem.variables.enter_primary_site_contact = "[{\"primary_phoneNumber\":\"642345789\",\"primary_email\":\"test1@example.com\",\"primary_location\":\"hyderabad\"},
    scItem.variables.enter_primary_site_contact = mrvsString;
    scItem.setWorkflow(false);
    scItem.update();
}

Please find the screenshot for request body. Could you please help me how to do this
Mallikarjunare1_0-1710157297938.png

 

Constantine Kr1
Giga Guru

In the Rhome version, I was able to get this to work.

  1. Create a JSON object
  2. Stringify the JSON object
  3. Put square brackets at the start [ and end of the JSON string.

 

Pasted below is an example of the string being hard coded into the item variable. 

var cart = new sn_sc.CartJS();
var item = {
    'sysparm_id': 'a9cad13a2f110110c9ebdcb6f699b6fa',
    'sysparm_quantity': '1',
    'variables': {
        'string_on_base_vars': 'Sample String Value',
        'mrvs_demo': '[{"mrvs_date_var":"2022-02-04","mrvs_checkbox_var":"true"}]'
    }
};
var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();
gs.info(checkoutInfo);

Detailed article on MRVS/CartJS: CartJS: How to populate a MRVS (Multi Row Variable Set)
CartJS API: https://developer.servicenow.com/dev.do#!/reference/api/sandiego/server/sn_sc-namespace/c_CartJSScop...