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

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

That worked, thanks James you are awesome!

 

 

Jonathan