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
H 007H 007 

Hi Everyone, I need to bulkify my apex trigger code, can anyone help me..

Trigger:
trigger PDFGenrate on Opportunity (after insert, after update) {
	 
   Set<Id> OppIds= new Set<Id>();
    
    for(Opportunity opp: trigger.new){        
        if(opp.StageName=='Closed Won'){            
         OppIds.add(Opp.Id);             
        } 
       }OpportunityPdfController.pdfGenration(OppIds);      
}

Trigger Handler: 
public class OpportunityPdfController {
    
    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){
        List<Sobject> attachmentlist = new List<Sobject>();
        List<Sobject> ContentDocLink = new List<Sobject>();
        for(Id OppId: OppIds){

            Blob body;
            
                    PageReference pdf = new  PageReference('/apex/PrintPDF');
                    pdf.getParameters().put('Id',OppId);     
                  
                    ContentVersion cv=new ContentVersion();            
                    try{                
                        body=pdf.getContentAsPDF();
                    }catch (Exception e) {
                        body = Blob.valueOf('Text');
                        system.debug(e.getMessage());
                    }                     
                    cv.PathOnClient= 'Invoice'+'.pdf';            	
                    cv.Title= 'Invoice';
                    cv.IsMajorVersion = true;  
            		cv.VersionData=body;
                    attachmentlist.add(cv);     
            
        //   }     
     
     //  if(attachmentlist.size()>0){
            insert  attachmentlist;              
            
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            system.debug(conDocId);
            ContentDocumentLink cdl = New ContentDocumentLink();

            cdl.LinkedEntityId = OppId;
            
            cdl.ContentDocumentId = conDocId;
            
            cdl.shareType = 'V';
            
        ContentDocLink.add(cdl);
            insert ContentDocLink;
            
        }
        
    }
}

 
Best Answer chosen by H 007
Abdul KhatriAbdul Khatri
Hi Harsh,

Please use the code below bulkified. The only change I made instead of Attachment, I used ContentVersion. 
 
public class OpportunityPdfController {
    
    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){
        //List<Sobject> attachmentlist = new List<Sobject>();
        //List<Sobject> ContentDocLink = new List<Sobject>();

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

        /*for(Id OppId: OppIds){

            Blob body;
            
            PageReference pdf = new  PageReference('/apex/PrintPDF');
            pdf.getParameters().put('Id',OppId);     
            
            ContentVersion cv=new ContentVersion();            
            try{                
                body=pdf.getContentAsPDF();
            }catch (Exception e) {
                body = Blob.valueOf('Text');
                system.debug(e.getMessage());
            }                     
            cv.PathOnClient= 'Invoice'+'.pdf';            	
            cv.Title= 'Invoice';
            cv.IsMajorVersion = true;  
            cv.VersionData=body;
            attachmentlist.add(cv);     
            
        //   }     
        
        //  if(attachmentlist.size()>0){
            insert  attachmentlist;              
            
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            system.debug(conDocId);
            ContentDocumentLink cdl = New ContentDocumentLink();

            cdl.LinkedEntityId = OppId;
            
            cdl.ContentDocumentId = conDocId;
            
            cdl.shareType = 'V';
            
            ContentDocLink.add(cdl);
            insert ContentDocLink;
            
        }*/
        
    }


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

        Map<Id, ContentVersion> contentVersionMap = new Map<Id, ContentVersion>(); 

        Blob body;

        for(Id OppId: OppIds)
        {
            PageReference pdf = new PageReference('/apex/PrintPDF');
            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';
            cv.IsMajorVersion = true;  
            cv.VersionData=body;  

            contentVersionMap.put(OppId, cv);  
        } 

        insert contentVersionMap.Values();

        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);

        }

        insert ContentDocumentLinkList;
        
    }
}
Let me know.
 

All Answers

Abdul KhatriAbdul Khatri
Hi Harsh,

Please use the code below bulkified. The only change I made instead of Attachment, I used ContentVersion. 
 
public class OpportunityPdfController {
    
    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){
        //List<Sobject> attachmentlist = new List<Sobject>();
        //List<Sobject> ContentDocLink = new List<Sobject>();

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

        /*for(Id OppId: OppIds){

            Blob body;
            
            PageReference pdf = new  PageReference('/apex/PrintPDF');
            pdf.getParameters().put('Id',OppId);     
            
            ContentVersion cv=new ContentVersion();            
            try{                
                body=pdf.getContentAsPDF();
            }catch (Exception e) {
                body = Blob.valueOf('Text');
                system.debug(e.getMessage());
            }                     
            cv.PathOnClient= 'Invoice'+'.pdf';            	
            cv.Title= 'Invoice';
            cv.IsMajorVersion = true;  
            cv.VersionData=body;
            attachmentlist.add(cv);     
            
        //   }     
        
        //  if(attachmentlist.size()>0){
            insert  attachmentlist;              
            
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
            system.debug(conDocId);
            ContentDocumentLink cdl = New ContentDocumentLink();

            cdl.LinkedEntityId = OppId;
            
            cdl.ContentDocumentId = conDocId;
            
            cdl.shareType = 'V';
            
            ContentDocLink.add(cdl);
            insert ContentDocLink;
            
        }*/
        
    }


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

        Map<Id, ContentVersion> contentVersionMap = new Map<Id, ContentVersion>(); 

        Blob body;

        for(Id OppId: OppIds)
        {
            PageReference pdf = new PageReference('/apex/PrintPDF');
            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';
            cv.IsMajorVersion = true;  
            cv.VersionData=body;  

            contentVersionMap.put(OppId, cv);  
        } 

        insert contentVersionMap.Values();

        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);

        }

        insert ContentDocumentLinkList;
        
    }
}
Let me know.
 
This was selected as the best answer
H 007H 007

Hi Abdul ,


Thank for your response, can you please also help me to send this attachment file on account email address. 
public class OpportunityPdfController {
		

    @future(callout=true)
    public static void pdfGenration(Set<Id> OppIds){      

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

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

        Map<Id, ContentVersion> contentVersionMap = new Map<Id, ContentVersion>(); 

        Blob body;

        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!=Null){      
        
            insert contentVersionMap.Values(); 
                            
        }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!=Null){    
       		insert ContentDocumentLinkList;            
        }       
    }  
    public static List<Messaging.EmailFileAttachment> ContentDocumentAsAttachement(Id[] contentDocumentIds) {
    List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>{};
    List<ContentVersion> documents                  = new List<ContentVersion>{};

    documents.addAll([
      SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId
      FROM ContentVersion
      WHERE isLatest = true AND ContentDocumentId IN :contentDocumentIds
    ]);

    for (ContentVersion document: documents) {
      Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();

      attachment.setBody(document.VersionData);
      attachment.setFileName(document.Title);

      attachments.add(attachment);
    }

    return attachments;
    }
}