合并字段将在Visualforce页面上显示出来

发布于 2025-01-24 11:51:52 字数 2604 浏览 0 评论 0原文

我最近才开始学习Apex,此时仍然有很多主题很难导航。我到处都在寻找有效的解决方案,但是我仍然无法弄清楚。

我在Salesforce org上创建了一个按钮,该按钮从Visualforce页面呈现PDF,并将其作为文件附加到记录上。这将在稍后与DocuSign一起使用,以捕获合同的签名。问题在于,当使用VF页面中使用合并字段时,它们要么根本不显示,要么我得到此例外:“通过SOQL检索SoBject Row而不查询所请求的字段”。

现在,例外明确地说我需要查询字段,这就是我发现要做这项工作需要做的事情,但是我无法弄清楚如何正确地做到这一点。我已经尝试在控制器扩展中的几个位置运行查询,但无济于事(我使用的是为自定义对象创建的标准控制器)。

这是我的扩展代码:

public class attachPDFToQuote {
    
    public final i360__Quote__c q {get; set;} //Quote object
     
   
    
    //constructor
    public attachPDFToQuote (ApexPages.StandardController stdController) {
        q = (i360__Quote__c)stdController.getRecord(); 
        
        /* for(i360__Quote__c query:[SELECT Id, Correspondence_Name__c, Name FROM i360__Quote__c WHERE Id=: q.Id]){
      
      System.debug(i360__Quote__c.Correspondence_Name__c);
      }*/
        
    }
     
    
    public PageReference attachPDF() {
    
   /* for(i360__Quote__c query:[SELECT Id, Correspondence_Name__c, Name FROM i360__Quote__c WHERE Id=: q.Id]){
      
      System.debug(i360__Quote__c.Correspondence_Name__c);
      }*/
    
        //generate and attach the PDF document
        PageReference pdfPage = Page.ProjectAgreement;
        Blob pdfBlob; //create a blob for the PDF content
        if (!Test.isRunningTest()) { //if we are not in testing context
            pdfBlob = pdfPage.getContent(); //generate the pdf blob
        } else { //otherwise, we are in testing context. Create the blob manually.
            pdfBlob = Blob.valueOf('PDF');
        }
        
        
        ContentVersion cvAttach = new ContentVersion(ContentLocation= 'S');
        cvAttach.PathOnClient= 'Project Agreement.pdf';
        cvAttach.Title= 'Project Agreement';
        cvAttach.VersionData= pdfBlob;
        insert cvAttach;
        
        Id conDoc = [SELECT ContentDocumentID FROM ContentVersion WHERE Id=: cvAttach.Id].ContentDocumentId;
        ContentDocumentLink ConDocLink = new COntentDocumentLink();
        conDocLink.LinkedEntityId= q.Id;
        conDocLink.ContentDocumentId= conDoc;
        conDocLink.ShareType= 'V';
        insert conDocLink;
       
        
        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(q).view(); //redirect the User back to the Quote detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way
    }

}

我保留了评论的代码,我尝试查询对象字段,以便它们在VF中显示。我还尝试了几种不同的方式,但似乎没有任何作用。请让我知道我是否需要添加其他任何东西。

谢谢你!

I just started learning Apex recently, and there's still a lot of topics that are hard for me to navigate at this time. I've searched everywhere for a solution that works, but I still haven't been able to figure it out.

I've created a button on my Salesforce org that renders a PDF from a visualforce page, and attaches it to the record as a File. This is to be used with Docusign later on to capture signatures for contracts. The problem is that, when using merge fields in the VF page, they either do not show at all, or I get this exception: "sObject row was retrieved via SOQL without querying the requested field".

Now, the exception explicitly says that I need to query the fields, and this is what I've found I need to do to make this work, but I have not been able to figure out how to do this properly. I've tried running a query in several places in my controller extension to no avail (I am using a standardController that SF created for my custom object).

Here's my extension's code:

public class attachPDFToQuote {
    
    public final i360__Quote__c q {get; set;} //Quote object
     
   
    
