Multi-Row Variable Set: Dynamically populating fields descending order

MorriX1
Tera Contributor

How to dynamically set the first column to the current year (2022) and then when adding a new row to show (2021) and then the next row (2020) and so on for only five rows. 
It also has to work next year so that the first row will show (2023). 
Thanks

1 ACCEPTED SOLUTION

This works with the caveat that I'm not certain if the Date() function always returns the value in the same format.  If you get it to work for you, then it's not working for other users - due to User Preferences of time zone or date/time format - then you'll have to replace the first section with a GlideAjax call to a Script include to return the current year from the functions available on the server side for date/time manipulation.

Run this as a Catalog Client Script that Applies on a Catalog Item view only, against the Catalog Item - not within the MRVS:

function onLoad() {
	var today = new Date().toString();
     //alert(today);  //for me this returns Thu Jun 23 2022 ...
	var dteArr = today.split(' ');
	var curryear =dteArr[3];
	
	var objArr = [];
		for (var i=4; i>-1; i--) {
			objArr.push({year:  curryear-i}); //replace 'year' with the name of the variable within the MRVS
		}
		g_form.setValue("mrvs1", JSON.stringify(objArr)); //replace with the internal name of the MRVS
   
}

To ensure that no additional rows can be added, populate the Variable Set attributes field on the MRVS with:

max_rows=5

View solution in original post

13 REPLIES 13

Hi Brad,
When I request the catalog via the service portal, I am getting an error message that is due to the code.  What can I change to make it work? is there something in there that the SP doesn't like? 

This is the error (Something went wrong and your request could not be submitted. Please contact your system administrator).  When I inactivate the script, then I can submit the catalog request. 

Thanks

MorriX1
Tera Contributor

@Brad Bowman I noticed that to make it work, ALL the other fields within the MRVS have to be completed, which in my requirements it won't always be mandatory.  When I completed all the other fields, I was able to submit the request without any issues. 
Any advice, please?

Thanks

I noticed some browser console errors something about trying to populate an unknown value into a field that requires a string value, even though it appears fine in the MRVS.  I guess when you edit the row then make some changes it realizes it's a string value?  In any event, if you force the calculated year to a string like this then it will work - also put the variable name in quotes for good measure:

for (var i=4; i>-1; i--) {
	var yr = curryear-i;
	objArr.push({"year" : yr.toString()}); 
}

@Brad Bowman Thank you so much.  I appreciate your help with this. 

It worked.  You rock man.

Thanks again. 

You are welcome!