1

We have a Screen Flow that basically creates quotes. In that flow, two Apex Actions are called: one that creates a QuoteDocument from a visualforce rendered as pdf:

PageReference pdf = Page.presupuestoPDF;
pdf.getParameters().put('id', presupuestos[0]);

QuoteDocument adjunto = new QuoteDocument();
adjunto.document = !Test.isRunningTest() ? pdf.getContent() : Blob.valueOf('test');
adjunto.quoteId = presupuestos[0]; //quote id list received as param
insert adjunto;

And then a second Apex Action that basically sends an email from a template, with some attachments, including that pdf:

for(QuoteDocument qd : quoteDocuments.values()){
    attachment = new Messaging.EmailFileAttachment();
    attachment.setFileName(qd.Name);
    attachment.setBody(qd.Document);
    attachment.setContentType('application/pdf'); //I tried adding this, but it doesn't solve anything
    attachments.add(attachment);
}

EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = :'Presupuesto'];

Messaging.SingleEmailMessage emailMessage = Messaging.renderStoredEmailTemplate(template.Id, oppo.ContactId, oppo.Id);
emailMessage.setBccSender(true);

emailMessage.setFileAttachments(attachments);

List<Messaging.SendEmailResult> result = Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{emailMessage});

So the problem is that, when using the screen flow normally, the email sends correctly BUT the pdf attached to the email is only 1.5kb and corrupt, can't preview or open it. But if I execute the screen flow from the debug section of the flow builder, the email is sent correctly and the PDF is NOT corrupted and I can preview and open it normally.

What is the difference between the execution context of the flow debugger and the normal execution context? What could I change in my flow to solve this?

EDIT:

When opening the generated pdf, this is what shows when opening with notepad:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">





<script>
function redirectOnLoad() {
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://vc1687380243540.my.salesforce.com?ec=302&startURL=%2Fapex%2Fpresupuestopdf%3Finline%3D1%26id%3D0Q0Ho000001DiiUKAS'); }  else 
if (window.location.replace){ 
window.location.replace('https://vc1687380243540.my.salesforce.com?ec=302&startURL=%2Fapex%2Fpresupuestopdf%3Finline%3D1%26id%3D0Q0Ho000001DiiUKAS');
} else {
window.location.href ='https://vc1687380243540.my.salesforce.com?ec=302&startURL=%2Fapex%2Fpresupuestopdf%3Finline%3D1%26id%3D0Q0Ho000001DiiUKAS';
} 
} 
redirectOnLoad();
</script>

</head>


</html>





<!-- Body events -->
<script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script>
            
</body>
</html>


<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->
2
  • What's the first part of the "corrupt" file looking like if you open it in Notepad or Wordpad? That would help explain the problem.
    – sfdcfox
    Aug 15 at 18:42
  • @sfdcfox I updated the post with the code from the pdf. Thanks! Aug 16 at 12:50

2 Answers 2

1

Based on the file contents, you're missing a valid Session Id. This HTML is what you'd get back in your browser, before being redirected, if you were not logged in to Salesforce. You should be able to fix this by setting your Session Id:

PageReference pdf = Page.presupuestoPDF;
pdf.getParameters().put('id', presupuestos[0]);
pdf.getHeaders().put('Authorization', 'Bearer '+UserInfo.getSessionId());

This is likely context-sensitive, which is why it may not always work as expected. If this doesn't work, you'll want to use a Named Credential and set up an Integration User or the like to act as a proxy to retrieve the PDF content.

3
  • Thank you for the answer! I actually solved this by moving the code with the getContent() to a future method, and changing the flow a little bit, it was a enough for our functionality. My question is: we have another org in which this corrupted file problem doesn't happen, we actually copied everything the same... Do you have any idea as to why in one org it works and in another it doesn't? Is there a org-wide setting that I could change? Security setting maybe? Aug 16 at 15:15
  • @joacoiudica Maybe. I don't think I've ever observed this happening. Maybe it's a permissions issue for the page? Make sure the profile(s) that need to access the page have permission.
    – sfdcfox
    Aug 16 at 16:03
  • this is all running from the admin user... so there shouldn't be any permissions issue. what I would like to know is, what difference in execution context exists in the flow builder debugger, run button from the debugger and future methods that makes the getContent work correctly, as opposed to the normal execution context from a lightning page. I don't know if you have that information but it is interesting to me Aug 16 at 16:41
0

I finally solved this by moving the logic containing the getContent() and DML in a @future(callout=true) method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .