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

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:
 

Incredible!!! THANK YOU. This code works perfectly.

Before you answered, I tried to use do the same by calling g_form.getValue at the end of the function. It wouldn't work but making it part of the obj variable works like a charm.

I've followed your GitHub and subscribed to your Youtube account. You are the best!

Göran, 

I'm hoping you can assist with an anomoly I'm encountering with multi-row variable sets. I have a Catalog Item with an MRVS which contains 3 variables - Part Number, Part Material, and Part Description...in that order. 

 

When the request is submitted, the RITM requires 1 approval, and after this approval, a workflow script executes which parses through the rows of the MRVS and creates a separate RITM for each part. The original MRVS in the original RITM is overwritten with the part information in position 0. 

This is all working as expected. The anamoly is the order in which the 3 variables appear within the MRVS. In the Variable Set itself, they are ordered Part Number, Part Material, Part Description. In performing some tests, their order appears correct until it is approved (once approved, the MRVS gets overwritten with only the first row). Afterward the order seems to be random. The order of the variables in the subsequent RITMs seem random as well. 

I've attached some screenshots for clarification. Any suggestions are appreciated. 

 

Submission Form: (Order is Correct)

find_real_file.png

RITM form prior to approval (order is correct

find_real_file.png

 

Original RITM after approval (Order changed)

find_real_file.png

 

2nd RITM generated by script (order is different)

find_real_file.png

3rd RITM generated by script (order is different than previous)

find_real_file.png

Hi Patrick,

Hmm. That is strange, I have never changed the data after the RITM been approved as you do, but I can't see why it shouldn't work. I think I also would need the see more precisely how the script etc. looks like to create the other RITMs. It feels like the issue might lay in there somewhere.

To troubleshoot I would first see if the order is random if you don't do anything with the original RITMs MRVS. Is there any reason why you, e.g. can't just keep it as one RITM and use catalog tasks to handle the different parts. 

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor’s Guide To ServiceNow

We split them into different RITMs because each part may be fulfilled by a different team. If we only split them into different catalog tasks I'm not sure how I would only display one part number per catalog task since the variable would still contain all of them. 

I've attached the workflow script that splits the mrvs into new RITMs.