0

My Goal is to remove certain email address from To when user click on Reply All

Map<Id, EmailConfiguration__c> mapEconf =  C3S_EM_EmailConfiguration.getAllEmailConfigsByIds();
            List<String> EmailConfigs = new List<String>();
            
            for( EmailConfiguration__c EM : mapEconf.values()){
                EmailConfigs.add(EM.EmailServicesAddress__c);
            }
            List<String> AllAddresses = new List<String>();
            AllAddresses.add(emailMessage.toAddress);
            for (String Address : AllAddresses){
                if(!EmailConfigs.contains(Address) ){
                    AllAddresses.add(Address);
                }
                
            }
            
            if (sendEmailDefaults.getInReplyToId() != null) {
                
                EmailMessage lv_origMessage =
                    [SELECT FromAddress
                     FROM EmailMessage
                     WHERE Id = :sendEmailDefaults.getInReplyToId()];
                if (String.isNotBlank(emailMessage.toAddress) &&
                    emailMessage.toAddress.contains(lv_origMessage.FromAddress) == false )
                    
                {
                    emailMessage.toAddress += ';' + lv_origMessage.FromAddress;
                } else {
                    emailMessage.toAddress = lv_origMessage.FromAddress;
                }                    
            }
            
                emailMessage.toAddress = String.join(AllAddresses,';'); 

The error is on line : for (String Address : AllAddresses){ - Any suggestion ?

Another issue : How to eliminate EmailConfigs from ccAddress ? i couldn't do this : AllAddresses.remove(EmailConfigs)

1
  • 1
    you are adding to the list that the for loop is iterating over - either iterate over a different list or add to a different list
    – cropredy
    Commented Jan 27, 2022 at 17:19

1 Answer 1

1

Your loop is trying to add new (potential duplicates) to the List of Strings that you are currently iterating over, this is not allowed.

To build a List of all addresses you can swap the List access such as

for (String emailConfig : EmailConfigs) {
    if(!AllAddresses.contains(emailConfig) ){
        AllAddresses.add(emailConfig);
    }
}

Or, you can use a Global list for the differences such as

List<String> globalList = new List<String>();
for (String Address : AllAddresses){
    if(!EmailConfigs.contains(Address) ){
        globalList.add(Address);
    }
}
4
  • thanks - but i'm stuck how to eliminate the emailConfig from To
    – user110654
    Commented Jan 27, 2022 at 17:36
  • my logic is : if toAddress contain EmailConfigs then eliminate EmailConfigs from toAddress
    – user110654
    Commented Jan 27, 2022 at 17:43
  • @Fabien instead of using List, use Set. Email addresses should be unique and the remove function is available in the System Set class.
    – TSmith
    Commented Jan 28, 2022 at 9:49
  • The issue i'm facing that i couldn't iterate over the emailMessage.ccAddress , globallist contain all email addresses not only the emailConfigs ..
    – user110654
    Commented Jan 28, 2022 at 11:13

You must log in to answer this question.