When I click button, method's setToApprove() action is beginning.

But first lines of method when terf_mr_status='approved' and terf_mr_approveddate=TODAY_VALUE doesn't appears inside of generated pdf attachment. these updated fields only appears after method's ending.

How to fix it? Thank you!


    public PageReference setToApprove() {

    //  update MR fields:

    MR.TERF_MR_Status__c = 'Approved';
    DateTime d = Date.Today().addDays(1) ;
    MR.TERF_MR_ApproveDate__c = d.format('dd.MM.yyyy');
    update MR;

    //  convert vf-page to attachment:
    PageReference pdf = Page.TERF_Print;
    pdf.getParameters().put('id', MR.Id);
    Attachment attach = new Attachment();
    Blob body;
    try {
        body = pdf.getContent();
    } catch (VisualforceException e) {
        body = Blob.valueOf('Error: ' + e);

    attach.Body = body;
    attach.Name = 'DopForm-' + mr.TERF_MR_EmployeeFullname__c + '-' + mr.TERF_MR_year__c + '-' + mr.TERF_MR_monthNumber__c + ', ' + d.format('dd.MM.yyyy') + '.pdf';
    attach.IsPrivate = false;
    attach.ParentId = MR.Id;
    insert attach;

    //  update URL field
    MR.TERF_MR_AttachmentURL__c = URL.getSalesforceBaseUrl().toExternalForm() + '/servlet/servlet.FileDownload?file=' + attach.Id;
    upsert MR;

    //  update page 
    PageReference pageRef = new PageReference('/apex/TERF_EditMR');
    pageRef.getParameters().put('id', report.Id);
    return pageRef;


I put lines of attachment creation into another method createAttachment(); and run it "onComplete":


<apex:actionFunction name="generatePDF" action="{!createAttachment}"></apex:actionFunction>

<apex:commandbutton value="Approve" action="{!setToApprove}" onComplete="generatePDF();">

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.