function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Laura Boyd 18Laura Boyd 18 

Passing Variable from VF page to controller

Hi,

To set the background:

I have a VF page which is triggered from a lightning action button on an object called Purchase_Order__c
The button directs to the VF page called SendEmailDialog_VF which calls the controller SendEmailDialog_Controller. The controller functionality is to send an email with a PDF attachment. The PDF is generated there and then using another VF page as a template

The functionality is working perfectly if I leave the filename for the PDF in the controller as a static value,but the requirement is to include a variable as part of the file name. I worked out how to do this using apex param value but are now experiencing the following situation:

1) If I use just Apex Param Value as part of the command button then it just passes the variable across as 'null'
2) If i add the the ReRender option to the command button functionality, as people recommend to do, the email sends and the PDF has the right filename BUT the PDF file gives an error about being corrupt when opened.

Any guidance would be appreciated.

VF Page (SendEmailDialog_VF)

<apex:page standardController="Purchase_Order__c" extensions="SendPDFbyEmail" showQuickActionVfHeader="false">

      

      <h1 style="font-size:16px">Send Email</h1>

      <p style="font-size:12px">Complete the below form to send the PDF by email</p>

     

      <apex:form >

      <apex:pageBlock >

      <apex:pageBlockSection columns="3">

      <apex:outputLabel style="font-size: 12px" for="Email Address" value="Email Address:">&nbsp; </apex:outputLabel>

      <apex:inputText size="50" maxlength="50" id="EmailAddress" value="{!emailAddress}" required="true"/><br></br>

      <apex:outputLabel style="font-size: 12px" for="Subject" value="Subject:">&nbsp; </apex:outputLabel>

      <apex:inputText size="50" maxlength="50" id="Subject" value="{!eSubject}" required="true"/><br></br>

      <apex:outputLabel style="font-size: 12px" for="Body" value="Body">:</apex:outputLabel>

      <apex:inputTextarea rows="10" html-maxlength="1024" style="width:99%" id="Body" value="{!eBody}" required="true"/><br></br>



      <apex:commandButton value="Send" action="{!SendEmail}" rerender="hiddenBlock">

      <apex:param name="POName" value="{!Purchase_Order__c.Name}" assignTo="{!POName}"/>

      </apex:commandButton>

      <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>

  

      </apex:pageBlockSection>

      </apex:pageBlock>

      </apex:form>  

</apex:page>
 

VF Controller (SendEmailDialog_Controller)
 

public with sharing class SendPDFbyEmail {



public SendPDFbyEmail (ApexPages.StandardController controller){

}



//Set variable values

public String POName {get;set;}

public String EmailAddress {get;set;}

public String eSubject{get;set;}

public String eBody{get;set;}





//Start mail process and set criteria for PDF

public PageReference SendEmail() {



Messaging.SingleEmailMessage email=new Messaging.SingleEmailMessage();

PageReference pdfGeneration=Page.Purchase_Order_PDF;

pdfGeneration.SetRedirect(true);

Blob b=pdfGeneration.getContent();



//Create attachment and set criteria

Messaging.EmailFileAttachment attach=new Messaging.EmailFileAttachment();

attach.setFileName(POName+': '+DateTime.now().format('ddMMyyyy')+'.pdf');



attach.setBody(b);



    email.SetSubject(eSubject);

    email.SetPlainTextBody(eBody);

    email.setToAddresses(new String[] { EmailAddress });

    email.setFileAttachments(new Messaging.EmailFileAttachment[] {attach});



    

//Now Sending the Email

Messaging.SendEmailResult[] r=Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

    

    return null;

    

}
}
PawanKumarPawanKumar
Hi,

You can add id to page block at line #13 in VF and the same id you can use at line #31 in rerender attribute.

i.e.
  <apex:commandButton value="Send" action="{!SendEmail}" rerender="hiddenBlock">

Regards,
Pawan Kumar

PS: Please let me know if it helps you.
Laura Boyd 18Laura Boyd 18

Hi Pawan,
 

Thanks for your reply. I have tried as suggested but the PDF is still corrupted when it arrives.
 

<apex:page standardController="Purchase_Order__c" extensions="SendPDFbyEmail" showQuickActionVfHeader="false">

      

      <h1 style="font-size:16px">Send Email</h1>

      <p style="font-size:12px">Complete the below form to send the PDF by email</p>

     

      <apex:form >

      <apex:pageBlock id="hiddenBlock">

      <apex:pageBlockSection columns="3">

      <apex:outputLabel style="font-size: 12px" for="Email Address" value="Email Address:">&nbsp; </apex:outputLabel>

      <apex:inputText size="50" maxlength="50" id="EmailAddress" value="{!emailAddress}" required="true"/><br></br>

      <apex:outputLabel style="font-size: 12px" for="Subject" value="Subject:">&nbsp; </apex:outputLabel>

      <apex:inputText size="50" maxlength="50" id="Subject" value="{!eSubject}" required="true"/><br></br>

      <apex:outputLabel style="font-size: 12px" for="Body" value="Body">:</apex:outputLabel>

      <apex:inputTextarea rows="10" html-maxlength="1024" style="width:99%" id="Body" value="{!eBody}" required="true"/><br></br>



      <apex:commandButton value="Send" action="{!SendEmail}" rerender="hiddenBlock">

      <apex:param name="POName" value="{!Purchase_Order__c.Name}" assignTo="{!POName}"/>

      </apex:commandButton>

      <apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>

  

      </apex:pageBlockSection>

      </apex:pageBlock>

      </apex:form>  

</apex:page>
PawanKumarPawanKumar
Hi Laura,

Two thinks,

1. now you are able to get Name in controller?
2. comment line # 35 (pdfGeneration.SetRedirect(true);) instead pass PurchaseOdrerRowId as below
pdfGeneration.getParameters().put('id', pucrchaseId);

Please let me know.

Regards,
Pawan Kumar
PawanKumarPawanKumar
Before doing above, Please comment VF line#37 and try as id='hiddenBlock' duplicated.