Add row onLoad to Multi-Row Variable Set - ServiceNow Community
Mark Roethof
Tera Patron
Tera Patron

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Hi there,

 

Commonly questions are asked on the Community about scripting around Multi-Row Variable Set. One of those questions which I've answered several times, "How to add a row, on load, to the Multi-Row Variable Set?"


As mentioned, myself (and others) have answered some topics on this already, so not entirely new. Though I'll try to give some additional information in this short article.

 

Note: While this article is about onLoad, this will also perfectly work if you have the desire to fill the Multi-Row Variable Set onChange. For example onChange of a certain Select Box value.

Result

Let's just start with the end result which we are trying to achieve. When a user is displaying a Catalog Item on the Service Portal, immediately a row should be added to the Multi-Row Variable Set.

 

find_real_file.png

Script options

Looking at scripting, we only have two possibilities here:
1) Catalog UI Policy
2) Catalog Client Script

Both can be used for onLoad purposes. A Catalog UI Policy without conditions, the "On load" checkbox checked and using the "Execute if true" script field. A Catalog Client Script for "Type" "onLoad", and using the "Script" field. We'll be using a Catalog Client Script for this example.

 

Note: It should be a Catalog Client Script on the Catalog Item. Not a Catalog Client Script on the Multi-Row Variable Set!

Catalog Client Script

Looking at how the Multi-Row Variable Set values are actually stored, it's a JSON Object. So through scripting we need to do perform g_form.setValue() with a JSON formatted object. The rows within the Multi-Row Variable Set JSON should contain an actions key with a blanc value, and the variable key with the variable value.

 

Knowing this, we could set up an onLoad Catalog Client Script like:

 

function onLoad() {
	
	var rowObj = [];
	rowObj.push({
		"actions": "",
		"variable_name": "Does this work?"
	});
	
	g_form.setValue('mrvs_internal_name', JSON.stringify(rowObj));
   
}

 

Note: Within the g_form.setValue(), the "Internal name" [internal_name] of the Multi-Row Variable Set should be used!

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

 

C

If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.

 

Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in?
- Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

 

Kind regards,


Mark Roethof

ServiceNow Technical Consultant @ Paphos Group
---

LinkedIn

Comments
Simm
Tera Contributor

var rowObj = [];

rowObj.push({

"actions": "",

"variable_name": "Does this work?"

});

g_form.setValue('mrvs_internal_name', JSON.stringify(rowObj));

 

 

// CORRECTION , this Code really works but this should be titled as

// Set Multi-Row Variable set  on onLoad 

//  not

// Add row onLoad to Multi-Row Variable Set

// this code does not append or add a row, it sets the entire MRSV

 

Abdul Khader
Mega Sage

Hi i have to insert a empty row in mrvs when a catalog item is selected.

AbdulKhader_0-1668498798784.png

 

Version history
Last update:
‎08-31-2024 12:14 AM
Updated by:
Contributors