Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Inconsistent Behavior with javascript: Filter Using Script Include in Report Conditions

mkm1
Mega Guru

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?

1 ACCEPTED SOLUTION

Rafael Batistot
Kilo Patron

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:

 

  1. 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

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

View solution in original post

4 REPLIES 4

Sujit Karn
Tera Contributor

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

Rafael Batistot
Kilo Patron

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:

 

  1. 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

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

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.

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(',');

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.