Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Populate Department based on business unit

Rahil Mirza
Tera Contributor

If a business unit is selected only the departments related to that business unit should be visible in the department reference field, both business unit and department fields are reference type fields, I have written a script include for the advanced reference qualifier, but it is not working 

Script Include

Name: PopulateDepartment
Accessible from: All application scopes
Client callable: No
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var PopulateDepartment = Class.create();
PopulateDepartment.prototype = {
    initialize: function() {},

    getDepartment: function(businessUnitId) {
        var departmentList = [];
        if (businessUnitId) {
            var gr = new GlideRecord('cmn_department'); 
            gr.addQuery('business_unit', businessUnitId);
            gr.addQuery('u_active', true);
            gr.query();
            while (gr.next()) {
                departmentList.push(gr.sys_id.toString());
            }
        }
        return departmentList.join(',');
    },

    type: 'PopulateDepartment'
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reference Qualifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
javascript:new PopulateDepartment().getDepartment(current.u_sbu_ssu) + '^u_active!=false';

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Change your return line to

 

 return 'sys_idIN' + departmentList.join(',');

 

and you don't really need the concatenated part on the Reference Qualifier, since your GlideRecord includes that.  

 

You could also do this without a Script Include by using a Reference Qualifier that looks more like this:

javascript: 'u_active!=false^business_unit=' + current.u_sbu_ssu;

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Change your return line to

 

 return 'sys_idIN' + departmentList.join(',');

 

and you don't really need the concatenated part on the Reference Qualifier, since your GlideRecord includes that.  

 

You could also do this without a Script Include by using a Reference Qualifier that looks more like this:

javascript: 'u_active!=false^business_unit=' + current.u_sbu_ssu;

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Rahil Mirza 

no script include required.

Simply use this in advanced ref qualifier

javascript: 'business_unitIN' + current.u_sbu_ssu + '^u_active=true';

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Rahil Mirza 

Did you get a chance to check my above comment?

That should also work and no scripting required.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rahil Mirza
Tera Contributor

Thank you @Brad Bowman  it works wonders!!!