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

Anil Lande
Kilo Patron

Hi,

Please check below links:

https://www.servicenow.com/community/developer-articles/auto-populate-reference-data-in-service-cata...

 

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

4 REPLIES 4

Anil Lande
Kilo Patron

Hi,

Please check below links:

https://www.servicenow.com/community/developer-articles/auto-populate-reference-data-in-service-cata...

 

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

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 G- LNG
Tera Patron
Tera Patron

Hi @Bijay Kumar Sha 

 

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

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Hi @Bijay Kumar Sha 

LearnNGrowAtul_0-1705581511161.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************