Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Conditional Scheduled report

Tapish Sharma1
Kilo Sage

Hi Folks,

i am trying to create a conditional scheduled report. But in the condition script box i am not able to create a function nor I am able to call a script include . i need to check if a table has records and only then trigger the report. In the documentation it is mentions contional script is sandbox script and runs in sandbox environment

" Conditional scripts for scheduled report emails are executed in the sandbox. Therefore, function definitions are not allowed. Some API calls and keywords are also not allowed. For more information, see Script sandbox property."

. Not sure How can I run the script ? I tried below script but it is not working ....neither is calling script include or functions working ...

TapishSharma1_0-1668438442085.png

var rec = new GlideRecord('sn_customerservice_contact_relationship');
rec.addEncodedQuery('company=a525514f1b5d11101786dbd3b24bcb36^u_contact_last_updatedONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^ORu_contact_last_validatedONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^responsibilityLIKE4d50a30e1b4f9510c4ac21f0b24bcb75');
rec.query();
var count = rec.getRowCount();
// gs.info('Count ' +count);

if (count==0)
{
answer = false;
}
else
{
answer = true;
}

1 REPLY 1

GregM1
Tera Contributor

Hi,

Try this approach:
1. Move all your code to the script include, remember to check the "Sandbox enabled" option:

var myReportHelper = Class.create();
myReportHelper.prototype = {
    initialize: function() {
    },

reportCriteria: function() {
        var ga = new GlideAggregate('sn_customerservice_contact_relationship');
        ga.addAggregate('COUNT');
        ga.addEncodedQuery('company=a525514f1b5d11101786dbd3b24bcb36^u_contact_last_updatedONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^ORu_contact_last_validatedONYesterday@javascript:gs.beginningOfYesterday()@javascript:gs.endOfYesterday()^responsibilityLIKE4d50a30e1b4f9510c4ac21f0b24bcb75');
        ga.query();
        ga.next();

        if (ga.getAggregate('COUNT') != 0) {
            return true;
        } else {
            return false;
        }
    },

    type: 'myReportHelper'
};


2. Update your scheduled report condition to use this SI:

new myReportHelper().reportCriteria()

 

Important things to remember:

1. If you need to do some logging update the glide.security.sandbox_no_logging system property

2. Info from ServiceNow KB article:

 

What Guarded Script Does Not Allow

Guarded script accepts only a single, simple expression. The following JavaScript features are not supported inside the sandbox:

  • Variable declarations (var, let, const)
  • Control flow (if, switch, for, while)
  • Function declarations
  • Assignment operators (=)
  • Multiple statements — only a single expression is allowed per script