如何将 pdf 从触发器附加到对象?

How to attach pdf from trigger to an object?

我有点迷路了,试图附加一个 pdf,其中包含机会记录中填充的值 这是代码:

触发器:

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

帮手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 页面:

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

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

VF 页面控制器:

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

结果是,每当我更新商机时,它都会附加相应的 pdf 文件,但它是空白的,如果我将以下内容添加到 vf 页面正文中:"<h1> hello World!</h1>" 这会按预期工作和显示,但这没有发生我上面要求的事情。

您并没有真正将机会 ID 传递给 VF 页面。我怀疑这是否真的有效?如果您以 /apex/PdfAttachmentForOpp?id=006... 手动访问 VF 页面,它是否可以呈现内容?我假设它没有。

正在修复页面

您没有指定构造函数,所以 SF 为您生成了一个,没问题。我认为您需要添加类似

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

添加这个,尝试访问传递有效 opp id 的页面,看看它是否呈现正常,如果正确的东西显示在调试日志中。 /apex/PdfAttachmentForOpp?id=006...

(VF 页面构造函数是更大的话题,使用 standardController + 扩展可能会更简单 class)

修复标注

VF 页面(特别是作为标注访问)不会与您在代码中创建的 OpportunityPdfController 控制器共享内存。将创建此 class 的新对象以支持该页面,而您的 file 将被忽略。您可能会尝试使用一些保存当前商机 ID 的静态变量来凑合,但感觉有点恶心。

如果这个 returns 正确 pdf:

在正常情况下执行匿名尝试
PageReference pdfPage = Page.PdfAttachmentForOpp;
pdfPage.getParameters().put('id', '006...');
Blob pdfBody = pdfPage.getContent();
System.debug(pdfBody.toString());

如果有效 - 在实际代码中使用类似的技巧,将 id 作为 url 参数传递。