I have a situation where i have a VF page which is generated as pdf from opportunity record and this needs to create a record in a custom object attach the pdf to the custom objects notes and attachment and this newly created record id is updated back on the opportunity record.

For this purpose i have a new VF page and its controller performing the insertion, attaching to notes and attachment and updation back into opportunity and after which is redirected to the page which generates the pdf.

The pdf vf page also has a reference to the field newly updated to the opportunity. This field gets populated fine in the generated pdf but is not populated in the pdf saved as attachment.

Below is the code which does all the insertion and updation. The Last_Generated_Quote__c is the field which doesnt get populated correctly in the saved VF page. It shows the value it had before the new quotehistory id was updated.

I am thinking that the updation has not completed when the pdf was generated and saved so it still shows the old value but by the time the redirection to show the generated pdf in browser is finished the updation is complete and hence the correct value is shown in the pdf generated in browser.

How do we make sure that all operations are complete before i call for the pdf generation and attachment ?

UPDATE : My assumption seems to be wrong. The value in the opp reflects the latest values after i requery the opp. So that is not really the issue. Any other ideas why the pdf generated in the browser and one saved in as attatchment have different values?

 public pageReference createQuoteHistoryRecord()

this.ocrs= [SELECT contactid FROM OpportunityContactRole WHERE OpportunityId =:this.opp.Id AND isprimary= true LIMIT 1] ;
    if(this.ocrs.size() > 0)
        this.contactId= this.ocrs[0].contactid ;
    qh= new quote_history__c (Contact__c=contactId,opportunity__c=this.opp.Id,account__c=this.opp.accountid, amounts__c=this.opp.amount);
    insert qh;

    qh=[SELECT id,name from quote_history__c  WHERE id=: qh.Id];

    //update the opportunity field with  quote History
    this.opp.Last_Generated_Quote__c = qh.Id;
    update this.opp;

    this.opp = [SELECT id,name,accountId,amount,Quote_name__c,Last_Generated_Quote__c,Last_Generated_Quote__r.name from opportunity where id =: oppId ];
    System.debug('opp is '+ this.opp);

    Attachment att = new Attachment(name ='Quote-'+ qh.name +'.pdf');
     PageReference QuotePage= Page.Quote;
 // return page content as blob type
 // Alt: att.body = invoicePage .getContentAsPDF();
     if (Test.IsRunningTest())
       att.body =Blob.valueOf('UNIT.TEST');
     att.body = QuotePage.getContent();
     att.parentid = qh.Id;
     insert att;
     update qh;
     return QuotePage;

The reason for this is Salesforce does not commit the changes to database until the execution of your code block is completed. This means when you call getContactAsPDF(), it still reads old values from the database as somehow that method is not within the same context.

  1. You can try moving the code that attaches the pdf to a separate method and mark it as @Future.
  2. Use actionFunction to update the opportunity first using an ajax request and then call another actionFunction on the onComplete event to do the attachment and redirection part.

See this post for more detais. Fields missing using getContent call for Visualforce rendered as PDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.