Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

Check recipients addresses

tsoct
Tera Guru

Helo all,

 

The script below is returning me 'No'. What could be the issue?

 var email = GlideRecord('sys_email');
 email.addEncodedQuery('mailbox=received^sys_id=67acb6c247109a10b58a86fd436d4312');
 email.query();

 if (email.next()) {
     var e = 'test@email.com';

     var recipientsArray = [];
     recipientsArray.push(email.recipients.toUpperCase().split(','));

     for (var i = 0; i < recipientsArray.length; i++) {

         if ((recipientsArray.indexOf(e) > -1)) {
             gs.info('Yes');
         } else if ((recipientsArray.indexOf(e) == -1)) {
             gs.info('No');
         } 
     }
 }
1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage

Hi @tsoct 

 

The issue is recipientsArray.push(email.recipients.toUpperCase().split(',')); creates a nested array, and when you later check recipientsArray.indexOf(e), it doesn't work as expected because recipientsArray contains an array inside of it, not a flat array of strings.

 

Try the below script :-

var email = new GlideRecord('sys_email');
email.addEncodedQuery('mailbox=received^sys_id=67acb6c247109a10b58a86fd436d4312');
email.query();
if (email.next()) {
var e = 'test@email.com';
var recipientsArray = email.recipients.toUpperCase().split(',');
for (var i = 0; i < recipientsArray.length; i++) {
if (recipientsArray[i] === e.toUpperCase()) {
gs.info('Yes');
break;
} else if (i === recipientsArray.length - 1) {
gs.info('No');
}
}
}

View solution in original post

3 REPLIES 3

PrashantLearnIT
Tera Sage

HI @tsoct 

 

Have you tried to utilize Inbound EMail Action for this?

 

 

Hello @PrashantLearnIT ,

 

Inbound Email Action is unable to meet my use case, which involves checking header text against the list of recipients. The script I added is only the part that is not working.

Ravi Gaurav
Giga Sage

Hi @tsoct 

 

The issue is recipientsArray.push(email.recipients.toUpperCase().split(',')); creates a nested array, and when you later check recipientsArray.indexOf(e), it doesn't work as expected because recipientsArray contains an array inside of it, not a flat array of strings.

 

Try the below script :-

var email = new GlideRecord('sys_email');
email.addEncodedQuery('mailbox=received^sys_id=67acb6c247109a10b58a86fd436d4312');
email.query();
if (email.next()) {
var e = 'test@email.com';
var recipientsArray = email.recipients.toUpperCase().split(',');
for (var i = 0; i < recipientsArray.length; i++) {
if (recipientsArray[i] === e.toUpperCase()) {
gs.info('Yes');
break;
} else if (i === recipientsArray.length - 1) {
gs.info('No');
}
}
}