+ Start a Discussion
Timothy SmithTimothy Smith 

Trigger sending email, ToAddresses depend on conditional

I need to send an email to one of two Public Groups depending on the value of the 3 fields in the `If Statement`. Within the the If and Else blocks I have a `String mailGroup` with the value of the Public Class.Now that I need the Else statement to access that private class with a different Public class name, I am having issues inserting `String mailGroup` , into the Private class `getAddresses()`. The Variable `emailAdds` declared at the top holds the email address values from the `getAddresses()` function. Is it my syntax? I am receiving numerous errors:

`Unexpected token 'private'`
 `Unexpected token '<'`
`Unexpected token '>'`

`Method does not exist or incorrect signature: void getAddresses(String) from type CaseHandlerCountAlert`

 
trigger CaseHandlerCountAlert on Case (after insert) {
    
 				String messageToSend;
   				List<String> ListOfMessages = new List<String>();
   		     	Set<Id> AcctIds = new Set<Id>();
				List<String> clientEmail;
				String messageBody;
    			String mailGroup;
    			List<String> emailAdds = getAddresses(mailGroup);
    
List<AggregateResult> AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
                                             FROM Case
                                             WHERE CreatedDate = LAST_N_DAYS:1 AND 
                                             Id IN :Trigger.New
                                             GROUP BY AccountId, Account.Name
                                             HAVING COUNT(Id)  >= 5 ];
    		 
            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){ 
  
    if(cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project'||
       cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c == 'Live - Closed Project'||
       cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c == 'Live - Closed Project'){    
        	
           	
            mailGroup = 'Customer Success Managers'; 
           
	String messageBody = accountIdEmailmessageMap.get(cl.AccountId);
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	  mail.setSenderDisplayName('IT Support'); 
          mail.setToAddresses(emailAdds);
          mail.Subject = 'Multiple cases created alert message';
          mail.setPlainTextBody(messageBody);
           
         if(messageToSend != null){
          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
          }
        
    }
    else{  
            
      			
         mailGroup = 'BI Team';
         
        
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	  mail.SetSenderDisplayName('IT Support');
          mail.setToAddresses(emailAdds);
          mail.Subject = 'Multiple cases created alert message';
          mail.setPlainTextBody(messageToSend);
      
        
        if(messageToSend != null){
          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
       
         
    }

    


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

               }

 
Best Answer chosen by Timothy Smith
Colton WehkingColton Wehking
Sorry, looks like I mistakenly placed the getAddresses function inside the for loop. This should fix that issue. 
trigger CaseHandlerCountAlert on Case(after insert) {

    String messageToSend;
    List < String > ListOfMessages = new List < String > ();
    Set < Id > AcctIds = new Set < Id > ();
    List < String > clientEmail;
    String messageBody;
    List < String > emailAddsCSM = getAddresses('Customer Success Managers');
	List < String > emailAddsBIT = getAddresses('BI Team');

    List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
        FROM Case
        WHERE CreatedDate = LAST_N_DAYS: 1 AND
        Id IN: Trigger.New
        GROUP BY AccountId, Account.Name
        HAVING COUNT(Id) >= 5
    ];

    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) {

        if (cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project' ||
            cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c == 'Live - Closed Project' ||
            cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c == 'Live - Closed Project') {

            String messageBody = accountIdEmailmessageMap.get(cl.AccountId);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('IT Support');
            mail.setToAddresses(emailAddsCSM);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageBody);

            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }

        } else {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.SetSenderDisplayName('IT Support');
            mail.setToAddresses(emailAddsBIT);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageToSend);


            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }
        }
    }


    private List < String > getAddresses(String mailGroup) {
        List < User > UserList = [SELECT id, name, email
            FROM User
            WHERE id
            IN(SELECT userorgroupid FROM groupmember WHERE group.name =: mailGroup)
        ];

        List < String > emailString = new List < String > ();

        for (User u: UserList) {
            emailstring.add(u.email);
        }
        System.debug(emailString);
        return (emailString);
    }

}

 

All Answers

Colton WehkingColton Wehking
Hi Timothy, the parameter for your getAddresses function doesn't have a type. Adding String should fix the issue
 
private List<String> getAddresses(String mailGroup){
...
}

Feel free to follow up if any other errors occur
Soyab HussainSoyab Hussain
Hi Timothy Smith,

Colton Wehking is correct you just need to add String before mailGroup in getAddresses method like this.

