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
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');
}
}
}

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

PrashantLearnIT
Giga Sage

HI @tsoct 

 

Have you tried to utilize Inbound EMail Action for this?

 

 

********************************************************************************************************
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.

********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect


Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************

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
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');
}
}
}

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


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/