
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 11:32 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2022 02:06 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2022 01:26 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 05:02 AM
Any advice, please?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 06:36 AM
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()});
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 06:45 AM
It worked. You rock man.
Thanks again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2022 06:59 AM
You are welcome!