0

I am sending Contact Attachment to Same Contact mail Id for this I have written an apex helper class for sending email template with attachment and Its working but when I doing the same using data loader its attaching all attachment from different record and sending on every mail id.

public with sharing class OpportunityMatchingHelper {

public static List<alu_Opportunity_Matching__c> sendEmail(List<alu_Opportunity_Matching__c> oppList) { 

    Map<Id,alu_Opportunity_Matching__c> OppMapByIDs = new Map<Id,alu_Opportunity_Matching__c>();
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

    EmailTemplate et=[Select Id,Subject,HtmlValue,Body FROM EmailTemplate WHERE Name=:'Pitched'];       
    for(alu_Opportunity_Matching__c opMap : oppList){
        if(opMap.Application_Status__c == 'Pitched'){
            OppMapByIDs.put(opMap.Applicant_Student_Record__c, opMap);  
        }       
    }
    for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :OppMapByIDs.keySet()])
    {
       Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
       efa.setFileName(a.Name);
       efa.setBody(a.Body);
       fileAttachments.add(efa);
    }
    system.debug('fileAttachments'+fileAttachments);
    //Contact con = [SELECT Id, Email FROM Contact WHERE Id IN : OppMapByIDs.keySet()];
    for(alu_Opportunity_Matching__c opp :OppMapByIDs.values()){           
        if(opp.Applicant_Student_Record__c != null){
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
            singleMail.setTargetObjectId(opp.Applicant_Student_Record__c);
            singleMail.setTemplateId(et.Id);
            singleMail.setWhatId(opp.Id);
            singleMail.setSaveAsActivity(false);
            singleMail.setReplyTo('sumit@teqsolution.com');
            singleMail.setSenderDisplayName('Career Development');
            singleMail.setFileAttachments(fileAttachments);
            emails.add(singleMail); 
            system.debug('emails'+emails);
        }
    }  
    system.debug('emails'+emails);
    Messaging.sendEmail(emails);
    return null;
}
}
0

In your code, you are fetching all the attachments and then adding it to a list fileAttachments and then adding it as attachment for the alu_Opportunity_Matching__c record. Create a map to store the attachments with each alu_Opportunity_Matching__c and then fetch the attachments of each alu_Opportunity_Matching__c record using the Map.

public with sharing class OpportunityMatchingHelper {

    public static List<alu_Opportunity_Matching__c> sendEmail(List<alu_Opportunity_Matching__c> oppList) { 

    Map<Id,alu_Opportunity_Matching__c> OppMapByIDs = new Map<Id,alu_Opportunity_Matching__c>();
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

    //List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
    Map<Id, List<Messaging.Emailfileattachment>> fileAttachmentWithOpp = new Map<Id, List<Messaging.Emailfileattachment>>(); 

    EmailTemplate et=[Select Id,Subject,HtmlValue,Body FROM EmailTemplate WHERE Name=:'Pitched'];       
    for(alu_Opportunity_Matching__c opMap : oppList){
        if(opMap.Application_Status__c == 'Pitched'){
            OppMapByIDs.put(opMap.Applicant_Student_Record__c, opMap);  
        }       
    }
    for (Attachment a : [select Name, Body, BodyLength, ParentId from Attachment where ParentId = :OppMapByIDs.keySet()]){
       Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
       efa.setFileName(a.Name);
       efa.setBody(a.Body);
       if(fileAttachmentWithOpp.containsKey(a.ParentId)){
           List<Messaging.Emailfileattachment> listOfAttachments = fileAttachmentWithOpp.get(a.ParentId);
           listOfAttachments.add(efa);
           fileAttachmentWithOpp.put(a.ParentId, listOfAttachments);
       } else {
           fileAttachmentWithOpp.put(a.ParentId, new List<Messaging.Emailfileattachment>{efa});
       }   
    }
    //system.debug('fileAttachments'+fileAttachments);
    //Contact con = [SELECT Id, Email FROM Contact WHERE Id IN : OppMapByIDs.keySet()];
    for(alu_Opportunity_Matching__c opp :OppMapByIDs.values()){           
        if(opp.Applicant_Student_Record__c != null){
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
            singleMail.setTargetObjectId(opp.Applicant_Student_Record__c);
            singleMail.setTemplateId(et.Id);
            singleMail.setWhatId(opp.Id);
            singleMail.setSaveAsActivity(false);
            singleMail.setReplyTo('sumit@teqforcesolution.com');
            singleMail.setSenderDisplayName('ALU Career Development');
            if(fileAttachmentWithOpp.containskey(opp.Id)){
                singleMail.setFileAttachments(fileAttachmentWithOpp.ge‌​t(opp.Applicant_Stud‌​ent_Record__c));

            }
            emails.add(singleMail);
            system.debug('emails'+emails);
       }
   }  
   system.debug('emails'+emails);
   Messaging.sendEmail(emails);
   return null;
   }
}

Let me know if it works for you.

|improve this answer|||||
1

Hey You are attaching all the files with every email that is why it is going with every email. below is the modified code for this Please add attachment inner query in oppList

public with sharing class OpportunityMatchingHelper {

public static List<alu_Opportunity_Matching__c> sendEmail(List<alu_Opportunity_Matching__c> oppList) { 

    Map<Id,alu_Opportunity_Matching__c> OppMapByIDs = new Map<Id,alu_Opportunity_Matching__c>();
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

    EmailTemplate et=[Select Id,Subject,HtmlValue,Body FROM EmailTemplate WHERE Name=:'Pitched'];       
    for(alu_Opportunity_Matching__c opMap : oppList){
        if(opMap.Application_Status__c == 'Pitched'){
            OppMapByIDs.put(opMap.Applicant_Student_Record__c, opMap);  
        }       
    }

    //Contact con = [SELECT Id, Email FROM Contact WHERE Id IN : OppMapByIDs.keySet()];
    for(alu_Opportunity_Matching__c opp :OppMapByIDs.values()){           
        if(opp.Applicant_Student_Record__c != null){
            List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            for(attachment attach : opp.attachments){
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setFileName(attach.Name);
                efa.setBody(attach.Body);
                fileAttachments.add(efa);
            }
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
            singleMail.setTargetObjectId(opp.Applicant_Student_Record__c);
            singleMail.setTemplateId(et.Id);
            singleMail.setWhatId(opp.Id);
            singleMail.setSaveAsActivity(false);
            singleMail.setReplyTo('sumit@teqforcesolution.com');
            singleMail.setSenderDisplayName('ALU Career Development');
            if(fileAttachments != null && fileAttachments.size() > 0){
                singleMail.setFileAttachments(fileAttachments);
            }
            emails.add(singleMail); 
            system.debug('emails'+emails);
        }
    }  
    system.debug('emails'+emails);
    Messaging.sendEmail(emails);
    return null;
}
}
|improve this answer|||||
  • Thanks @Rishi Golyan , But getting error on Variable does not exist: a.Name efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); – Maddy Jul 5 '17 at 8:15
  • Edited please check. – Rishi Golyan Jul 5 '17 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.