Need to Reduce email notifications for Timesheet Approval

Tamilvanan T
Tera Contributor

We have a scheduled job which triggers the notification based for the time sheet submitted. This notification is triggered via event configured in the Scheduled Job. But the notifications are triggered multiple times to the user manager for the timesheets submitted, How we can change the script to triggered only one notification per manager so, that we can reduce the email notifications for the timesheet.

 

Scheduled script:

remindApprovers();

function remindApprovers() {
    var startDate = new GlideDate();
    startDate.setValue('2020-09-26');

    //Query the Time Sheet table for those in the Status of Submitted
    var ts = new GlideRecord('time_sheet');
    ts.addQuery('state', "Submitted");
    ts.addQuery('week_starts_on', ">", startDate);
    ts.query();

    while (ts.next()) {
        var usr = ts.user;
        var user = new GlideRecord('sys_user');
        user.addQuery("sys_id", usr);
        user.addQuery('active', 'true');
        user.query();
        if (user.next()) {
            gs.eventQueue('timesheet.approval.reminder', ts, user.manager.email);
        }
    }
2 REPLIES 2

Ankur Bawiskar
Tera Patron

@Tamilvanan T 

how is your notification configured? any email script?

check below links on how to combine and send 1 email, where I shared approach. enhance your code as per that

Email script

Email Notification to all users of assets assigned specifically to them  

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Giga Patron

Hi @Tamilvanan T ,

 

Modify your Scheduled Job script to group timesheet records by the manager's ID before triggering the event and check once.

 

Sample Code:

 

var gr = new GlideRecord('ts_timesheet'); 
gr.addEncodedQuery('state=submitted'); // replace it by your condition
gr.query();
var mgrMap = {};
while (gr.next()) { var mgrSysId = gr.user.manager.toString(); if (!mgrMap[mgrSysId]) { mgrMap[mgrSysId] = []; } mgrMap[mgrSysId].push(gr.user.getDisplayValue() + " - " + gr.number); }for (var mngr in mgrMap) { var timesheetList = mgrMap[mngr].join(', '); gs.eventQueue('timesheet.approval.reminder', null, timesheetList, mngr); }
 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: