Add Email addresses from report to Scheduled Report

Shree Nag
Tera Expert

hello,

I have a report which has a an email field.

I need to pull unique email ids from this report and put on to the TO field of the scheduled email of the report.

I have this script written in the condition part of the Scheduled job. When executed, the To does not seem to populate.

Could you please let me know what is that missing in the script or what is the best way to populate the email Ids on to the email to send the report.

 

Script

 

answer = lookupUsersAndUpdateReport();
function lookupUsersAndUpdateReport(){
    // look up "assigned_to" people -- assumes "table" is task-based
    var senderArray = [];
    var PrevSender = '';

    var taskQueryGR = new GlideRecord(current.report.table);
    taskQueryGR.addEncodedQuery(current.report.filter);
    taskQueryGR.addEncodedQuery('assigned_toISNOTEMPTY');
    taskQueryGR.orderBy('assigned_to');
    taskQueryGR.query();
    while (taskQueryGR.next()){
        if (PrevSender != taskQueryGR.assigned_to + ''){ //filters out duplicate assignees
            senderArray.push(taskQueryGR.assigned_to + '');
            PrevSender = taskQueryGR.assigned_to + '';
        }
    }

    current.user_list = senderArray.join(',');
    current.setWorkflow(false);
    current.update();
    current.setWorkflow(true);

    // only return true if there were records listed in the result set
    if (senderArray.length > 0){
        return true;
    }
    return false;
}
1 ACCEPTED SOLUTION

Sumanth16
Kilo Patron

Hi @Shree Nag , 

 

Please try below approach if possible:

 

After creating your report, pull down the menu and choose "Schedule". Note that you'll want to omit the "Assigned to" query parameter.. we'll take care of that later in our script:

 

Sumanth16_0-1709240348294.png

 

 

Then on the resulting page, click the "Conditional" box, and a script area shows up. Taking the suggested script from the other thread you referenced, paste it in the script area:

 

Sumanth16_1-1709240348278.png

 



Here's the script that should work for you:

 

answer = lookupUsersAndUpdateReport();

 

function lookupUsersAndUpdateReport(){

 

  // look up "assigned_to" people -- assumes "table" is task-based

 

  var senderArray = [];



  var taskQueryGR = new GlideRecord(current.report.table);

 

  taskQueryGR.addEncodedQuery(current.report.filter);

 

  taskQueryGR.query();

 

  while (taskQueryGR.next()){

 

          senderArray.push(taskQueryGR.assigned_to + '');

 

  }



  current.user_list = senderArray.join(',');

 

  current.setWorkflow(false);

 

  current.update();

 

  current.setWorkflow(true);



  // only return true if there were records listed in the result set

 

  if (senderArray.length > 0){

 

          return true;

 

  }

 

  return false;

 

}

 

 

..And that should do it!

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

View solution in original post

1 REPLY 1

Sumanth16
Kilo Patron

Hi @Shree Nag , 

 

Please try below approach if possible:

 

After creating your report, pull down the menu and choose "Schedule". Note that you'll want to omit the "Assigned to" query parameter.. we'll take care of that later in our script:

 

Sumanth16_0-1709240348294.png

 

 

Then on the resulting page, click the "Conditional" box, and a script area shows up. Taking the suggested script from the other thread you referenced, paste it in the script area:

 

Sumanth16_1-1709240348278.png

 



Here's the script that should work for you:

 

answer = lookupUsersAndUpdateReport();

 

function lookupUsersAndUpdateReport(){

 

  // look up "assigned_to" people -- assumes "table" is task-based

 

  var senderArray = [];



  var taskQueryGR = new GlideRecord(current.report.table);

 

  taskQueryGR.addEncodedQuery(current.report.filter);

 

  taskQueryGR.query();

 

  while (taskQueryGR.next()){

 

          senderArray.push(taskQueryGR.assigned_to + '');

 

  }



  current.user_list = senderArray.join(',');

 

  current.setWorkflow(false);

 

  current.update();

 

  current.setWorkflow(true);



  // only return true if there were records listed in the result set

 

  if (senderArray.length > 0){

 

          return true;

 

  }

 

  return false;

 

}

 

 

..And that should do it!

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda