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

 

 


 

5 REPLIES 5

Brad Bowman
Kilo Patron

You could push the Script Include responses to an object in the Catalog Client Script, then set the value of the MRVS once it's done, but a better approach is for your Catalog Client Script to look more like this to call the GlideAjax only once, passing in the value of the MRVS.  This ensures the client script runs efficiently, removing unnecessary trips to the server and back.

function onSubmit() {

    var type = g_form.getValue('request_type');
    var mode = g_form.getValue('request_mode');

    if (type != 'Delete' && mode == 'Multi User Request') {
        var multiRowVariableSet = g_form.getValue('multiple_staff_id');
        if (multiRowVariableSet.length > 2) { //native UI returns [] for empty MRVS value
            ShowHideLoaderCatalog();

            var getDepartment = new GlideAjax('abcRecordHelper');
            getDepartment.addParam('sysparm_name', 'getParametersHelper');
            getDepartment.addParam('sysparm_mrvs', multiRowVariableSet);
            getDepartment.getXMLAnswer(function(response) {
                if (response.indexOf('error') != -1) {
                    g_form.setValue('multiple_staff_id', response);
                    alert('Atachment contains one or more invalid Employee IDs');
                    // g_form.showFieldMsg('multiple_staff_id', getMessage('Attachment contains one or more invalid Employee IDs'), 'error');
                    return false;
                } else {
                    g_form.setValue('multiple_staff_id', response);
                }
                ShowHideLoaderCatalog('hide');
            });
        }
    }
}

You'll need to change your Script Include Function to parse and loop through the MRVS rows, building a new object with the values from the GlideRecord results.  You can try to make it dynamic as you did before, also passing in the table name, fields, and filter with the MRVS value, but that probably isn't as useful in this specialized situation.

var abcRecordHelper = Class.create(); 
abcRecordHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getParametersHelper: function() { 
        var mrvs = this.getParameter('sysparm_mrvs');
		var obj = JSON.parse(mrvs);
		var rowObj = [];
		for (var i = 0; i < obj.length; i++) {
			var usr = new GlideRecord('sys_user');
        	usr.addQuery('employee_number', obj[i].employee_id);
        	usr.query();
        	if(usr.next()) {
            	rowObj.push({
                	"employee_id": obj[i].employee_id,
                	"firstname": usr.getValue("first_name"),
					"lastname": usr.getValue("last_name")
	            });
			} else {
				rowObj.push({
                	"employee_id": obj[i].employee_id,
                	"firstname": "error",
					"lastname": "error"
	            });
			}
        }

	    return JSON.stringify(rowObj);
        
    },
    type: 'abcRecordHelper' 
});

You can instead run this in the process that is populating the employee ids in the MRVS when an attachment is added for a better user experience.