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
Morino Italis 8Morino Italis 8 

Trigger to Send Email with Attachment

I have trigger which send an attchement to a recipient email when  a field called Percent_distributed__c of an object called Container__c is 100%.  It worked fine.  A new field called Center__c was added to the same object and my new requirement is 
-  send the email to a recipient email A, if the Center is X, 
-  send the email to a recipient email B, if the Center is Y, 
-  send the email to a recipient email C, if the Center is Z, 

in the code below, i get an error saying  : 
 Compile Error: Extra ';', at '}'. at line 89 column 85

Here is my Code
public class DetailedDistributionTrigger
{
    public static void afterUpdateOperation(List<Container__c> lstNewContainers, Map<Id, Container__c> mapOldContainers)
    {
        List<String> lstUpdatedContainerIds = new List<String>();
       
        for(Container__c newContainer : lstNewContainers)
        {
            Container__c oldContainer = new Container__c();
           
            oldContainer = mapOldContainers.get(newContainer.Id);
           
            if(!Test.isRunningTest())
            {
                if(newContainer.Percent_Distributed__c == 100 && oldContainer.Percent_Distributed__c != 100)
                {
                    lstUpdatedContainerIds.add(newContainer.Id);
                }
            }
            else
                lstUpdatedContainerIds.add(newContainer.Id);
        }
       
        if(!lstUpdatedContainerIds.isEmpty())
            sendContainerPDF(String.join(lstUpdatedContainerIds,','));
    }
   
    @future(callout = true)
    public static void sendContainerPDF(String strUpdatedContainers)
    {
        List<String> lstContainerIds = new List<String>();
        lstContainerIds = strUpdatedContainers.split(',');
       
        List<Container__c> lstContainers = new List<Container__c>();
        
        if(!lstContainerIds.isEmpty())
        {
            lstContainers = [SELECT Id,Name, Owner.Email FROM Container__c WHERE Id IN :lstContainerIds];
        }
       
        for(Container__c Container :lstContainers)
        {
           // if(Container.Requisitor__c != null && Container.Requisitor__r.Contact_email__c != null)
           // {
            //    sendpdfEmail(Container,Container.Requisitor__r.Contact_email__c);
          //  }
           
          if(Container.Owner.Email != null)
           {
              sendpdfEmail(Container, Container.Owner.Email);
           }
        } 
    }
   
    public static void sendpdfEmail(Container__c Container, String emailid)
    {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 
        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.ContainerDetailedDistributionExcel;
        pdf.getParameters().put('id',Container.Id);
        pdf.setRedirect(true);
        Blob pdfData;
       
        // Take the PDF content
        if(!Test.isRunningTest())
        {
            pdfData = pdf.getContent();
        }
        else
        {
            pdfData = Blob.valueOf('test');
        }
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(Container.Name + '.xls');
        efa.setBody(pdfData);
 
        //String[] toAddresses = new List<String> {emailId};
        //email addresses to send email
            String[] toAddresses = new List<String>{'capcontainerdistribrep@foodforthepoorhaiti.org'};
            String[] toCanteenAddresses = new List<String>{'crndistributionreport@foodforthepoorhaiti.org'};
            String[] toCrnAddresses = new List<String>{'bcldistributionreport@foodforthepoorhaiti.org'};
 
        // Sets the paramaters of the email
       
        email.setSubject(Container.Name +' Detailed Distribution Report');
//Line 85

        if(COntainer.FFP_Center__c =='FFP Nord'){
           email.setToAddresses( toAddresses )
        };
        email.setPlainTextBody('The Container ' + Container.Name +  ' has been totally distributed, Please find attached the detailed distribution report' );
 
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
 
        // Sends the email
        Messaging.SendEmailResult [] r =
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}


 
Raj VakatiRaj Vakati
below lines was wrong 
 
if(COntainer.FFP_Center__c =='FFP Nord'){
           email.setToAddresses( toAddresses )
        };


Use this code


 
public class DetailedDistributionTrigger
{
    public static void afterUpdateOperation(List<Container__c> lstNewContainers, Map<Id, Container__c> mapOldContainers)
    {
        List<String> lstUpdatedContainerIds = new List<String>();
       
        for(Container__c newContainer : lstNewContainers)
        {
            Container__c oldContainer = new Container__c();
           
            oldContainer = mapOldContainers.get(newContainer.Id);
           
            if(!Test.isRunningTest())
            {
                if(newContainer.Percent_Distributed__c == 100 && oldContainer.Percent_Distributed__c != 100)
                {
                    lstUpdatedContainerIds.add(newContainer.Id);
                }
            }
            else
                lstUpdatedContainerIds.add(newContainer.Id);
        }
       
        if(!lstUpdatedContainerIds.isEmpty())
            sendContainerPDF(String.join(lstUpdatedContainerIds,','));
    }
   
    @future(callout = true)
    public static void sendContainerPDF(String strUpdatedContainers)
    {
        List<String> lstContainerIds = new List<String>();
        lstContainerIds = strUpdatedContainers.split(',');
       
        List<Container__c> lstContainers = new List<Container__c>();
        
        if(!lstContainerIds.isEmpty())
        {
            lstContainers = [SELECT Id,Name, Owner.Email FROM Container__c WHERE Id IN :lstContainerIds];
        }
       
        for(Container__c Container :lstContainers)
        {
           // if(Container.Requisitor__c != null && Container.Requisitor__r.Contact_email__c != null)
           // {
            //    sendpdfEmail(Container,Container.Requisitor__r.Contact_email__c);
          //  }
           
          if(Container.Owner.Email != null)
           {
              sendpdfEmail(Container, Container.Owner.Email);
           }
        }  
    }
   
    public static void sendpdfEmail(Container__c Container, String emailid)
    {
        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
 
        // Reference the attachment page and pass in the account ID
        PageReference pdf =  Page.ContainerDetailedDistributionExcel;
        pdf.getParameters().put('id',Container.Id); 
        pdf.setRedirect(true);
        Blob pdfData;
       
        // Take the PDF content
        if(!Test.isRunningTest())
        {
            pdfData = pdf.getContent();
        }
        else
        {
            pdfData = Blob.valueOf('test');
        }
        // Create the email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(Container.Name + '.xls');
        efa.setBody(pdfData);
 
        //String[] toAddresses = new List<String> {emailId};
        //email addresses to send email
            String[] toAddresses = new List<String>{'capcontainerdistribrep@foodforthepoorhaiti.org'};
            String[] toCanteenAddresses = new List<String>{'crndistributionreport@foodforthepoorhaiti.org'};
            String[] toCrnAddresses = new List<String>{'bcldistributionreport@foodforthepoorhaiti.org'};
 
        // Sets the paramaters of the email
       
        email.setSubject(Container.Name +' Detailed Distribution Report');
        if(COntainer.FFP_Center__c =='FFP Nord'){
           email.setToAddresses( toAddresses );
        }
        email.setPlainTextBody('The Container ' + Container.Name +  ' has been totally distributed, Please find attached the detailed distribution report' );
 
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
 
        // Sends the email
        Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}