    //constructor
    public attachPDFToQuote (ApexPages.StandardController stdController) {
        q = (i360__Quote__c)stdController.getRecord(); 
        
        /* for(i360__Quote__c query:[SELECT Id, Correspondence_Name__c, Name FROM i360__Quote__c WHERE Id=: q.Id]){
      
      System.debug(i360__Quote__c.Correspondence_Name__c);
      }*/
        
    }
     
    
    public PageReference attachPDF() {
    
   /* for(i360__Quote__c query:[SELECT Id, Correspondence_Name__c, Name FROM i360__Quote__c WHERE Id=: q.Id]){
      
      System.debug(i360__Quote__c.Correspondence_Name__c);
      }*/
    
        //generate and attach the PDF document
        PageReference pdfPage = Page.ProjectAgreement;
        Blob pdfBlob; //create a blob for the PDF content
        if (!Test.isRunningTest()) { //if we are not in testing context
            pdfBlob = pdfPage.getContent(); //generate the pdf blob
        } else { //otherwise, we are in testing context. Create the blob manually.
            pdfBlob = Blob.valueOf('PDF');
        }
        
        
        ContentVersion cvAttach = new ContentVersion(ContentLocation= 'S');
        cvAttach.PathOnClient= 'Project Agreement.pdf';
        cvAttach.Title= 'Project Agreement';
        cvAttach.VersionData= pdfBlob;
        insert cvAttach;
        
        Id conDoc = [SELECT ContentDocumentID FROM ContentVersion WHERE Id=: cvAttach.Id].ContentDocumentId;
        ContentDocumentLink ConDocLink = new COntentDocumentLink();
        conDocLink.LinkedEntityId= q.Id;
        conDocLink.ContentDocumentId= conDoc;
        conDocLink.ShareType= 'V';
        insert conDocLink;
       
        
        //redirect the user
        PageReference pageWhereWeWantToGo = new ApexPages.StandardController(q).view(); //redirect the User back to the Quote detail page
        pageWhereWeWantToGo.setRedirect(true); //indicate that the redirect should be performed on the client side
        return pageWhereWeWantToGo; //send the User on their way
    }

}

I kept the commented code where I try to query the object fields so they show in VF. I also tried a couple of different ways, but nothing seems to work. Please let me know if I need to add anything else.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鱼窥荷 2025-01-31 11:51:52

您没有发布Visualforce页面的代码。

即使是同一页面(如果您的Apex类在ProjectAgreement VF中使用为< Apex:Page StandardController =“ i360__quote__c” extensions =“ stactpdftoquote” - 抓取页面计数的行为作为标注,我可以说出一个单独的HTTP流量,以便

您说明您

PageReference pdfPage = Page.ProjectAgreement;
pdfPage.getParameters().put('id', q.Id);
Blob = pdfPage.getContent();

是否需要类似的内容...下一步是查看您的VF代码

。代码> {!i360__Quote__c.name},{!i360__Quote__c.corresporce_name__c} salesforce应该通过查看您的VF页面来确定哪些字段甚至需要构造函数中的查询,您只需将stdcontroller.getID()保存到类变量,然后在pdfpage.getParameters()中使用该ID。代码>

但是,如果您的VF页面引用了{!quote.correspondence_name__c},则需要将显式查询保留在其中。

You didn't post your Visualforce page's code.

Even if it's same page (if your apex class is used in ProjectAgreement VF as <apex:page standardController="i360__Quote__c" extensions="attachPDFToQuote" - the act of grabbing a PDF version of the page counts as callout, a separate http traffic to fresh instance of the page so to speak.

So I suspect you need something like

PageReference pdfPage = Page.ProjectAgreement;
pdfPage.getParameters().put('id', q.Id);
Blob = pdfPage.getContent();

If that works... next step would be to look at your VF code.

If the page has merge fields such as {!i360__Quote__c.Name}, {!i360__Quote__c.Correspondence_Name__c} then magic should happen. Salesforce should figure out which fields are needed by looking at your VF page and silently query them for you. So you wouldn't even need the query in your constructor, you could just save stdController.getId() to class variable and then use that id in pdfPage.getParameters().set(...)

But if your VF page has references to {!quote.Correspondence_Name__c} then you need to keep the explicit query in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文