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.

WorkFlow Notification

dixitreddy
Tera Contributor

Hi

my requirement is in catalog item if catalog task is in open(not closed) then it should trigger email after 1 day if task is still in open state ,like wise after 3 days,7 days,15 days  if it is closed at after 3 days remaining notifications should not trigger this should be done using workflow

6 REPLIES 6

SP22
Giga Sage

Hello @dixitreddy,

Please see the below screenshot and let me know whether it is helpful or not.

find_real_file.png

Thanks
SP

dixitreddy
Tera Contributor

if catalog task closes after timer1 then remaining notifications will trigger

hi @dixitreddy 

you can achieve this functionality by scheduled job & flow designer (recommended) 

Here i am providing the scheduled job script (frequency - daily; time - set as per your requirement)

// Define the notification intervals
var intervals = [1, 3, 7, 15]; // Days after which to send notifications

// Get the current date
var currentDate = new GlideDateTime();

// Loop through each interval
for (var i = 0; i < intervals.length; i++) {
    var days = intervals[i];

    // Calculate the date threshold
    var thresholdDate = new GlideDateTime(currentDate);
    thresholdDate.addDays(-days); // Subtract the number of days

    // Query catalog tasks that are still open and were created before the threshold date
    var taskGR = new GlideRecord('sc_task');
    taskGR.addQuery('state', '!=', 'Closed'); // Ensure the task is not closed
    taskGR.addQuery('sys_created_on', '<=', thresholdDate);
    taskGR.query();

    while (taskGR.next()) {
        // Check if an email has already been sent
        if (!hasNotificationBeenSent(taskGR.sys_id, days)) {
            // Send notification
            sendNotification(taskGR);
        }
    }
}

// Function to check if a notification has already been sent
function hasNotificationBeenSent(taskId, days) {
    var notificationGR = new GlideRecord('sys_email');
    notificationGR.addQuery('table_name', 'sc_task');
    notificationGR.addQuery('table_sys_id', taskId);
    notificationGR.addQuery('subject', 'CONTAINS', 'Reminder: Task still open after ' + days + ' days');
    notificationGR.query();
    return notificationGR.hasNext();
}

// Function to send the notification
function sendNotification(taskGR) {
    var email = new GlideEmailOutbound();
    email.setTo(taskGR.requested_for.email); // Change as necessary for your recipient
    email.setSubject('Reminder: Task still open after ' + days + ' days');
    email.setBody('This is a reminder that your catalog task ' + taskGR.number + ' is still open.');
    email.send();
}

 

I hope this will helps you

thank you

rajesh

Thank for ur answer but i need to add this functionality to existing workflow