- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
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.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
Hi @Karla Chorny ,
Whats your answerString returning?
it should return comma separated values
can you try answer = "sys_idIN"+answerString.join(",");
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
@Karla Chorny You cannot convert an array to string. Try below:
var answerString = 'sys_idIN'+hwList;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
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
