Need to compare email address from user table to email address in ad account table and send notiy

siddharth26
Tera Guru

Hi All,

Am using below script to match the email address from user table to email address i AD table and if mismatch is found in email address i need a email to be sent out with the details.

/

/ Create a new GlideRecord for sys_user table with an encoded query
var sysUser = new GlideRecord('sys_user');
sysUser.addEncodedQuery('u_account_typeINemployee,contractor^active=true^);
sysUser.query();

var matchFound = false; // Initialize the matchFound flag to false

while (sysUser.next()) {
var sysUserEmail = sysUser.email.toString();

// Create a new GlideRecord for u_ad_user_account table with an encoded query
var adUser = new GlideRecord('u_ad_user_account');
adUser.addEncodedQuery('email_address=' + sysUserEmail);
adUser.query();

if (adUser.next()) {
// A match is found, set matchFound to true and break out of the loop
matchFound = true;
break;
}
}

// Send an email outside the loop if no match is found
if (!matchFound) {
var email = new GlideEmailOutbound();
email.setSubject('No Email Match Found');
email.setBody('No matching email addresses found.');
email.addRecipient('group@example.com'); // Add recipient group email address
email.send();
}

 

but am not getting any emails when mismatch is found could you please suggest .

thanks 

sid

2 REPLIES 2

Mayur2109
Kilo Sage
Kilo Sage

Hi @siddharth26 ,

Check line sysUser.addEncodedQuery('u_account_typeINemployee,contractor^active=true^); 

It should be sysUser.addEncodedQuery('u_account_typeINemployee,contractor^active=true^'); 

You missed to complete single quote.

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

Hi Mayur,

thanks for the reply

var sysUser = new GlideRecord('sys_user');
sysUser.addEncodedQuery('u_account_typeINemployee,contractor^active=true');
sysUser.query();

var matchFound = false;

while (sysUser.next()) {
var sysUserEmail = sysUser.email.toString();

if (sysUserEmail.endsWith('xyz.') || sysUserEmail.endsWith('abc.com')) {

var adUser = new GlideRecord('u_ad_user_account');
adUser.addQuery('email_address', sysUserEmail);
adUser.query();

if (adUser.next()) {

matchFound = true;
break;
}
}
}


if (!matchFound) {
var email = new GlideEmailOutbound();
email.setSubject('No Email Match Found');
email.setBody('No matching email addresses found.');
email.addRecipient('siddharth@example.com');
email.send();
}

 

i have made the changes to the query as well but still we the emails are not been sent .

could you please suggest 

thanks