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.