Scheduled email of report

paraspuri
Tera Contributor

issue1.png

issue2.png

 

I created a scheduled email of report which runs every 12 hours.It should send email if there are no records created on interaction table for past 12 hours.If any record is created on interaction table in last 12 hours,then no email should be sent.Attached is the script.I n the logs and background script,it is showing  answer value as true but email is not triggering.

3 REPLIES 3

jonsan09
Giga Sage
Giga Sage

Try this: 

var tableName = "Your_table";
var encodedQuery = "Your_Encoded_Query";


var recCheck= new GlideRecord(tableName);
recCheck.addEncodedQuery(encodedQuery);

recCheck.setLimit(1); // Optimize performance for existence check. Set limit to 1, as we only need to know if any record exists.
recCheck.setWorkflow(false);
recCheck.query();

// This boolean value determines if the scheduled report will be sent.
answer = !recCheck.hasNext();



Ankur Bawiskar
Tera Patron
Tera Patron

@paraspuri 

it should work ideally.

Remember the condition script doesn't evaluate when you click "Execute Now".

Did you actually let the scheduled report run and verify?

OR

Another way

1) keep the scheduled report inactive, don't give any condition and condition script

2) have scheduled job which runs periodically every 12 hours

3) have script to check if records are present or not

a) if not then trigger the scheduled report via script

// your glideRecord query here

if (record not present) {
    var schReport = new GlideRecord('sysauto_report'); // Or your specific table
    schReport.addQuery('name', 'Your Scheduled Report Name'); // Replace with your report's name
    schReport.query();
    if (schReport.next()) {
        if (typeof SncTriggerSynchronizer != 'undefined') {
            gs.executeNow(schReport);
        } else {
            Packages.com.snc.automation.TriggerSynchronizer.executeNow(schReport);
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

kaushal_snow
Mega Sage

Hi @paraspuri ,

 

Don’t use a scheduled report with omit if no records, that only works the other way (send only when records exist). Use sys_created_on and addHours(-12) to accurately detect the 12‑hour timeframe..

 

var gr = new GlideRecord('interaction');
var cutoff = new GlideDateTime();
cutoff.addHours(-12);
gr.addQuery('sys_created_on', '>=', cutoff);
gr.query();

if (gr.getRowCount() === 0) {
gs.eventQueue('interaction.no.records', null, '', '');
} else {
gs.log('Found ' + gr.getRowCount());
}

 

Try this once and let me know. If you find this helpful, please accept this as a solution and hit the helpful button..

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/