private List<String> getAddresses(String mailGroup){}
Regards,
Soyab
Timothy SmithTimothy Smith
@Colton Wehking,
Thank you for your response.  When I change the line of code to meet your's I no longer receive the error messages.  However, after the code change, I am receiving `null` when it comes to the public group.  I have verified the group name and tried other group names with same result.  I appreciate any other insight you may offer.  Also if I try to `System.debug(emailString)` at the end of the getAddresses function, I get the error `Unreachable Statement`.   Thank you again, for your assistance.
Colton WehkingColton Wehking
It looks like the null public group is because on line 8 mailGroup is not defined yet, then the getAddresses() function is called, and then not called again. The 'Unreachable Statement' is because anything after the final return statement will never be reached, so putting it before should fix that issue. Here is the changed code if you wanted to give this a try
trigger CaseHandlerCountAlert on Case(after insert) {

    String messageToSend;
    List < String > ListOfMessages = new List < String > ();
    Set < Id > AcctIds = new Set < Id > ();
    List < String > clientEmail;
    String messageBody;
    String mailGroup;
    List < String > emailAdds;

    List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
        FROM Case
        WHERE CreatedDate = LAST_N_DAYS: 1 AND
        Id IN: Trigger.New
        GROUP BY AccountId, Account.Name
        HAVING COUNT(Id) >= 5
    ];

    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) {

        if (cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project' ||
            cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c == 'Live - Closed Project' ||
            cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c == 'Live - Closed Project') {


            mailGroup = 'Customer Success Managers';
            emailAdds = getAddresses(mailGroup);
            String messageBody = accountIdEmailmessageMap.get(cl.AccountId);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('IT Support');
            mail.setToAddresses(emailAdds);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageBody);

            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }

        } else {


            mailGroup = 'BI Team';
            emailAdds = getAddresses(mailGroup);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.SetSenderDisplayName('IT Support');
            mail.setToAddresses(emailAdds);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageToSend);


            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }
        }
    }


    private List < String > getAddresses(String mailGroup) {
        List < User > UserList = [SELECT id, name, email
            FROM User
            WHERE id
            IN(SELECT userorgroupid FROM groupmember WHERE group.name =: mailGroup)
        ];

        List < String > emailString = new List < String > ();

        for (User u: UserList) {
            emailstring.add(u.email);
        }
        System.debug(emailString);
        return (emailString);
    }

}

 
Timothy SmithTimothy Smith
@Colton Wehking, thank you for help so far.  I have tried the code above and I am receiving error message: `Line: 86, Column: 1
System.LimitException: Too many SOQL queries: 101`.   I have even tried by changing the criteria where only 1 record comes through the Trigger.new and still receive the same error.
Timothy SmithTimothy Smith
I added some System.debug lines and my `line 86` reads:  `List < User > UserList = [SELECT id, name, email`
Colton WehkingColton Wehking
Sorry, looks like I mistakenly placed the getAddresses function inside the for loop. This should fix that issue. 
trigger CaseHandlerCountAlert on Case(after insert) {

    String messageToSend;
    List < String > ListOfMessages = new List < String > ();
    Set < Id > AcctIds = new Set < Id > ();
    List < String > clientEmail;
    String messageBody;
    List < String > emailAddsCSM = getAddresses('Customer Success Managers');
	List < String > emailAddsBIT = getAddresses('BI Team');

    List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co
        FROM Case
        WHERE CreatedDate = LAST_N_DAYS: 1 AND
        Id IN: Trigger.New
        GROUP BY AccountId, Account.Name
        HAVING COUNT(Id) >= 5
    ];

    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) {

        if (cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project' ||
            cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c == 'Live - Closed Project' ||
            cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c == 'Live - Closed Project') {

            String messageBody = accountIdEmailmessageMap.get(cl.AccountId);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('IT Support');
            mail.setToAddresses(emailAddsCSM);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageBody);

            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }

        } else {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.SetSenderDisplayName('IT Support');
            mail.setToAddresses(emailAddsBIT);
            mail.Subject = 'Multiple cases created alert message';
            mail.setPlainTextBody(messageToSend);


            if (messageToSend != null) {
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {
                    mail
                });
            }
        }
    }


    private List < String > getAddresses(String mailGroup) {
        List < User > UserList = [SELECT id, name, email
            FROM User
            WHERE id
            IN(SELECT userorgroupid FROM groupmember WHERE group.name =: mailGroup)
        ];

        List < String > emailString = new List < String > ();

        for (User u: UserList) {
            emailstring.add(u.email);
        }
        System.debug(emailString);
        return (emailString);
    }

}

 
This was selected as the best answer
Timothy SmithTimothy Smith
Thanks for all the assistance.  I walked into the office and of course they changed requirements.  No more need to access any Public Groups...of course...