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
rohitash yadav 8rohitash yadav 8 

Passing variable from visualforce page to apex class

Hi,

I am passing a parameter email from visual force pagfe to apex class. Based on the parameter I want to send pdf to the email address, but it is displaying error

Visualforce Page:
<apex:page standardController="Payment_Receipt__c" extensions="ReceiptEmailController">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Send Email">
            <div>
                <p style="text-align:center; font-size:16px;">Send an Email to <b><apex:outputField id="emailNew" value="{!Payment_Receipt__c.Contact_Email__c}" /></b> attaching Customer Details as PDF</p>
            </div>
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Cc:" />
                    <apex:inputText value="{!email_cc}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Send Email" action="{!sendEmailReceipt}" rendered="true" reRender="">

                </apex:commandButton>
          </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:iframe src="/apex/SendEmailReceiptPdf?id={!$CurrentPage.parameters.id}"/>
    </apex:form>
</apex:page>

Apex class is:
 
public with sharing class ReceiptEmailController {
    public ID receiptId {get;set;}
    public String email {get;set;}
    public String emailNew {get;set;}
    public String email_cc {get;set;}

    public ReceiptEmailController (ApexPages.StandardController controller) {
        receiptId = System.currentPageReference().getParameters().get('id');
    }

    public PageReference sendEmailReceipt() {

        String emailNew = System.currentPagereference().getParameters().get('emailNew');
        if(email_cc !='' && !Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email_cc)) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email')); 
        } else {
            PageReference pdf = Page.SendEmailReceiptPdf;
            // add parent id to the parameters for standardcontroller
            pdf.getParameters().put('id',receiptId);
    
            // the contents of the attachment from the pdf
            Blob body;
            try {
                // returns the output of the page as a PDF
                body = pdf.getContent();
    
            // need to pass unit test -- current bug  
            } catch (VisualforceException e) {
                body = Blob.valueOf('Some Text');
            }
    //email = System.currentPageReference().getParameters().get('email');
            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.setContentType('application/pdf');
            attach.setFileName('receipt.pdf');
            attach.setInline(false);
            attach.Body = body;
    
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setUseSignature(false);
            mail.setToAddresses(new String[] { emailNew });
            mail.setSubject('PDF Receipt Demo');
            mail.setHtmlBody('Here is the email you requested! Check the attachment!');
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
    
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+emailNew ));
        }
        return null;
    }
}

When I click on send email then it is displaying the following error:

SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]

Can anyone help me:

Thanks,
Rohitash 
VineetKumarVineetKumar
This is not how you pass the value from VF page to a controller.
You have just specified the Id of the component. Since the value is not getting passed, you are getting this exception.
Also, I can see that you have useed an outputField, means the value is already there on the controller, you are not inputting the value anywhere, then what value are you trying to send from VF page to Apex?
rohitash yadav 8rohitash yadav 8
Hi Vineet,

I am trying to get the value of output field in apex class.

<apex:outputField id="emailNew" value="{!Payment_Receipt__c.Contact_Email__c}" />

What should I do to get it

Thanks,
Rohitash
VineetKumarVineetKumar
Something like this
<apex:outputLink value="{!Payment_Receipt__c.Contact_Email__c}">
        <apex:param name="emailNew" value="{!Payment_Receipt__c.Contact_Email__c}"/>
 </apex:outputLink>

And then in controller you can use :
String receiptId = System.currentPageReference().getParameters().get('emailNew');

 
rohitash yadav 8rohitash yadav 8
Hi Vineet,

I am getting the same error message. 

SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]

Thanks,
Rohitash
 
VineetKumarVineetKumar
This is still the same, values not getting passed to the controller
Can you share your code here?
rohitash yadav 8rohitash yadav 8
Hi Vineet,

Visualforce Page is:
 
<apex:page standardController="Payment_Receipt__c" extensions="ReceiptEmailController">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="Send Email">
            <div>
                <p style="text-align:center; font-size:16px;">Send an Email to 
                <b>
                <apex:outputField  value="{!Payment_Receipt__c.Contact_Email__c}">
                    <apex:param name="emailNew" value="{!Payment_Receipt__c.Contact_Email__c}"/>
                </apex:outputField>
                </b> attaching Customer Details as PDF</p>
            </div>
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Cc:" />
                    <apex:inputText value="{!email_cc}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Send Email" action="{!sendEmailReceipt}" rendered="true" reRender="">
                    <apex:param name="email" value="rohitash.singh@dotsquares.com" assignTo="{!email}"  />
                </apex:commandButton>
          </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:iframe src="/apex/SendEmailReceiptPdf?id={!$CurrentPage.parameters.id}"/>
    </apex:form>
</apex:page>

APex Class is:
 
public with sharing class ReceiptEmailController {
    public ID receiptId {get;set;}
    public String email {get;set;}
    public String emailNew {get;set;}
    public String email_cc {get;set;}

    public ReceiptEmailController (ApexPages.StandardController controller) {
        receiptId = System.currentPageReference().getParameters().get('id');
        emailNew = System.currentPageReference().getParameters().get('emailNew');

    }

    public PageReference sendEmailReceipt() {
        //String emailNew = System.currentPagereference().getParameters().get('emailNew');
        String  emailNew = System.currentPageReference().getParameters().get('emailNew');
        if(email_cc !='' && !Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email_cc)) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Check your email')); 
        } else {
            PageReference pdf = Page.SendEmailReceiptPdf;
            // add parent id to the parameters for standardcontroller
            pdf.getParameters().put('id',receiptId);
    
            // the contents of the attachment from the pdf
            Blob body;
            try {
                // returns the output of the page as a PDF
                body = pdf.getContent();
    
            // need to pass unit test -- current bug  
            } catch (VisualforceException e) {
                body = Blob.valueOf('Some Text');
            }

            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.setContentType('application/pdf');
            attach.setFileName('receipt.pdf');
            attach.setInline(false);
            attach.Body = body;
    
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setUseSignature(false);
            mail.setToAddresses(new String[] { emailNew });
            mail.setSubject('PDF Receipt Demo');
            mail.setHtmlBody('Here is the email you requested! Check the attachment!');
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
    
            // Send the email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+emailNew ));
        }
        return null;
    }
}