function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
smitha vikramsmitha vikram 

how to send an email when contact campaign member or lead gets updated

I have written a code where when a lead or contact is added to campaign, i need to check on some coditon and if they satisfy send out a survey link as an email and after that update last survey date field and type to a specific value. This is what i have so far, but not sure how to add email code without hitting email limit....
public class CampaignMembers_Hanlder {
    
    public static void getCampaignMembers(List<CampaignMember> campaignMember) {
        
        Set<id> setOfContactIds = new Set<id>();
        Set<id> setOfLeadIds= new Set<id>();
        
        if(campaignMember!=null){
            for(CampaignMember varCampaignMember: campaignMember){
                if(varCampaignMember.ContactId!=null){
                    
                    setOfContactIds.add(varCampaignMember.ContactId);
                }
                
                if(varCampaignMember.leadId!=null){
                    setOfLeadIds.add(varCampaignMember.leadId);
                    
                }
            }
            
            for(Contact varContactRecord:[Select id, HasOptedOutOfEmail,Last_Survey_Sent_Date__c,account.Licensed_Mainframe__c,
                                          Last_Survey_Sent_Type__c from contact where id IN:setOfContactIds])
            {
                If(varContactRecord.HasOptedOutOfEmail==false && varContactRecord.account.Licensed_Mainframe__c =='Active'){
                    
                    
                    if(varContactRecord.Last_Survey_Sent_Date__c!=null && varContactRecord.Last_Survey_Sent_Date__c>= system.today()-60){
                        
                        
                        String[] toAddresses = new String[] {'user@acme.com'};
                            
                            }
                    
                }
                
            }        
            
            for(Lead varLeadRecord: [Select id, HasOptedOutOfEmail, Last_Survey_Sent_Date__c, 
                                     Last_Survey_Sent_Type__c, SFDC_Account__r.Licensed_Mainframe__c from Lead WHERE id IN:setOfLeadIds]){
                                         
                                         
                                         If(varLeadRecord.HasOptedOutOfEmail==false && varLeadRecord.SFDC_Account__r.Licensed_Mainframe__c =='Active'){
                                             
                                             
                                             if(varLeadRecord.Last_Survey_Sent_Date__c!=null && varLeadRecord.Last_Survey_Sent_Date__c>= system.today()-60){
                                                 
                                                 
                                                 String[] toAddresses = new String[] {'user@acme.com'};
                                                     
                                                     }
                                             
                                         }
                                         
                                     }    
            
        }
    }
    
}