Populating response in MRVS using onSubmit client script

Ankita9793
Tera Contributor

Hi all,


I am getting the expected response but not able to populate/ set value in MRVS field, Please suggest 
based on the attachment 'employee id's will be populated in mrvs and onsubmit associated first and last name should be populate in the mrvs. 

Ankita9793_2-1766991419007.png

 

function onSubmit() {

    var type = g_form.getValue('request_type');
    var mode = g_form.getValue('request_mode');
    var xl = g_form.getValue('excel_input');
    var empID = g_form.getValue("employee_id");

    if (type != 'Delete' && mode == 'Multi User Request') {

        var multiRowVariableSet = JSON.parse(g_form.getValue('multiple_staff_id'));
 
        for (var i = 0; i < multiRowVariableSet.length; i++) {

            if (multiRowVariableSet) {

                alert('multiRowVariableSet.employee_id[i]' + multiRowVariableSet[i].employee_id);
                ShowHideLoaderCatalog();

                var getDepartment = new GlideAjax('abcRecordHelper');
                getDepartment.addParam('sysparm_name', 'getParametersHelper');
                getDepartment.addParam('sysparm_table_name', 'sys_user');
                getDepartment.addParam('sysparm_fields', 'employee_number,first_name,last_name');
                getDepartment.addParam('sysparm_filter_query', 'employee_number=' + multiRowVariableSet[i].employee_id);
                getDepartment.getXMLAnswer(function(response) {
 
                    if (!response) {
                        g_form.clearValue('employee_id');
                        g_form.clearValue('firstname');
                        g_form.clearValue('lastname');
                        // g_form.showFieldMsg('employee_id', getMessage('It is not a valid Employee ID'), 'error');
                        return false;
                    }
                    response = JSON.parse(response);
                    alert('response' + JSON.stringify(response));
                    alert('response[0]' + JSON.stringify(response[0]));
                   
                    var newres = JSON.stringify(response);

                    if (response[0]) {
                        g_form.setValue('multiple_staff_id[i].firstname', newres[0].first_name);
                        g_form.setValue('multiple_staff_id[i].lastname', newres[0].last_name);

                    } else {
                        g_form.clearValue('employee_id');
                        g_form.clearValue('firstname');
                        g_form.clearValue('lastname');
                        g_form.showFieldMsg('employee_id', getMessage('It is not a valid Employee ID'), 'error');
                    }
                    ShowHideLoaderCatalog('hide');
                });

            }
        }
    }
}

 Getting response/ response[0] as below
Ankita9793_0-1766991277566.png

 

 


 

3 REPLIES 3

Matthew_13
Kilo Sage

Hi there, this is a common MRVS gotcha..

Even if your GlideAjax response looks correct, the MRVS will only populate if you set the MRVS variable itself the multi-row set field and the value you pass is JSON in the MRVS row format array of objects keyed by the MRVS variable names.

Lets see please check:

  • Make sure you’re doing g_form.setValue('<MRVS_VARIABLE_NAME>', JSON.stringify(rows));
    not setting a field inside the MRVS

  • The JSON must be like:

    [
      {"u_employee_id":"123","u_first_name":"John"},
      {"u_employee_id":"456","u_first_name":"Jane"}
    ]

    Where the keys match the variable names inside the MRVS.

  • If you’re calling GlideAjax in a loop don’t set the MRVS inside each callback and async responses can overwrite each other. Build the rows array and set the MRVS once after all responses return.

Once I switched to setting the MRVS field directly and only updating it once at the end, it populated consistently.

 

@Ankita9793 - Please mark as Accepted Solution and Thumbs Up if you found Helpful!!


Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita9793 

it might be challenging to set the MRVS onSubmit as by the time you set the form might submit

why not use onChange catalog client script to populate the MRVS?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Ankita9793 

when you are returning value from script include pass the exact json string so that you can directly set it rather than parsing in client side

example: your json should be array of json objects like this

[

{

"first_name":"Abel",

"last_name":"Tuter",

"employee_id": "8978"

},

{

"first_name":"Beth",

"last_name":"Anglin",

"employee_id": "89895456"

}

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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