Inconsistent Behavior with javascript: Filter Using Script Include in Report Conditions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
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
an hour ago - last edited an hour ago
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