- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 02:00 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 02:45 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 02:45 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 03:50 AM
Hi David,
Many thanks for the solution suggested. You were spot on.
regards,
Bogdan