Populating response in MRVS using onSubmit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
