Please help with the syntax for my message.toAddresses accepts a List. Error message

Initial expression is of incorrect type, expected: String but was: List<String>

My Code:

List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                         FROM Case
                                         WHERE CreatedDate = LAST_N_DAYS:7
                                         GROUP BY AccountId, Account.Name
                                         HAVING COUNT(Id)  >= 8];

                         Set<Id> AcctIds = new Set<Id>();
                for(AggregateResult aggr: AggregateResultList){

                    Id accId = (Id)aggr.get('AccountId');

                List<Case> caseList = [SELECT Id, AccountId, Account.Name,Parent_Project_if_applicable__r.Implementation_status__c,
                                       FROM Case
                                       WHERE AccountId IN :AcctIds];
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            for(Case cl:caseList){ 

                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
                             message.toAddresses = new String[] { getAddresses() }; 
                            message.toAddresses = new String[] { Parent_Project_if_applicable__r.Client_Advisor_Email__c  };
                    message.Subject = 'Subject Test Message';
                    message.PlainTextBody = 'Account name: ' + aggr.get('name') + ' has ' + (Integer)aggr.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){
return (emailString);
  • what line is the error on? – Kris Goncalves Oct 31 '19 at 18:48
  • Line: 25, Column: 56 – ZeroCalm Oct 31 '19 at 18:59
  • Does it work if Implementation_Status__c is not equal to "Live - Closed Project"? – Kris Goncalves Oct 31 '19 at 19:05
  • No, it does not. Same error. Even when I try message.toAddresses = List<String> { getAddresses() }; ' I get the error: Line: 25, Column: 56 Unexpected token 'List'. – ZeroCalm Oct 31 '19 at 19:09

the issue is

message.toAddresses  = new String[] { getAddresses() };

should be

message.toAddresses  = getAddresses(); // returns a list of email addrs  

I believe the issue is the following

message.toAddresses = new String[] { getAddresses() }; 

private List<String> getAddresses()

You're returning a list of email address and trying to insert it into an array as one value. That's why it's expecting a String (only one string per value in the List).

Assign the returned list directly to the toAddresses:

message.ToAddresses = getAddresses();

For the else statement, I think you have a typo, you're missing cl in front of the variable.

message.toAddresses = new String[] { cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c  };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.