Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add rows to multi row variable set on load

Eric Manning
Tera Contributor

I have a request item with a multi row variable set containing 2 variables. What I would like to see is that when the Request is opened that this variable set is pre-populated with two rows that the user can Edit or Add to. I tried g_form.setValue('var_set_name',[{"var1":"value1","var2":"value2"}]); in an onLoad Catalog Client Script applying to the Variable Set (and also tried the item). I suspect I am missing something like initialize() a variable row and then setValue() but I haven't found anything like this and I'm hoping this is easier than I am thinking.

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

See below working example (tested in Madrid). It's about a Catalog Client Script on your Catalog Item (not on the MRVS!).

find_real_file.png

find_real_file.png

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

Note: setValue ==> MRVS name, in my case I named the MRVS "mrvs_onload_setvalue"

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

View solution in original post

4 REPLIES 4

Supriya28
Kilo Guru

Hi Eric

To prepopulate your variable set with a couple of rows, you could use the following OnLoad catalog client script, applying to the catalog item (not the variable set). In the below example, the variable set I have used is 'new_set' which has the variables 'var_1' and 'var_2'.

 

function onLoad() {

var arr = [];
arr.push({
var_1: "abc",
var_2: "123"
},
{
var_1: "xyz",
var_2: "456"
});
g_form.setValue('new_set',JSON.stringify(arr));
}

 

Please mark this answer CORRECT/HELPFUL if it solved your query.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

See below working example (tested in Madrid). It's about a Catalog Client Script on your Catalog Item (not on the MRVS!).

find_real_file.png

find_real_file.png

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

Note: setValue ==> MRVS name, in my case I named the MRVS "mrvs_onload_setvalue"

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi Mark,

How to addrows from a table?

Thanks,
Diane

Hello Mark,

 

How to setValue if the field is reference type field?

Like suppose onload I am trying to pull logged in user details like company, country, manager.

and 

Does the above script for on change user scenario

Thanks in advance.