Add Rows to Multi-row Variable Set in Client Script

Anthony Sheculs
Kilo Expert

Hi all,

I am trying to to add rows to a Multi-row Variable Set in a Catalog Item from a UI Page client script before submission of the form.

The GlideForm setValue method will only set the first row in the Multi-row variable set and will overwrite it if it is called more than once.

function continueOK(){
	
	var selectbox = document.getElementById("quick_message_dropdown").value;
	var slushbucket = slush.getValues(slush.getRightSelect());
	
		
 	 var obj = [{
 	     "primary": selectbox,
 	     "sub": slushbucket
 		   }];
 

	g_form.setValue("varset", JSON.stringify(obj));
}


 

Iterating over the obj variable will allow me to add multiple rows, but will overwrite every row in the Multi-Row Variable Set. Also, setting the values from an array is not easy to work with, since the array is lengthened each time the function is called. This means that if I delete a row in the Multi-row variable set, but call the function again, the deleted row will be re-added. Ideally, the GlideForm setValue method could work to add a row instead of overwriting the first row.

 

//Global Variable
var selectedArray = [];

//Called when the 'OK' button gets clicked
function continueOK(){
   //Get the selected values from the right slushbucket
	
	var selectbox = document.getElementById("quick_message_dropdown").value;
    var slushbucket = slush.getValues(slush.getRightSelect());
	
	//Push to global variable selectedArray

	selectedArray.push([selectbox,slushbucket]);
	
  var obj = [];
  for (var i = 0; i < selectedArray.length; i++){
    obj.push({
      "selectbox": selectedArray[i][0],
      "slushbucket": selectedArray[i][1]
    });
  }

  g_form.setValue("varset", JSON.stringify(obj));

 

I have read the ServiceNow documentation on "Scriptable service catalog variables", Brad Tilton's blog post on "Scripting the Multi-row Variable Set", and hrng's article on "Accessing the Multi-row Variable Set" but can't seem to reference the variable set this way from a client script on an unsubmitted form. 

 

current.variables.<variable_name>

current.variables.<variable_set_name>.<variable_name>

 

Any help with this would be greatly appreciated!!

 

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Here is a link to my video where I show you how to add rows with client script and also how to preserve the rows that already might be there.

https://youtu.be/JZ341t0iVtY

//Göran

Feel free to connect with me:
 

View solution in original post

10 REPLIES 10

Andrew Barnes -
ServiceNow Employee
ServiceNow Employee

Greetings Anthony,

 I am unaware of a method to set these via client script at this time. I am interested if someone has found a way as well.

It's possible, Let me make a quick video after dinner 😃

Jace Benson
Mega Sage

Can you make a simple ui page that we can import and test your stuff?  I'm sure like Goran suggests, this is possible.  Really it would be a replacement of the current value but would act like a add row function.

I'm not sure I can...

The UI Page and Multi-row variable set are both catalog item variables. The UI Page has a select box and slushbucket populated by a gliderecord query. The user is supposed to make a few selections and press OK which calls the continueOK() function. The selected values should then get set on the multi-row variable set so that they are submitted with the form. Otherwise the UI Page values don't get submitted.

 If it's possible to add a multi-row variable set to a UI Page, I can't find the Jelly script to do it! This would make testing easier for sure though.