Auto Populate Department field on Catalog Item form

Bijay Kumar Sha
Giga Guru

Hi,

I've a catalog Item, where there is field called Caller which is a reference field to User table which is auto populate. I've created another text field besides that which should automatically reflect corresponding Department of the caller field. 

And again, if I change the caller field, then automatically Department field should be changed.

 

FYI - Department is a reference field in user table. 

For Example, my name is Bijay Kumar Sharma and if I open my Profile, then there is a reference field called 'Department'. If I open that department record, I'm seeing it's in 'cmn_department' table where there are different fields such as Name, ID, Description, Department Head etc

 

How to implement this?

1 ACCEPTED SOLUTION
4 REPLIES 4

Service_RNow
Mega Sage

HI @Bijay Kumar Sha 

Plz try to Glide Ajax to call a script include then return the user values as needed.

 

Create a script inclde get the data makesure Client callable is true.
var loaner = Class.create();
loaner.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getdpt: function() {
        var obj = {};
        var sysid = this.getParameter('sysparm_usrsysid');
        var grusr = new GlideRecord("sys_user");
        grusr.addQuery('sys_id', sysid);
        grusr.query();
        if (grusr.next()) {
			
            obj.department = grusr.getValue('department');
        }
        return JSON.stringify(obj);
    },


    type: 'loaner'
});
On change client script:-
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var name = g_form.getValue('requested_for');
    var user = new GlideAjax('global.loaner');
    user.addParam('sysparm_name', 'getdpt');
    user.addParam('sysparm_usrsysid', name);
    user.getXML(getData);
}

function getData(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var data = JSON.parse(answer);
    g_form.setValue('department', data.department);

}
 

 Please mark reply as Helpful/Correct, if applicable. Thanks!

Dr Atul - LNG
Tera Patron

Hi @Bijay Kumar Sha 

 

Use Auto populate on variable and by dot walk it will work for you.