Email list of records to users dynamically

John Reynolds
Mega Expert

I'm looking for the best way to create a daily email notification that gets sent out to a dynamic list of users.   The requirements are as follows:

  • Once per day generate a list of records matching the below criteria and email it to each person pulled from the Assigned to field:
    • Record opened today
    • Asset tag field is empty
    • Assigned to (is dynamic, me)
    • Group by   field (u_car_number)
  • Email a link directly to the list of records to the user in the Assigned To field
  • In the email, display the records in text form to the user (glide query)

I've created a filter on the table that I'd like to use as the direct link in the email using the criteria from the 1st bullet: https://<instance-name>.service-now.com/alm_asset_list.do?sysparm_query=assigned_toDYNAMIC90d1921e5f...

Query used: assigned_toDYNAMIC90d1921e5f510100a9ad2572f2b477fe^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)^asset_tagISEMPTY

I've written most of the Glide Record query I want to include in the email.

The two issues I'm running into / unsure how to accomplish are:

  1. Sending the email once per day to the users
  2. Including only those users in the GlideRecord lookup.

Any help would be appreciated it!

gs.print("Assets pending certification");

var gr = new GlideRecord("alm_asset");

gr.addQuery("state", "Pending");

gr.addQuery("u_asset_tag", "");

gr.oderBy("assigned_to");

gr.orderByDesc('u_car_number');

gr.query();

while(gr.next()) {

gs.print(gr.u_number + " From CAR: " + gr.u_car_number + " Assigned To: " + gr.assigned_to.getDisplayValue());

}

12 REPLIES 12

Chuck Tomasi
Tera Patron

Hi John,



Can I ask what the underlying purpose of this is? Are you trying to remind people how many records they have assigned to themselves?


Hi Chuck,



Correct. I'd like to send a daily email reminder that they have records assigned to them where the asset tag field is blank and needs to be filled in.


Alikutty A
Tera Sage

Hi John,



You can write a scheduled job that runs on daily basis to query the URL and send notifications via an event.



A sample script would be



var gr = new GlideRecord("alm_asset");  


gr.addEncodedQuery("assigned_toDYNAMIC90d1921e5f510100a9ad2572f2b477fe^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)^asset_tagISEMPTY");  


gr.query();  


while(gr.next()) {  


  gs.eventQueue('event_name', current, gr.assigned_to, '');


}



The event should be selected in your notification, You can access all current fields on the notification or write email scripts to get additional details. Let me know if you require additional help.



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


FYI, you might want to consider changing 'current' to 'gr' as there is no 'current' object available to scheduled jobs natively. I also recommend wrapping it in IIFE format or using better variable naming than 'gr' as my co-worker michael.ritchie has run in to issues using 'gr' as a generic GlideRecord variable.



Example (untested)



(function () {


var gr = new GlideRecord("alm_asset");


gr.addEncodedQuery("assigned_toDYNAMIC90d1921e5f510100a9ad2572f2b477fe^sys_created_onONToday@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)^asset_tagISEMPTY");


gr.query();


while(gr.next()) {


  gs.eventQueue('event_name', gr, gr.assigned_to, '');


})();