1

I have been struggling with this issue from last few days and cannot figure this out. Im trying to dynamically set Document Id in my Visualforce PDF controller but cannot get this to work. If I set the link manually, it works fine. However, if I set it dynamically via get/set, it doest work. Debug log shows the correct value but PDF renders the broken image.

    @future(callout = true)
public static void sendPDF (String name, String contactemail, String lastname, String accountName, String phone, String contractorSign, String contractorTitle, String Conid,
                            String MailingStreet, String MailingCity, String MailingState, String MailingPostalCode,Id accountId, string strNetwork, string strSignatureBlob ){   


   system.debug('SendPDF method started : ' );

    Document dc = [SELECT Id, FolderId,Folder.Name FROM Document WHERE Folder.Name = 'signatures' limit 1];

    strSignatureBlob  = strSignatureBlob.replace('data:image/jpeg;base64,',''); 

    system.debug('strSignatureBlob===2: ' + strSignatureBlob);
    document document = new Document();
    document.Body = EncodingUtil.base64Decode(strSignatureBlob); 
    document.ContentType = 'image/jpeg';
    document.DeveloperName = 'dn' + Conid;
    document.IsPublic = true;
    document.Name = conid  ;
    document.FolderId = dc.FolderId ;
    insert document; 
    system.debug('document===2: ' + document);



    PageReference pdf = Page.MCA_VF;// Replace PdfOfInvoice with your Page which render as PDF.
    pdf.getParameters().put('signerName',(string)name );
    pdf.getParameters().put('signerEmail',(string)contactemail );
    pdf.getParameters().put('lastname',(string)lastname );
    pdf.getParameters().put('accName',(string)accountName );
    pdf.getParameters().put('Phone',(string)phone );
    pdf.getParameters().put('ContractorSign',(string)contractorSign );
    pdf.getParameters().put('ContractorTitle',(string)contractorTitle );
    pdf.getParameters().put('Conid',(string)Conid );
    pdf.getParameters().put('MailingStreet',(string)MailingStreet );
    pdf.getParameters().put('MailingCity',(string)MailingCity );
    pdf.getParameters().put('MailingState',(string)MailingState );
    pdf.getParameters().put('MailingPostalCode',(string)MailingPostalCode );
    pdf.getParameters().put('signatureIMG',document.Id);


    Blob b = pdf.getContentAsPDF();
    //Blob b;
    if (Test.IsRunningTest()){b=Blob.valueOf('UNIT.TEST');}else{b = pdf.getContentAsPDF();}
    // Create Attachment Object to attach with Email

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName(' MCA'+'.pdf');
    efa.setBody(b);
    // Define the email
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    // Sets the paramaters of the email
    email.setSubject('PDF of MCA ');
    email.setToAddresses( new List<String>{contactemail} );

    //email.setbccAddresses( new List<String>{'admin@gmail.com'} );
    //
    email.sethtmlBody('Hi '+ name +',<br/><br/> '
                      +'Please find the attached Master Contractor Agreement.'
                      +'<br/><br/>'+'Thanks,'+'<br/>'
                      +UserInfo.getName()+'<br/>'
                      +UserInfo.getOrganizationName());
    email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});




    ContentVersion cv=new Contentversion();
    cv.title='MCA Document';
    cv.ContentLocation = 'S';
    cv.PathOnClient = '_MCA.pdf';
    cv.versiondata= b;
    if (strNetwork != null)
        cv.NetworkId = strNetwork;
    insert cv;
    system.debug('content version cv ::' + cv);

    ContentVersion cv2 = [select id, ContentDocumentId from ContentVersion where id = :cv.Id limit 1];


    ContentDocumentLink link = new ContentDocumentLink();
    link.LinkedEntityId = accountId;
    link.ContentDocumentId = cv2.ContentDocumentId;
    link.ShareType = 'V';
    link.Visibility = 'AllUsers';
    insert link ;
    system.debug('content document link ::' + link);


    try{
        Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }catch(System.Exception e){

    }



}

If I manually hard code the image link this, it works fine.

 <apex:image value="https://devco123--community3--c.documentforce.com/servlet/servlet.ImageServer?id=0150x000000HZt1AAG&oid=00D0x0000000ccE" />

But when I set it up dynamically like this, it doesnt work. But I do see get/set variable has the value set.

 <apex:image value="https://devco123--community3--c.documentforce.com/servlet/servlet.ImageServer?id={!signature2}&oid=00D0x0000000ccE" />

Does this have something to do with @future method? This is the most weird issue I have come across. Any help is much appreciated.

0

The problem is most likely that:

Blob b = pdf.getContentAsPDF();

is a call to outside of the current transaction and the current transaction doesn't commit - make the image available to other transactions - until the end of the code.

So you need to figure out a way to move the pdf.getContentAsPDF(); and following code call into the future, or see if you can do the image insert in the transaction before your call the @future code.

2
  • Thanks Keith. I want to move document insert before @future call, but Im doing this for Partner community user. Partner Community doesnt have an access to create Document object so the code fails. I`m also using HTML2Canvas in my current VF page so that I can capture Signature Blob string, create Document and then deliver that as a image on PDF page. Any suggestions around that? Also, I do see "0150x000000HZt1AAG" ID in the debug, but it just doesn't get rendered as image in PDF. – Wendy Apr 14 '19 at 9:12
  • @Wendy The Id is allocated and is visible within the transaction, it is just that that data is not visible outside the transaction. See e.g. en.wikipedia.org/wiki/Isolation_(database_systems) for some info about that. – Keith C Apr 14 '19 at 10:05

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.