+ Start a Discussion
Tim Smith 58Tim Smith 58 

Sending email - Illegal Assignment from String to List<String>

This Trigger will send an email alert when an account has 8 cases created within 7 days.  Depending on the status of `cl.Parent_Project_if_applicable__r.Implementation_status__c`
The destination of that email will go one of two places.

In the Else block, I am assigning the value `clientEmail` which is only one email address not a List.  I am receiving the error:  

`Line: 66, Column: 9
Illegal assignment from String to List<String>`

HOw do I assign the value to the `mail.toAddresses`?
 
String messageToSend;
   				List<String> ListOfMessages = new List<String>();
   		     	Set<Id> AcctIds = new Set<Id>();
				List<String> clientEmail;
				List<String> emailAdds = getAddresses();

List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                             FROM Case
                                             WHERE CreatedDate = LAST_N_DAYS:8
                                             GROUP BY AccountId, Account.Name
                                             HAVING COUNT(Id)  = 7];
    		 
            Map<Id, String> accountIdEmailmessageMap = new Map<Id, String>();
            
				for(AggregateResult aggr: AggregateResultList){
                String messageToSend = 'Account name: ' + aggr.get('name') +
                    ' has ' + (Integer)aggr.get('co') +
                    ' cases opened in the last 8 days.';
                Id accId = (Id)aggr.get('AccountId');
                accountIdEmailmessageMap.put(accId,messageToSend);
                AcctIds.add(accId); 
            }
            
			
List<Case> caseList = [SELECT Id, AccountId, Account.Name,Parent_Project_if_applicable__r.Implementation_status__c,
                       Parent_Project_if_applicable__r.PM_Implementation_Status__c, 
                       Parent_Project_if_applicable__r.RCM_Implementation_Status__c,
                       Parent_Project_if_applicable__r.Client_Advisor_Email__c
                       FROM Case
                       WHERE AccountId IN :AcctIds];

  

for(Case cl:caseList){ 
   
    
    String clientEmail = cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c;
   
    if(cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project'){    
        //Private method *** getAddresses() *** retrieves email address from Customer_Success_Managers Public Group
			String messageBody = accountIdEmailmessageMap.get(cl.AccountId);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
		mail.setToAddresses(emailAdds);
		mail.setSenderDisplayName('IT Support'); 
        mail.Subject = 'Multiple cases created alert message';
        mail.setPlainTextBody(messageBody);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        
        System.debug('Imp ' + cl.Parent_Project_if_applicable__r.Implementation_status__c +
                     ' PM ' + cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c + 
                      ' RCM ' + cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c +
                       ' Email ' + cl.Parent_Project_If_Applicable__r.Client_Advisor_Email__c);
          
    }
    else{  
             
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	   // mail.SenderDisplayName('Salesforce Support');
        mail.toAddresses = clientEmail;
        mail.Subject = 'Subject Test Message';
        mail.setPlainTextBody(messageToSend);
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
        
       // System.debug('in the loop: ' + messageToSend);
         
    }

    //	message.Subject = 'Subject Test Message';
    //    message.PlainTextBody = 'Account name: ' + cl.get('name') + ' has ' + (Integer)cl.get('co') + ' cases opened in the last 8 days.';
    //    Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
    //    Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
}


private List<String> getAddresses(){
    List<User> UserList =
        [SELECT id, name, email
         FROM User 
         WHERE id 
         IN (SELECT userorgroupid 
             FROM groupmember
             WHERE group.name = 'Customer Success Managers')];
    
    List<String> emailString = new List<String>();
    
    for(User u: UserList){
        emailstring.add(u.email);
    }   
    return (emailString);
    //System.debug(emailString);
}

 
Best Answer chosen by Tim Smith 58
Deepali KulshresthaDeepali Kulshrestha
Hi Tim,

According to my understanding this will be helpful for you.

You can use this line:-
List<String> sendTo = new List<String>();
sendTo.add(cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c);

Intead of:-
String clientEmail = cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.SenderDisplayName('Salesforce Support');

//Comment this line:-
mail.toAddresses = clientEmail;

//And

//Use this line:-
mail.setToAddresses(sendTo);

mail.Subject = 'Subject Test Message';
mail.setPlainTextBody(messageToSend);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

and so on...

OR

You can use this line:-
List<String> clientEmail = new List<String>();
clientEmail.add(cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c);

Intead of:-
String clientEmail = cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.SenderDisplayName('Salesforce Support');

//Comment below line:-
mail.toAddresses = clientEmail;

//And

//Use below line:-
mail.setToAddresses(clientEmail);

mail.Subject = 'Subject Test Message';
mail.setPlainTextBody(messageToSend);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

and so on...

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Tim Smith 58Tim Smith 58
mail.setToAddresses(new String[] { clientEmail });
Deepali KulshresthaDeepali Kulshrestha
Hi Tim,

According to my understanding this will be helpful for you.

You can use this line:-
List<String> sendTo = new List<String>();
sendTo.add(cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c);

Intead of:-
String clientEmail = cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.SenderDisplayName('Salesforce Support');

//Comment this line:-
mail.toAddresses = clientEmail;

//And

//Use this line:-
mail.setToAddresses(sendTo);

mail.Subject = 'Subject Test Message';
mail.setPlainTextBody(messageToSend);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

and so on...

OR

You can use this line:-
List<String> clientEmail = new List<String>();
clientEmail.add(cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c);

Intead of:-
String clientEmail = cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.SenderDisplayName('Salesforce Support');

//Comment below line:-
mail.toAddresses = clientEmail;

//And

//Use below line:-
mail.setToAddresses(clientEmail);

mail.Subject = 'Subject Test Message';
mail.setPlainTextBody(messageToSend);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

and so on...

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer