Service catalog script API and multi-row variable sets

Bernd Kallweit
Mega Expert

Hi,

I need to create a catalog request using the Service catalog script API. The catalog item contains a multi-row variable set I need to fill as well. Creating the cart and setting variables works ok, but how would I set the multi-row?

What I tried:

var cart = new Cart();
var item = cart.addItem("my cat item SID");
cart.setVariable(item, "normal_variable", "ABC");
// building the multi-row
var mr = [{
"var_1" : "value 1",
"var_2" : "value 2"
}];
cart.setVariable(item, "variable_set_internal_name", JSON.stringify(mr));
var req = cart.placeOrder();

As a result, this produces the desired request and requested item, with normal variables set; but the multi-row remains empty. 

Anyone with a suggestion?

1 ACCEPTED SOLUTION

Bernd Kallweit
Mega Expert

After some fiddling around, I came to a solution that deals with the multi-row after the order was placed. Something like:

var cart = new Cart();
var item = cart.addItem("my cat_item SID");
cart.setVariable(item, "normal_variable", "ABC");
var req = cart.placeOrder();
var ritm = new GlideRecord("sc_req_item");
if (ritm.get("request", req.getValue("sys_id"))) {
var mr = ritm.variables.variable_set_internal_name;
var newRow = mr.addRow();
newRow.var_1 = "value 1";
newRow.var_2 = "value 2";
ritm.update();
}

Not exactly, what I was looking for, but it works for me.

Cheers!

View solution in original post

4 REPLIES 4

Dylan Mann1
Giga Guru

This is a great post about scripting with MRVS by Brad Tilton. It's helped me and can probably help you out here:

https://community.servicenow.com/community?id=community_blog&sys_id=865b5aeddbc023c0feb1a851ca9619f9

Cheers!

Brad's article surely is a excellent start, and helped me to get to this point. Perhaps my question is more around: did anyone successfully use Service catalog script API to set a multi-row, i. e. using cart.setVariable?

Bernd Kallweit
Mega Expert

After some fiddling around, I came to a solution that deals with the multi-row after the order was placed. Something like:

var cart = new Cart();
var item = cart.addItem("my cat_item SID");
cart.setVariable(item, "normal_variable", "ABC");
var req = cart.placeOrder();
var ritm = new GlideRecord("sc_req_item");
if (ritm.get("request", req.getValue("sys_id"))) {
var mr = ritm.variables.variable_set_internal_name;
var newRow = mr.addRow();
newRow.var_1 = "value 1";
newRow.var_2 = "value 2";
ritm.update();
}

Not exactly, what I was looking for, but it works for me.

Cheers!

Constantine Kr1
Giga Guru

I was able to get this to set before the item was submitted through the CartJS api. This was done in the Rome release.

  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...