+ Start a Discussion
H 007H 007 

Hi, Everyone I am trying to generate a Invoice for opportunity product and also share a same invoice to the opportunity contact email id, when I am trying to run this in bulkify mode then they will send the different multiple invoice to contact email ids

public class OpportunityPdfController {
		
	
    
    @future(callout=true)
      
    public static void pdfGenration(Map<Id, Id> oppIdWithAccId){  
        Set<Id> OppIds=oppIdWithAccId.keySet();
        System.debug('OPPIDS'+ OppIds);

        Map<Id, ContentVersion> contentVersionMap = createContentVersion(OppIds, oppIdWithAccId);
        createContentDocumentLink(contentVersionMap);       
        
    }

    private static Map<Id, ContentVersion> createContentVersion(Set<Id> OppIds, Map<Id, Id> oppIdWithAccId){

        System.debug('OPPIDS2'+ OppIds);
        Map<Id, ContentVersion> contentVersionMap = new Map<Id, ContentVersion>(); 
		Blob body;        
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.EmailFileAttachment> attachList = new List<Messaging.EmailFileAttachment>();
        List<String> sendTo= new List<String>();
        
		List<Contact>  conList=[SELECT Id, AccountId, Email FROM Contact WHERE AccountId IN: oppIdWithAccId.values() AND Email!=Null];
       
        system.debug('conList'+conList );
        
       Map<Id, String> oppIdWithEmail= new Map<Id, String>();  
         
        for(Id OppId: OppIds)
        {		
            PageReference pdf = new PageReference('/apex/InvoicePDF');
            pdf.getParameters().put('Id',OppId);     
            
            ContentVersion cv=new ContentVersion();            
            try{   
                
                body=pdf.getContentAsPDF();
                System.debug('body : ' + body);
                
            }
            catch (Exception e) {
                
                body = Blob.valueOf('Text');
                System.debug(e.getMessage());
            }                     
            cv.PathOnClient= 'Invoice'+'.pdf';            	
            cv.Title= 'Invoice'+' '+ Date.today();
            cv.IsMajorVersion = true;  
            cv.VersionData=body;  
            
            contentVersionMap.put(OppId, cv);    
          
        } 
        if(!contentVersionMap.Values().isEmpty()){      
        
            insert contentVersionMap.Values(); 
            for(Id oid:  contentVersionMap.keySet()){
                
                Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
                attach.setContentType('.pdf/txt');
                attach.setFileName('Invoice.pdf');
                attach.setInline(false);
                attach.Body = contentVersionMap.get(oid).VersionData; 
                attachList.add(attach);
                
            }
            For(Contact mailList: conList){               
          
                
            Messaging.SingleEmailMessage  mail= new Messaging.SingleEmailMessage();
            
            sendTo.add(mailList.Email);
                
            system.debug('EmailList'+ sendTo);
            mail.setToAddresses(sendTo);
            mail.setSubject('Product Invoice');
            mail.setHtmlBody('');
       		mail.setFileAttachments(attachList);            
            mails.add(mail);         
            
        }
          Messaging.sendEmail(mails);            
      
        }return contentVersionMap;      
          
    }

    private static void createContentDocumentLink(Map<Id, ContentVersion> idOppContentVersionMap){

        Map<Id, Id> idConDocMap = new Map<Id, Id>();
        List<ContentDocumentLink> ContentDocumentLinkList = new List<ContentDocumentLink>();

        for(ContentVersion cv : [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:idOppContentVersionMap.Values()])
        {
            idConDocMap.put(cv.Id, cv.ContentDocumentId);
        }

        if(idConDocMap.isEmpty()) return;

        for(Id OppId : idOppContentVersionMap.keySet()){

            ContentVersion contentVersion = idOppContentVersionMap.get(OppId);

            ContentDocumentLink cdl = New ContentDocumentLink();
            cdl.LinkedEntityId = OppId;           
            cdl.ContentDocumentId = idConDocMap.get(contentVersion.Id);           
            cdl.shareType = 'V';
            
            ContentDocumentLinkList.add(cdl);

        }
		if(!ContentDocumentLinkList.isEmpty()){    
       		insert ContentDocumentLinkList;            
        } 
    }   

}