Dynamic filter help

Karla Chorny
Tera Expert

I need to be able to filter a list by department that will pull all records associated to the user's department PARENT (the division).

 

I've created a dynamic filter that calls a script include.

KarlaChorny_0-1761752571329.png

 

The function I'm calling:

getDivision: function(user) {
		//called from dynamic filter 'My Division'
		var answer = '';
        gs.log('HW: getDivision function called',"KC");
		var hwList = [];
		var u = new GlideRecord('sys_user');
		if (u.get(user)) {
			var dParent = u.department.parent;		
			var dpt = new GlideRecord('cmn_department');
            dpt.addQuery('sys_id',dParent).addOrCondition('parent.sys_id',dParent);
            dpt.query();
			gs.log('HW: Number of depts: ' + dpt.getRowCount(),"KC");
            while (dpt.next()) {
                hwList.push(dpt.sys_id + '');
            }
            gs.log('HW: List: ' + hwList,"KC");
			var answerString = 'sys_idIN'+hwList.toString();
			gs.log('HW: Answer: ' + answerString,"KC")
			answer = answerString;        			
		}
		return answer;      
    },

All of my log messages show that I'm getting the expected values, but the filter isn't working, even when I run it as admin. No records are returned. 

 

What am I missing? TIA

1 ACCEPTED SOLUTION

Muhammad Salar
Giga Sage

Hello @Karla Chorny 
return array, modify your script include function like this

getDivision: function(user) {
        var hwList = [];
        var u = new GlideRecord('sys_user');
        if (u.get(user)) {
            var dParent = u.department.parent;
            var dpt = new GlideRecord('cmn_department');
            dpt.addQuery('sys_id', dParent).addOrCondition('parent.sys_id', dParent);
            dpt.query();
            while (dpt.next()) {
                hwList.push(dpt.sys_id);
            }
            
        }
        return new ArrayUtil().unique(hwList);
    },


Mark as correct if this resolves your issue

View solution in original post

5 REPLIES 5

Hemanth M1
Giga Sage
Giga Sage

Hi @Karla Chorny ,

 

Whats your answerString returning?
it should return comma separated values


can you try answer = "sys_idIN"+answerString.join(",");

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Sarthak Kashyap
Kilo Sage

Hi @Karla Chorny ,

 

Please try with below code

getDivision: function(user) {
    var hwList = [];
    var u = new GlideRecord('sys_user');
    if (u.get(user)) {
        var dParent = u.department.parent;

        var dpt = new GlideRecord('cmn_department');
        dpt.addQuery('sys_id', dParent).addOrCondition('parent', dParent);
        dpt.query();

        while (dpt.next()) {
            hwList.push(dpt.getUniqueValue());
        }
        var answerString = 'sys_idIN'+hwList.toString();
    }

    return answerString.join(',');
},

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

RaghavSh
Mega Patron

@Karla Chorny You cannot convert an array to string. Try below:

 

var answerString = 'sys_idIN'+hwList;

 

 


Raghav
MVP 2023
LinkedIn

Muhammad Salar
Giga Sage

Hello @Karla Chorny 
return array, modify your script include function like this

getDivision: function(user) {
        var hwList = [];
        var u = new GlideRecord('sys_user');
        if (u.get(user)) {
            var dParent = u.department.parent;
            var dpt = new GlideRecord('cmn_department');
            dpt.addQuery('sys_id', dParent).addOrCondition('parent.sys_id', dParent);
            dpt.query();
            while (dpt.next()) {
                hwList.push(dpt.sys_id);
            }
            
        }
        return new ArrayUtil().unique(hwList);
    },


Mark as correct if this resolves your issue