How to attach pdf from trigger to an object?

Viewed 542

I'm a bit lost trying to attach a pdf with populated values from an opportunity record Here is the code:

Trigger:

trigger OpportunityTrigger on Opportunity (after insert)
if(trigger.isAfter && trigger.isUpdate) {
    opportunityTriggerHelper.attachFileToOpportunityRecord(trigger.new);
}

Helper Class:

private void attachFileToOpportunityRecord(List<Opportunity> lstOpp) {
    List<Id> oppListIdsForAttach = new List<Id>();

    for(Opportunity opp : lstOpp) {
        oppListIdsForAttach .add(opp.Id);
    }

    attachFileToOpportunities(oppListIdsForAttach);

}

@future(callout=true)
private static void attachFileToOppotunities(List<Id> OpportunityIds) {
    List<Attachment> attachList = new List<Attachment>();

    for(Id oppId : opportunityIds) {
        OpportunityPdfController file = new OpportunityPdfController();
        file.getData(oppId);
        PageReference pdfPage = Page.PdfAttachmentForOpp;
        blob pdfBody;
        pdfBody = pdfPage.getContent();

        Attachment attach = new Attachment();
        attach.Body = pdfBody;
        attach.Name = 'Pdf file';
        attach.IsPrivate = false;
        attach.ParenId = oppId;

        attachList.add(attach);
    }
    insert attachList;
}

VF Page:

<apex:page controller="OpportunityPdfController" renderAs="pdf">

<apex:repeat value="{!pricingDetails}" var="pd">
    <apex:outputText>{!pd.basePrice}</apex:outputText>
</apex:repeat>
</apex:page>

VF Page Controller:

public with sharing class OpportunityPdfController {
    public List<PricingDetailWrapper> pricingDetails {get;set;}

    public void getData(Id opportunityId) {
        List<Pricing_Detail__c> pdList = [
            SELECT basePrice
            FROM Pricing_Detail__c
            WHERE OpportunityId =: opportunityId
        ];
    
        for(Pricing_Detail__c pd : pdList) {

            PricingDetailWrapper pdw = new PricingDetailWrapper();
            pdw.basePrice = pd.basePrice;
    
            pricingDetails.add(pdw);
        }
    }

    public class PricingDetailWrapper {
        public String basePrice {get;set;}
    }
}

The result is whenever I update an opportunity it attaches the corresponding pdf file but it is blank and if I add for example the following to vf page body: "<h1> hello World!</h1>" this works and shows as expected, but this is not happening to what I required above.

1 Answers

You didn't really pass the opportunity id to the VF page. And I doubt this actually works at all? If you manually access the VF page as /apex/PdfAttachmentForOpp?id=006... does it render the content ok? I'm assuming it doesn't.

Fixing the page

You didn't specify constructor so SF generates one for you, fine. I think you need to add something like

public OpportunityPdfController(){
    if(ApexPages.currentPage() != null){
        Id oppId = ApexPages.currentPage().getParameters().get('id');
        System.debug(oppId);
        getData(oppId);
    }
}

Add this, try to access the page passing valid opp id and see if it renders ok, if right stuff shows in debug log. /apex/PdfAttachmentForOpp?id=006...

(VF page constructors are bigger topic, this might be simpler with standardController + extension class)

Fixing the callout

VF page (especially accessed as callout) will not share memory with the OpportunityPdfController controller you've created in the code. New object of this class will be created to support the page and your file will be ignored. You might try to make-do with some static variable holding current opportunity's id but it feels bit yucky.

In normal execute anonymous try if this returns correct pdf:

PageReference pdfPage = Page.PdfAttachmentForOpp;
pdfPage.getParameters().put('id', '006...');
Blob pdfBody = pdfPage.getContent();
System.debug(pdfBody.toString());

If it works - use similar trick in the actual code, pass the id as url parameter.

Related