Populate Variable in MRV from Reference variable in MRV
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 08:07 AM
Let's say I have multi row variable set "user_info". The first variable in this MRV is "staff_member" which is a reference to the user table. The next variable is "employee_id" which is a string. How can I auto populate the employee ID variable on change when the user reference field is populated?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 08:15 AM
You can write onchange catalog client script on staff member variable and can call script inlcude to get the emp ID, Below is the client script and script include:
Client script:
// Get the value of the staff_member reference field
var staffMember = g_form.getValue('staff_member');
if (staffMember) {
// Make a GlideAjax call to get the employee ID associated with the selected user
var ga = new GlideAjax('GetEmployeeIDScript');
ga.addParam('sysparm_name', 'getEmployeeID');
ga.addParam('sysparm_user_id', staffMember);
ga.getXMLAnswer(function(response) {
// Populate the employee_id field with the retrieved employee ID
g_form.setValue('employee_id', response);
});
} else {
// If the staff_member field is empty, clear the employee_id field
g_form.setValue('employee_id', '');
}
}
Script Include:
getEmployeeID: function() {
// Get the user ID parameter passed from the client script
var userID = this.getParameter('sysparm_user_id');
// Check if the user ID is provided
if (userID) {
var userGr = new GlideRecord('sys_user');
if (userGr.get(userID)) {
// If user found, retrieve the employee ID field value
var employeeID = userGr.getValue('employee_id');
// Return the employee ID to the client script
return employeeID;
} else {
// If user not found, return an empty string
return '';
}
} else {
// If user ID parameter not provided, return an empty string
return '';
}
},
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 09:12 AM
The staff member reference variable in the MRV is not available to choose for a catalog client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 09:28 AM
@Nic Omaha, you can use the auto-populate function to achieve this with no scripting.
Open the Employee ID variable.
Click on the Auto-populate tab.
Select "Staff Member" as the Dependent Question value.
Select the "User ID" field as the Dot walk path value.