HI how to send email notification on scheduled job

siddharth26
Tera Guru

Hi,

 

i have a requirement to match the email address of the users in user table and email address in AD account table and if its matches it needs to do nothing , if its emails address of user table and AD table does not match it should send a notification to the group saying email address does not match in a scheduled job which runs every day.

 

i have used the below script but its not genertaing an email.

 

var userGr = new GlideRecord('sys_user');
userGr.query();
 
while (userGr.next()) {
 
    if (userGr.employee_number) {
        // Query the u_ad_account table for matching records
 
        var adAccountGr = new GlideRecord('u_ad_accounts');
 
        adAccountGr.addQuery('u_emp_id', userGr.employee_number);
 
        adAccountGr.query();
 
        var emailMismatch = 1;
        var emailAddresses = [];
 
        // Iterate through the matching ad_account records
 
        while (adAccountGr.next()) 
{
           
            if (userGr.email != adAccountGr.u_email)                             // Check if email id and email address match
 
{
                ++emailMismatch;
            }
 
            emailAddresses.push(adAccountGr.u_email);
        }
        
        if (emailMismatch > 1)                                       // Update the u_ad_account_status field in the sys_user record
{
            userGr.u_ad_account_status = emailAddresses.join(', ');
 
 
        } else {
 
            userGr.u_ad_account_status = "all emails match";
        }
 
        // Update the sys_user record
        userGr.update();
    }
}
 
could you please suggest i need a email to sent when email ID does not match from user table and AD table
 
thanks
sid

 

 

3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @siddharth26,

 

Create a new event in the Event Registry.

 

Trigger that event in your scheduled job, where parm1 and parm2 are optional:

 

gs.addEventQueue('<<event name>>', userGr, 'parm1', 'parm2');

 

 

Use that event in your notification record as trigger.

 

References:

GlideSystem - Global (servicenow.com)

Event registry (servicenow.com)


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter,

 

Thanks , emails are getting triggered but for every mismatch its sending individual emails suppose there is 10 emails mismatch from user table to Ad email address 10 emails is been sent , i just need single email to be sent to a group with these 10 emails address mismatch details.

 

thanks

sid

Then you will have to place the gs.eventqueue lower in your script, outside of the while loop.

Gather information regarding the users, in a way which suits you, and parse that in parm1 or parm2.

 

Something like this in the begin of your script.

var failedUserDetails = '';


Then in every mismatched user:

failedUserDetails += userGr.getDisplayValue() + ' ' + userGr.getValue('user_name') + '</br>';


In your notification you can use it as ${event.parm1}


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.