The CreatorCon Call for Content is officially open! Get started here.

to send a email notification with count of open sc tasks

Priyadarshini2
Tera Contributor

I want to send a email notification every 6 hours. The email body will contain the count of all open tasks , the tasks number and short descriptions of those open tasks. Please help urgent.

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Priyadarshini2 

 

better to create a flow for this.

 

https://www.servicenow.com/community/developer-forum/how-to-send-notification-every-one-hour-for-the...

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyadarshini2 

you can use scheduled job which runs periodically every 6 hours.

You can configure event, email etc on your INC table

Then use this approach on how to send 1 email to 1 user containing list of all tasks/incidents

I have given example for alm_asset table you can enhance for your table

1) Create event on alm_asset table

2) Create Notification on alm_asset table and link the above event created

3) Use the script shared below in scheduled job to trigger email per user

4) Create email Script and in that you can print the list of Assets Assigned to that particular user for which email triggered.

1) Event

Name: send.email.assignedToUser.asset

Table: alm_asset

2) Email Script:

Name: assets_assigned

(function runMailScript(current, template, email, email_action, event) {

var user = event.parm1;

var arr = [];

var gr = new GlideRecord('alm_asset');

gr.addQuery('assigned_to', user);

gr.query();

while(gr.next()){

arr.push(gr.display_name.toString());

}

template.print('Assets assigned to you are: ' + arr.toString());

})(current, template, email, email_action, event);

3) Job Script:

sendEmail();


function sendEmail(){


               var gr = new GlideAggregate("alm_asset");

               gr.addAggregate("COUNT");

               gr.addEncodedQuery('assigned_toISNOTEMPTY');

               gr.groupBy("assigned_to");

               gr.query();

               while(gr.next()) {

                              var user = gr.assigned_to;


                              var assetRec = new GlideRecord('alm_asset');

                              assetRec.get('assigned_to', user);


                              gs.eventQueue("send.email.assignedToUser.asset", assetRec, user, "");

               }

}

4) Notification:

Body:

${mail_script:assets_assigned}

 

AnkurBawiskar_0-1743685706918.png

 

 

AnkurBawiskar_1-1743685706919.png

 

 

AnkurBawiskar_2-1743685706923.png

 

 

AnkurBawiskar_3-1743685706925.png

 

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

Mark Manders
Mega Patron

The question really is: why? What is the upside to doing this? People will check the email the first two times and then ignore it. And how can sending an email be urgent? If someone really wants this, they can also log in to the system every 6 hours.

And for the solution: why not use a scheduled report? Just create a report on the table you need, add the fields you need and schedule it to run every 6 hours. No need for scripting at all.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyadarshini2 

simply use this

(function() {
    // Query to get all open tasks
    var taskGR = new GlideRecord('incident');
    taskGR.addActiveQuery();
    taskGR.addEncodedQuery('assigned_to!=NULL');
    taskGR.setLimit(10);
    taskGR.query();
    while (taskGR.next()) {
        var gr = new GlideRecord("incident");
        gr.addActiveQuery();
        gr.addEncodedQuery('assigned_to=' + taskGR.assigned_to);
        gr.query();
        var taskCount = gr.getRowCount();
        // Create the email body
        var emailBody = "There are " + taskCount + " open tasks for you:\n\n";
        emailBody += "Task Number: " + taskGR.getValue('number') + "\n";
        emailBody += "Short Description: " + taskGR.getValue('short_description') + "\n\n";
        // Send the email
        var email = new GlideEmailOutbound();
        email.setSubject("Open Tasks Notification");
        email.setBody(emailBody);
        email.setRecipients(taskGR.assigned_to.email.toString()); // Replace with your recipient email
        email.save();
    }


})();

I ran this for incident and here is my email log

AnkurBawiskar_4-1743687402797.png

 

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