+ Start a Discussion
MaggieSumitMaggieSumit 

I have create apex class for Sending emails, but getting error like

Hi All, 
I create apex class for sending email template with attachment. When I uploading data through data loader then I am getting the issue.
Like I am updating a single record than its sending a specific attachment for that contact record, but when I updating data by data loader it's attaching all attachment from all contacts and sending on email. 

please help me on this,

thanks
  1. public with sharing class OpportunityMatchingHelper {
  2.  
  3.     public static List<alu_Opportunity_Matching__c> sendEmail(List<alu_Opportunity_Matching__c> oppList) { 
  4.         Map<Id,alu_Opportunity_Matching__c> OppMapByIDs = new Map<Id,alu_Opportunity_Matching__c>();
  5.         List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
  6.         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
  7.         
  8.         EmailTemplate et=[Select Id,Subject,HtmlValue,Body FROM EmailTemplate WHERE Name=:'Pitched'];          
  9.         for(alu_Opportunity_Matching__c opMap : oppList){
  10.             if(opMap.Application_Status__c == 'Pitched'){
  11.                 OppMapByIDs.put(opMap.Applicant_Student_Record__c, opMap);    
  12.             }        
  13.         }
  14.         for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :OppMapByIDs.keySet()])
  15.         {
  16.            Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  17.            efa.setFileName(a.Name);
  18.            efa.setBody(a.Body);
  19.            fileAttachments.add(efa);
  20.         }
  21.         
  22.         for(alu_Opportunity_Matching__c opp :OppMapByIDs.values()){           
  23.             if(opp.Applicant_Student_Record__c != null){
  24.                 Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
  25.                 singleMail.setTargetObjectId(opp.Applicant_Student_Record__c);
  26.                 singleMail.setTemplateId(et.Id);
  27.                 singleMail.setWhatId(opp.Id);
  28.                 singleMail.setSaveAsActivity(false);
  29.                 singleMail.setReplyTo('mycareer@alueducation.com');
  30.                    singleMail.setSenderDisplayName('ALU Career Development');
  31.                 singleMail.setFileAttachments(fileAttachments);
  32.                 emails.add(singleMail); 
  33.             }
  34.         }  
  35.         Messaging.sendEmail(emails);
  36.         return null;
  37.     }
  38. }
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sumit,
public class testemail
{
private final Contact con;
public testemail(ApexPages.StandardController controller)
{
this.con=(Contact)controller.getRecord();
}

public void SendEmail()
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(con.Id);
mail.setTemplateId('00X90000000QHUD');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Please refer the below link for reference. I hope it will be helpful.

Thanks
Rahul Kumar