- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2025 09:41 AM
In our scoped application, I’ve created three reports in ServiceNow—Active Planned Actions (Companies), Active Procurement Actions (Companies), and Contracts Under Administration (Companies)—all using the same filter condition: field_name.sys_id is one of javascript:x_20949_ppt.getUserDescendantStakeholderCompanies(). This function returns a list of company sys_ids relevant to the current user. However, only the first report (Active Planned Actions) fails to return results unless I explicitly add gs.info('getUserDescendantStakeholderCompanies(): ' + x_20949_ppt.getUserDescendantStakeholderCompanies()); in the Query Business Rule. The other two reports work without this logging line. It seems like the function is not being evaluated properly in the first report unless forced by gs.info(). I’m trying to understand why this inconsistency occurs and whether it’s related to lazy evaluation, or execution timing. Has anyone encountered similar behavior or know best practices for using javascript: filters with Script Includes in reports?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2025 01:32 PM - edited 09-27-2025 01:34 PM
Hi @mkm1
Your issue happens because javascript: filters in reports are inconsistently evaluated.
Some report types force the function to run, others delay (lazy evaluation). Adding gs.info() “forces” execution, which is why the first report only works with it.
This is a known quirk and not a bug in your Script Include.
Best practice: avoid javascript: in reports.
Recommended options:
- Use a Dynamic Filter Option (DFO)
- Create a Script Include that implements GlideFilterExtension.
- Register a Dynamic Filter Option (e.g., My Stakeholder Companies).
- Then in your report, you can filter with:
field_name.sys_id is (Dynamic) My Stakeholder Companies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2025 11:48 AM
We can use javascript: filters when we need the dynamic data or want to reuse logic for multiple reports. Arrays or comma-separated strings work best for filters in script include.
Check-> Is getUserDescendantStakeholderCompanies() public and accessible from the report context? (isPublic: true) For example -
var ReportUtils = Class.create();
ReportUtils.prototype = {
initialize: function() {},
getRecords: function() {
var result = [];
var gr = new GlideRecord('incident');
gr.addQuery('priority', '1');
gr.query();
while (gr.next()) {
result.push(gr.getValue('sys_id'));
}
return result.join(',');
}
};Report condition sys_id is javascript:new ReportUtils().getRecords()
or
Company is javascript:x_20949_ppt.getUserDescendantStakeholderCompanies()
also check if the first report has additional filters or conditions that might exclude the expected companies. Debug more
gs.info('Fetching companies for user: ' + gs.getUserID());
gs.info('Companies found: ' + companyList);
var companies = x_20949_ppt.getUserDescendantStakeholderCompanies();
gs.info('Returned Companies: ' + companies);
Check the thread Solved: gs.getUser() in script include called from scoped ... - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2025 01:32 PM - edited 09-27-2025 01:34 PM
Hi @mkm1
Your issue happens because javascript: filters in reports are inconsistently evaluated.
Some report types force the function to run, others delay (lazy evaluation). Adding gs.info() “forces” execution, which is why the first report only works with it.
This is a known quirk and not a bug in your Script Include.
Best practice: avoid javascript: in reports.
Recommended options:
- Use a Dynamic Filter Option (DFO)
- Create a Script Include that implements GlideFilterExtension.
- Register a Dynamic Filter Option (e.g., My Stakeholder Companies).
- Then in your report, you can filter with:
field_name.sys_id is (Dynamic) My Stakeholder Companies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2025 02:08 PM
Hi Rafael,
Thank you for your guidance. Created a Dynamic filter option, the report is not returning any results when using the Dynamic Filter.
Will the condition field_name.sys_id is (Dynamic) My Stakeholder Companies work if My Stakeholder Companies returns multiple sys_ids in an array?
Thanks again for your input.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2025 02:27 AM
Hi @mkm1
Yes, it works but the return format is important.
When you implement your Dynamic Filter Option, the getFilterCondition() (or similar) must return a comma-separated list of sys_ids (e.g. "sys_id1,sys_id2,sys_id3") rather than a JavaScript array
Try in your scrip include return
return myArray.join(',');
