Departent field is not populating

Shaik22
Tera Expert

Hi,

 

Requested by details are in variable set, created department as seperate variable. i want to populate department based on requested by. i wrote script include and onload CS. i'm not getting where i did mistake. Please help me.

Script include:

getDepartmentDetail: function() {
        var userSysId = this.getParameter('sysparm_user');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userSysId)) {
            var departmentSysId = userGR.department; 
            var departmentGR = new GlideRecord('cmn_department'); 
            if (departmentGR.get(departmentSysId)) {
                return JSON.stringify({
                    dept_info: departmentGR.getDisplayValue()
                });
            }
        }
        return JSON.stringify({dept_info: '' });
    }
onLoad CS:
function onLoad() {
   
    var req = g_form.getValue('requested_by');
    var ga = new GlideAjax("autopopulatedepartment");
    ga.addParam("sysparm_name", "getDepartmentDetail");
    ga.addParam("sysparm_user", req);
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response); // Parse the JSON string to an object
        if (result.dept_info) {
            // Assuming 'department1' is a reference field
            g_form.setValue('department1', req); // Set the sys_id (or you may need to adjust if setting a different format)
           g_form.setDisplayValue('department1', result.dept_info); // Set the display value
        }
    });
2 REPLIES 2

Slava Savitsky
Giga Sage

First of all, when you post your code, make sure to format it as code using the corresponding toolbar icon. Otherwise, your code is very difficult to read.

 

Secondly, there are lots of things that can go wrong in your code. You need to learn to debug your code. Have you done any debugging at all before reaching out to the Community?

 

Finally, the most obvious reason why your code doesn't work is that you are using the sys_id of the Requestor as the value for the Department reference in this line:

g_form.setValue('department1', req);

 

ShubhamGarg
Kilo Sage

Hello @Shaik22 

Are you trying to autopopulate department on selection of requested by? -> Use onChange Client Script & Script Include
Or Are both requested by and department getting populated basis on current logged in user? -> onLoad CS & script include?
Seeing your code, it seems you have over-complicated the code. Instead, you can simply define your code and use. For example -

getDepartmentDetail: function() {
var userSysId = this.getParameter('sysparm_user');
var userGR = new GlideRecord('sys_user');
if (userGR.get(userSysId)) {
var departmentSysId = userGR.department;
}
return departmentSysId;
}

It gives you sys_id of department field. If your variable is already referencing department table (reference type) and you call this function as deafult value, that can also help.
Similarly, you can use the same function as a reference qualifier to limit the selection (if possibility to select new department value.)

 

Hope it helps.

Regards,

Shubham