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
SayaSaya 

Send Mass Email from a custom Object

Hi good People,

 

Am trying to implement Sending Mass email on a Candidate Custom Object. I understand the standard Messaging.MassEmailMessage is not supported by custom objects.How do i get to use Messaging.SingleEmailMessage to send mass email by looping through the selected records?

 

 

public class WrapperMassEmail {

public String aMessage {
        get; set;   
    }
  
    public List<string> candidateList1=new List<string>();
        
    //Our collection of the class/wrapper objects cCandidate 
    public List<cCandidate> candidateList {get; set;}
     
    //This method uses a simple SOQL query to return a List of Candidates
    public List<cCandidate> getCandidates(){
        if(candidateList == null){
            candidateList = new List<cCandidate>();
        
            for(Candidates__c c : [select Id, First_Name__c,Name, Main_Email__c from Candidates__c limit 10]){
                
                candidateList.add(new cCandidate(c));
            }
        }
        return candidateList;
    }
    
    public PageReference processSelected(){
    
        /*We create a new list of Candidates that we be populated only with Candidates
        if they are selected*/
        List<Candidates__c> selectedCandidates = new List<Candidates__c>();
           this.aMessage = '';
           
        /*We will cycle through our list of cCandidate and will check to see if the 
        selected property is set to true, if it is we add the Contact to the 
        selectedCandidates list. */
        for(cCandidate cCon : getCandidates()){
            if(cCon.selected == true){
                selectedCandidates.add(cCon.con);
            }
        }
        
        
        /* Now we have our list of selected candidates and can perform any type of 
        logic we want, sending emails, updating a field on the Contact, etc */
        System.debug('These are the selected Candidates...');
        Integer numselectedCandidates = selectedCandidates.size();
        Integer counter = 0;
        System.Debug(selectedCandidates);
        for(Candidates__c con : selectedCandidates){
            counter++;
    
           
                if(counter==numselectedCandidates) {
                    this.aMessage += con.Id + ','+ con.First_Name__c+',' + con.Main_Email__c;
                    candidateList1.add(con.Id);
                } else {
                    this.aMessage += con.Id + ','+con.First_Name__c+','+ con.Main_Email__c+', ';
                    candidateList1.add(con.Id);
                }
            
        }
       return null;
      
        
       
    }
    
    /* This is our wrapper/container class. A container class is a class, a data 
    structure, or an abstract data type whose instances are collections of other 
    objects. In this example a wrapper class contains both the standard salesforce 
    object Contact and a Boolean value */
    public class cCandidate
    {
       public Candidates__c con {get; set;}
        public Boolean selected {get; set;}
        
        /*This is the contructor method. When we create a new cCandidate object we pass a 
        candi that is set to the con property. We also set the selected value to false*/
      public cCandidate(Candidates__c c){
            con = c;
            selected = false;
            
        }
    }
    
   public PageReference send()
        {
        
                 
    
EmailTemplate eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ];
//Trying to get the ids of the selected candidates     
List<Candidates__c> cand = [Select Id,Name,First_Name__c,Main_Email__c from Candidates__c where Id = :];
            
              String[] address = new String[] {
                    cand[0].Main_Email__c
                    };
             
           
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            email.setTemplateId(eTemplate.id);
            email.setToAddresses(address);
            email.setBccSender(true);
            email.setSaveAsActivity(true);
                     
                         
         PageReference Page = new PageReference('/' + Schema.getGlobalDescribe().get('Candidates__c').getDescribe().getKeyPrefix() + '/o');
                    Page.setRedirect(true);
                    return Page;
        
        }
        
        }

 

mail.setBccSender(true);
                mail.setSaveAsActivity(true);



Shashikant SharmaShashikant Sharma

You can only send Mass Email Messages to Contact, Lead or User records via Apex. You can only send Single Email Messages to an email address stored on a custom object. But as I see your code I see that You have merge fields to be used in Email Template so there are two options for You

 

1) You should fetch Email Template using SOQL and use some signs like #FieldAPIName#  in template and replace it with field value in Apex

 

Ex

 

List<EmailTemplate> emailTemp =[Select Body,Subject From EmailTemplate where DeveloperName='Interview_Client_Email'];
if (emailTemp.size() > 0 && emailTemp.get(0).Body != null)
             {
               
                 body = emailTemp.get(0).Body;
                 body = body.replace('#FieldAPIName#', recordValue);
                }
email.setPlainTextBody(body);

 

 

2)Remove this code to send mail ,

    You can just use a CheckBox field in that object SendMAIL__C and set it to true in apex and create a workflow and email alert on that object . With codition for the CheckBox field.

 

Let me know if any issues in it

SayaSaya

Hi Shashikant,

Thanks for your reply. However am not really sure i follow on your second solution. Could you kindly give me a clear guide on how this can be achieved on my Candidate__c object.