Scheduled job to trigger email notification recipient value

BanuMahalakshmi
Tera Contributor

Hi, 

Pls advise me to trigger email notification recipient value from scheduled job, below is the scenario to get recipient value:

 

  • Recipients will be determined in the scheduled job script and passed to email notification as a string in parm1.
  • CI assigned to user. 
    • Include assigned to user as a recipient if the CI has an assigned to user, and if this user is NOT a VIP.
  • CI assigned to user’s manager.
    • Include assigned to user’s manager only if sending notification to assigned to user, and only if the manager is NOT a VIP.
  • Active group contact of type “Local support” of the location associated with the CI.
    • If query returns more than one group, just send to the first group returned
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_class_name', '=', 'cmdb_ci_computer');
gr.addQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
while (gr.next()) {
var assignedto = gr.assigned_to.email;
var param1
if(assignedto == " || assignedto.vip = true)
{
param1 = gr.assigned_to.manager.email;
}
else
{
param1 = gr.assigned_to.email;
}
 
gs.eventQueue("PC Email Notification", gr, param1);
}
2 REPLIES 2

Bhavya11
Kilo Patron

Hi @BanuMahalakshmi ,

 

can you try script like below

 

 var param1 = ""; 
 var recipients = [];
 var a;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_class_name', 'cmdb_ci_computer');
gr.addEncodedQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();

while (gr.next()) {
    var assignedToEmail = gr.assigned_to.email;
    var assignedToManagerEmail = gr.assigned_to.manager.email;

    if (assignedToEmail && !gr.assigned_to.vip) {
        recipients.push(assignedToEmail); // Add assigned user to recipients

        if (assignedToManagerEmail && !gr.assigned_to.manager.vip) {
            recipients.push(assignedToManagerEmail);
        }
    }

    if (gr.location) {
        var groupGR = new GlideRecord('sys_user_group');
       // groupGR.addQuery('contact_type', 'Local support');
        groupGR.addQuery('active', true);
        groupGR.addQuery('name', gr.location.u_service_desk);
        groupGR.query();

        if (groupGR.next()) {
            // Only add the first group contact found
            recipients.push(groupGR.email);
        }
    }

    param1 = recipients.join(',');
  a=gr;
  
}
  gs.eventQueue("pc_email_notification", a, param1);

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

BK

Runjay Patel
Giga Sage

Hi @BanuMahalakshmi ,

 

Bhavya code will work perfectly only one modification you can like below,

//From
if (assignedToEmail && !gr.assigned_to.vip) {
        recipients.push(assignedToEmail); // Add assigned user to recipients

        if (assignedToManagerEmail && !gr.assigned_to.manager.vip) {
            recipients.push(assignedToManagerEmail);
        }
    }

//To

if (assignedToEmail && !gr.assigned_to.vip) 
        recipients.push(assignedToEmail); // Add assigned user to recipients
  
if (assignedToManagerEmail && !gr.assigned_to.manager.vip) 
            recipients.push(assignedToManagerEmail);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------