Assigning values to multi-row variable set via transform script

jonw1
Mega Expert

I'm writing this transform script to take a .csv data source which includes data in from 1 to 12 pairs of columns, and map to a multi-row variable set on a catalog item using the Cart API.  The data's general format is ...

ColA, ColB, ColC, ColD1, ColE1, ColD2, ColE2, ColD3, ColE3, ColD4, ColE4, ... ColD12, ColE12

So, we always receive Columns A, B, C, D1 and E1  ... optionally, if there is a D2, there will be an E2, D3/E3, etc.  (the D1/E1 through D12/E12 combinations are my multi-row variable set) 

I'm a rookie scripter and, while I can map an individual data element to a variable (see below), I don't know how to handle the multi-row variable.

example snippet from transform script:

var item = cart.addItem('*** sys_id goes here ***');

  cart.setVariable(item, 'u_item_1', source.u_item_1);

 

If anyone has experience with this, I would appreciate your assistance!   Thanks,  - Jon

4 REPLIES 4

Manish Vinayak1
Tera Guru

Hi Jonw,

 

The values in MRVS get stored as JSON String internally. So, if you want to set values in an MRVS , you need to construct the JSON string with the values.

E.g.

 

Let's say there is an MRVS with the following variables:

Name (name)

Surname (surname)

 

And I want to add the following values:

1. Name: Manish

   Surname: Vinayak

2. Name: Bernard

   Surname: Angeles

 

Then I need to create an array of JSON objects, which then needs to be converted to JSON string. In this case, my array would be:

var nameArr = [];

nameArr.push({"name" : "Manish", "surname" : "Vinayak"});

nameArr.push({"name" : "Bernard", "surname" : "Angeles"});

var nameJSONString = JSON.stringify(nameArr);

var item = cart.addItem('*** sys_id goes here ***');

//Where mrvs is the internal name of the MRVS

cart.setVariable(item, 'mrvs', nameJSONString));

 

So, You can create array of objects from the available values in your transform script, then transform it to JSON string to assign it to the MRVS.

 

Hope this helps!

Cheers,

Manish

 

Hi Manish,

Your instructions are really helpful.
I have done exactly same and it worked - it set our RITM to the required status (closed, we do some automation).
However, when opening the RITM - the MRVS is not populated, marked with red asterisk as not set.
Question: how to make MRVS filled/visible in RITM?

Thanks,

Serg

Jyoti8
Kilo Guru

Hi jonw,

 

This might help you.

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

 

Mark it as Correct if this solves your issue and also mark Helpful if you find my response worthy.

Thanks & Regards

Jyoti

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