Scheduled job for a catalog task, how to populate MRVS?

jonsr20
Tera Expert

Hey everyone,

 

I created a scheduled job to run once a week to create a catalog item automatically for weekly task. I am able to get the script to populate the variables, but I also have a MRVS in the catalog item I would like to populate. Below is my script and attached a picture of the MRVS details. Can anyone help with what I need to add for the MRVS to populate those fields? Thank you!

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('a74ee200c32682903e0637af0501312f'); // Put the sys_id of the catalog item that you want to raise the request

// Set other variables of your catalog item accordingly
cart.setVariable(item, 'requested_for', '1c0c47ef971b85108d663fe3f153afc7');//it is reference field
cart.setVariable(item, 'other_task', 'true');
cart.setVariable(item, 'task_description', 'Perform Audit on all LCM Stockrooms');


 

var rc = cart.placeOrder();
gs.info(rc.number);
1 ACCEPTED SOLUTION

Hi @jonsr20,

 

No worries, it looks like you can't set an MRVS via Cart API.

You can update the MRVS after creating the request - https://www.servicenow.com/community/now-platform-forum/set-multi-row-variable-set-using-cart-api/m-...

 

But there is another workaround using CartJS API which did its job- https://www.servicenow.com/community/now-platform-articles/cartjs-how-to-populate-a-mrvs-multi-row-v...

 

So, for your example, it will be something like:

var mrvsObj = [{
        "details": "Test 123",
        "audit_stockroom": "102eb8c4c300f1103e0637af05013104",
        "completion_date": "2024-05-30 11:11:46 PM"
    },
    {
        "details": "Test 123456",
        "audit_stockroom": "d82eb8c4c300f1103e0637af05013103",
        "completion_date": "2024-05-30 11:31:46 PM"
    }
];

var cart = new sn_sc.CartJS();
var item = {
    'sysparm_id': 'a74ee200c32682903e0637af0501312f',
    'sysparm_quantity': '1',
    'variables': {
		'requested_for' : '1c0c47ef971b85108d663fe3f153afc7', //add the remaining variables here
		
        'stockroom_audit': JSON.stringify(mrvsObj)
	}
};

var cartDetails = cart.addToCart(item);
var checkoutInfo = cart.checkoutCart();

Make sure you double check the date/time format as it may needs some adjustment

 

Cheers

View solution in original post

6 REPLIES 6

Saloni Suthar
Mega Sage
Mega Sage

Hi @jonsr20 ,

 

Please refer to the community article for the script reference. Your script will be different but you will get an idea on how to auto populate your MRVS variable. 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

I seen that article, but the scenario looks different than what I am trying to do, I am also pretty new with writing script. Any additional details would be helpful, I would like to populate the same variables from this job every week.

James Chun
Kilo Patron

Hi @jonsr20,

 

I believe you will need to create an array of objects, something like the following:

 

var mrvsObj = [{
        "details": "Some details here",
        "audit_stockroom": "sys_id of reference record",
        "completion_date": "some date"
    },
    {
        "details": "Some details here",
        "audit_stockroom": "sys_id of reference record",
        "completion_date": "some date"
    }
];

cart.setVariable(item, 'stockroom_audit', mrvsObj);

Adjust the object as per your need.

 

Cheers

Thank you, 

I gave it a try but still appears to not populate the items in the MRVS. I have attached a picture of what it looks like, I feel like I am close any idea what is missing? I inserted your lines into the script like this.

 

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('a74ee200c32682903e0637af0501312f'); // Put the sys_id of the catalog item that you want to raise the request
var mrvsObj = [{
        "details": "Test 123",
        "audit_stockroom": "102eb8c4c300f1103e0637af05013104",
        "completion_date": "2024-05-30 11:11:46 PM"
    },
    {
        "details": "Test 123456",
        "audit_stockroom": "d82eb8c4c300f1103e0637af05013103",
        "completion_date": "2024-05-30 11:31:46 PM"
    }
];



// Set other variables of your catalog item accordingly
cart.setVariable(item, 'requested_for', '1c0c47ef971b85108d663fe3f153afc7');//it is reference field 
cart.setVariable(item, 'lcm_audit', 'true');
cart.setVariable(item, 'stock_audit', 'true');
cart.setVariable(item, 'task_description', 'Perform Audit on all LCM Stockrooms, use the above Stockroom Audit form and complete details of your weekly audit.');
cart.setVariable(item, 'stockroom_audit', mrvsObj);



// Query the sys_user table to get the sys_id of the requester


var rc = cart.placeOrder();
gs.info(rc.number);​


Really appreciate any insight and thanks again!!