Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Display values on a reference field

mcroxall
Tera Guru

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? 


 

 

 

2 REPLIES 2

Not applicable

@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.

mcroxall
Tera Guru

i cant get to work... still displaying the department name, not the actual user... 
i am not sure i am doing this right