Display values on a reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2024 09:38 AM
I created a field on my RITM called Head Department. I would like for my end user to be able to reference the cmn_department table and see all department, however when they select one of them, it should display the actual Head of department no the department. How do i configure that without having to change the display option in the cmn_department dictionary?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2024 10:23 AM
@mcroxall There might be other way but what I have understood I will go with below option:
This client script will dynamically update the display value of the reference field based on the selected department’s Head of Department:
(function executeClientScript(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// GlideAjax call to get the Head of Department name
var ga = new GlideAjax('DepartmentUtils');
ga.addParam('sysparm_name', 'getDepartmentHead');
ga.addParam('sysparm_department_id', newValue);
ga.getXMLAnswer(function(response) {
var headOfDepartment = response;
if (headOfDepartment) {
g_form.setDisplayValue('u_head_department', headOfDepartment);
}
});
})();
The client script makes a GlideAjax call to fetch the Head of Department. You need a Script Include that handles this request.
var DepartmentUtils = Class.create();
DepartmentUtils.prototype = {
initialize: function () {},
getDepartmentHead: function () {
var departmentId = this.getParameter('sysparm_department_id');
var dept = new GlideRecord('cmn_department');
if (dept.get(departmentId)) {
var headOfDepartment = dept.department_head.name;
return headOfDepartment ? headOfDepartment : 'No Head Assigned';
}
return 'Department Not Found';
},
type: 'DepartmentUtils'
};
Please feel free to modify code if works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2024 11:53 AM