How to return a list of departments using a Script Include

matthew_hughes
Kilo Sage

I've got a reference field on my Business Application form that gets its entries from the 'cmn_department' table:

matthew_hughes_0-1733216650779.png

 

I'm trying to create a script include that will return the following:

  • Active Platforms (Department Type = Platform)
  • Active Children of Platforms where:
    • If an active platform has no children with a Department Type of 'Lab' then the child departments should be displayed and selectable.
    • If an active platform has any children with a Department Type of 'Lab' then ONLY the child departments with a Department Type of 'Lab' should be displayed and selectable

I've tried to write a script include that will get the required results. The code in my script include is:

function LBGfilterDepartmentsAPM(department) {

    var returnString = [];

    var departmentRecord = new GlideRecord('cmn_department');
    departmentRecord.addEncodedQuery('u_active=true^u_department_type=Platform');
    departmentRecord.query();
    while (departmentRecord.next()) {
        returnString.push(departmentRecord.toString());
    }


    var departmentChild = new GlideRecord('cmn_department');
    departmentChild.addQuery('parent', department);
    departmentChild.addQuery('parent.u_department_type', 'Platform');
    departmentChild.addQuery('parent.u_active', true);
    departmentChild.query();
    gs.log('Number of children are ' + departmentChild.getRowCount());
    if (departmentChild.getRowCount() >= 1) {
        while (departmentChild.next()) {
            var departmentLab = new GlideRecord('cmn_department');
            departmentLab.addQuery('u_active', true);
            departmentLab.addQuery('parent', department);
            departmentLab.addQuery('u_department_type', 'Lab');
            departmentLab.query();
            gs.log("Number of child Lab departments are " + departmentLab.getRowCount());

            if (departmentLab.getRowCount() > 0) {
                while (departmentLab.next()) {
                    returnString.push(departmentLab.toString());
                }
            }
            var departmentPlat = new GlideRecord('cmn_department');
            departmentPlat.addQuery('u_active', true);
            departmentPlat.addQuery('parent', department);
            departmentPlat.addQuery('u_department_type', '!=', 'Lab');
            departmentPlat.query();
            gs.log("Number of child departments are " + departmentPlat.getRowCount());

            if (departmentPlat.getRowCount() > 0) {
                while (departmentPlat.next()) {
                    returnString.push(departmentPlat.toString());
                }
            }
        }
    }
    return "sys_idIN" + returnString.join(',');
}
 
 
On the Accountable tech organisation field, I'm trying to reference my script include by the following:
matthew_hughes_1-1733216778311.png

 

However, what I'm finding is that I'm not getting any results at all. Does anyone what I need to do to get the required results?

 

1 REPLY 1

Animesh Das2
Mega Sage

Hi @matthew_hughes,

 

I hope the code logic and syntax of the Script include is correct. At a first look I found that the syntax of calling the script include is wrong in your SS.

You need to call the a script include as shown below,

 

javascript: new ScriptIncludeName().LBGfilterDepartmentsAPM(current.department);

 

If this address your question, please don't forget to mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das