Iterate through the Recipients array

Bogdan18
Tera Guru

Hi guys,

 

I am trying to iterate through the Recipients array in search for a specific email address. I have the below script include and it works great but only if the email address is the first one in line, if it is situated in the TO or CC field on a second or third.... position the for loop fails. Any idea why?

 

I see in the logs that only the first email in the Recipients field is being evaluated and then other inbound email actions run and then I don't see any other logs for gs.log('Bog  ' + checkEm[i]); only "inlooppp" logs, no data.

 

Script Include below:

var checkemail= Class.create();
checkemail.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkSpecificEmail: function(){
var gr = new GlideRecord("sys_email");
gr.query();
//while (gr.next()) {
var checkEm = email.recipients_array;
for(var i = 0; i < checkEm.length; i++){
gs.log('Bog  ' + checkEm[i]);
if(checkEm[i] == '<<my email address>>' || checkEm[i] == '<<my email address in lowercase>>'){
return true;
}
else {
return false;
}
}
//}

},

type: 'checkemail'
});

 

 

Thanks,

Bogdan

1 ACCEPTED SOLUTION

Dubz
Mega Sage

Hi Bogdan,

 

You're returning the response in the for loop so it's completing the function before it has iterated through the entire array. Try this:

checkSpecificEmail: function(){
var answer = false;
var checkEm = email.recipients_array;

for(var i = 0; i < checkEm.length; i++){
var recipient = checkEm[i].toLowerCase();
if(recipient.indexOf('youraddress@inlowercase.com') > -1){
answer = true;
}
}
return answer;
}

View solution in original post

2 REPLIES 2

Dubz
Mega Sage

Hi Bogdan,

 

You're returning the response in the for loop so it's completing the function before it has iterated through the entire array. Try this:

checkSpecificEmail: function(){
var answer = false;
var checkEm = email.recipients_array;

for(var i = 0; i < checkEm.length; i++){
var recipient = checkEm[i].toLowerCase();
if(recipient.indexOf('youraddress@inlowercase.com') > -1){
answer = true;
}
}
return answer;
}

Hi David,

 

Many thanks for the solution suggested. You were spot on. 

 

regards,

Bogdan