In the Multi Row Question Answer, you cannot set the value for the Date/Time and Date type Question

tyzha
Tera Contributor

 

 

It is used by record producers.

Attach a screenshot of your browser console.
In the Multi Row Question Answer, you cannot set the value for the Date/Time and Date type Questions.

The values ​​can be set correctly for other types of Questions.

 

The following is a catalog client script。

function onLoad() {
    //Type appropriate comment here, and begin script below
    (function() {
        var variableSetName = "kazokujyouho";

       
        var rowDataArray = [{
                "46e6ee7333486210c39d9b4a7e5c7b6e": "test001",
                "4a7726f333486210c39d9b4a7e5c7b82": "手入力A",
                "5979ae7733486210c39d9b4a7e5c7b1e": "1",
                "83982e3733486210c39d9b4a7e5c7b2e": "false",
                "8ebdf03633182e10c39d9b4a7e5c7bd7": "1990-10-10",
                "8738a63733486210c39d9b4a7e5c7b9a": ""

            },
            {
                "46e6ee7333486210c39d9b4a7e5c7b6e": "test002",
                "4a7726f333486210c39d9b4a7e5c7b82": "手入力B",
                "8ebdf03633182e10c39d9b4a7e5c7bd7": "2025-04-15",
                "5979ae7733486210c39d9b4a7e5c7b1e": "0",
                "83982e3733486210c39d9b4a7e5c7b2e": "true",
                "8738a63733486210c39d9b4a7e5c7b9a": "49f489103b50e2106df3a2f726e45a46"

            }
        ];

       
        console.log("Setting MRVS Data:", JSON.stringify(rowDataArray, null, 2));
        g_form.setValue(variableSetName, JSON.stringify(rowDataArray));


    })();

}
#Service Portal Development
#itsm
#Variable Set
#Multi Row Question Answer
#Multi Row
3 REPLIES 3

-O-
Kilo Patron
Kilo Patron

It should work - it works in Xanadu for me, but it should work in any version.

To get the data or date/time string that will be used in the MRVS, one should use function formatDate and global variables g_user_date_time_format and g_user_date_format.

E.g. I have created a Catalog Item that contains a Variable Set internal-named tests.

The Variable Set contains variables a_user (type reference), current_data_and_time (type date/time) and current_date (type date/time).

I have added to the Catalog Item an onLoad Catalog Client Script with the following Script:

function onLoad() {
	var aDateTime = new Date(2010, 5, 27, 13, 56, 45);
	var glideDateTime = formatDate(aDateTime, g_user_date_time_format);
	var aDate = new Date(2013, 11, 25);
	var glideDate = formatDate(aDate, g_user_date_format);

	var value = [
		{
			"a_user": "a8f98bb0eb32010045e1a5115206fe3a",
			"current_data_and_time": glideDateTime,
			"current_date": glideDate,
		}
	];

	g_form.setValue('tests', JSON.stringify(value));
}

The result is that when I open the Catalog Item, I am presented the screen:

1.png

 Remember months start with index 0 in case of native JavaScript dates, so 11 = December and 5 = June.

I have also added an onLoad Catalog Client Script to the MRVS itself:

function onLoad() {
	var aDate = new Date();
	var glideDateTime = formatDate(aDate, g_user_date_time_format);
	var glideDate = formatDate(aDate, g_user_date_format);

	g_form.setValue("a_user", g_user.userID);
	g_form.setValue("current_data_and_time", glideDateTime);
	g_form.setValue("current_date", glideDate);
}

Therefor when I press the "Add" button on the MRVS I get:

2.png

The idea is that on client side, when setting a Date or a Date/Time, in a Catalog Item or in a Form, one needs to set a string that is using the format chosen by the current user in its profile (and - of course - the Time Zone the current user has selected for itself in its profile).

Ankur Bawiskar
Tera Patron
Tera Patron

@tyzha 

seems issue with the date format of the logged in user who is submitting the form

the date in the JSON needs to be converted into the logged in user's date format and then set it

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@tyzha 

try this

function onLoad() {
    // Type appropriate comment here, and begin script below
    (function() {
        var variableSetName = "kazokujyouho";

        var rowDataArray = [{
                "46e6ee7333486210c39d9b4a7e5c7b6e": "test001",
                "4a7726f333486210c39d9b4a7e5c7b82": "手入力A",
                "5979ae7733486210c39d9b4a7e5c7b1e": "1",
                "83982e3733486210c39d9b4a7e5c7b2e": "false",
                "8ebdf03633182e10c39d9b4a7e5c7bd7": "1990-10-10",
                "8738a63733486210c39d9b4a7e5c7b9a": ""
            },
            {
                "46e6ee7333486210c39d9b4a7e5c7b6e": "test002",
                "4a7726f333486210c39d9b4a7e5c7b82": "手入力B",
                "8ebdf03633182e10c39d9b4a7e5c7bd7": "2025-04-15",
                "5979ae7733486210c39d9b4a7e5c7b1e": "0",
                "83982e3733486210c39d9b4a7e5c7b2e": "true",
                "8738a63733486210c39d9b4a7e5c7b9a": "49f489103b50e2106df3a2f726e45a46"
            }
        ];

        // Function to format the date according to the user's date format
        function formatDate(date, format) {
            var day = date.getDate();
            var month = date.getMonth() + 1; // Months are zero-based
            var year = date.getFullYear();

            // Replace format tokens with actual date values
            format = format.replace('dd', ('0' + day).slice(-2));
            format = format.replace('MM', ('0' + month).slice(-2));
            format = format.replace('yyyy', year);

            return format;
        }

        // Get the user's date format (assuming it's stored in a variable or can be retrieved)
        var userDateFormat = g_user_date_format; // This should be the user's date format, e.g., 'dd/MM/yyyy'

        // Iterate over the rowDataArray and format the dates
        rowDataArray.forEach(function(row) {
            if (row["8ebdf03633182e10c39d9b4a7e5c7bd7"]) {
                var dateObj = new Date(row["8ebdf03633182e10c39d9b4a7e5c7bd7"]);
                row["8ebdf03633182e10c39d9b4a7e5c7bd7"] = formatDate(dateObj, userDateFormat);
            }
        });

        console.log("Setting MRVS Data:" +  JSON.stringify(rowDataArray));
        g_form.setValue(variableSetName, JSON.stringify(rowDataArray));
    })();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader