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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi MorriX

I've created a mrvs with name "mrvs" that has a field named "year" of type "single line text".

Created onLoad() script as follows.

function onLoad() {
    var today = new Date();
    var currYear = today.getFullYear();
    var objArr = [];
    for (var i = currYear; i > currYear - 4; i--) {
        objArr.push({
            year: i
        }); //replace 'year' with the name of the variable within the MRVS
    }
    g_form.setValue("mrvs", JSON.stringify(objArr)); //replace with the internal name of the MRVS

}

Execution result

find_real_file.png

Thanks Hitoshi. 

Hi Hitoshi:

The script since to work correctly but when I try to submit the catalog I get an error message and it won't allow me to submit the request.\

Thanks

MorriX1
Tera Contributor

@Brad Bowman Hello

I am trying to remove from this MRVS the "Remove All" and the Delete "X" options.  I have tried everything I found in the community, but it is not working for me.  Can you please help me with some of your magic?

Thanks