0

I have a custom visual force page with several contact fields . User will enter data and hit save button. Once Save button is hit , it should send email to the emailid specified on emailid field on the page. I have written a controller to send email. As of now, I hard coded emailid for Toaddress field.This is working fine. How can I use !contact.email in controller so that the email will go to the !contact.email

Here is the part of the controller

Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

String[] toAddresses = new String[] {  'abcd@gmail.com' };

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);  
mail.setReplyTo('xyz@yahoo.com');

mail.setSenderDisplayName('KFC');
mail.setSubject('Your upcoming shift with KFC');
mail.setBccSender(false);
mail.setPlainTextBody('Hi,I am sending a email');

// Send the email you have created.

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Here is the visual force page

  <apex:inputField value="{! contact.firstname}"/> 
  <apex:inputField value="{! contact.lastname}"/>    
  <apex:inputField value="{! contact.Title}"/>
  <apex:inputField value="{! contact.email}"/>
  <apex:inputField value="{! contact.phone}"/>     
  <apex:inputField value="{!contact.MailingStreet}"/>
  <apex:pageBlockButtons location="bottom" >  
  <apex:commandbutton value="Submit" action="{!save}"/>
  </apex:pageBlockButtons>

Thank you all

1

So, since you're code is incomplete I'm going to make a quick assumption:

  • You are using the standard controller with an extension

Your extension will NOT see the contact directly. You have to create a new variable in your extension that holds the contact provided by the standard controller:

public Contact c {get; set;}
public classname (standardController con) {
    c = con.getRecord();
    //Note: the record will ONLY contain fields available on the page which should work
    //in your case. If you need to access more fields than what is on the page
    //you will need to query for the contact:
    // c = [SELECT Id, Email FROM Contact WHERE Id = :con.getRecord().Id];
}

Once you do that, you should be able to fill your email address by referencing the new variable within your method:

Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

**String[] toAddresses = new String[]();**
**toAddresses.add(c.Email);**

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);  
mail.setReplyTo('xyz@yahoo.com');

mail.setSenderDisplayName('KFC');
mail.setSubject('Your upcoming shift with KFC');
mail.setBccSender(false);
mail.setPlainTextBody('Hi,I am sending a email');

If you are not using the standard set controller with an extension; rather, you are using a completely custom controller, that means you have something like this in your controller:

Contact contact = new Contact();

Shame on you...

Regardless, the answer is much easier:

Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

**String[] toAddresses = new String[]();**
**toAddresses.add(contact.Email);**

// Assign the addresses for the To and CC lists to the mail object.
mail.setToAddresses(toAddresses);  
mail.setReplyTo('xyz@yahoo.com');

mail.setSenderDisplayName('KFC');
mail.setSubject('Your upcoming shift with KFC');
mail.setBccSender(false);
mail.setPlainTextBody('Hi,I am sending a email');
|improve this answer|||||
  • 1
    I did everything except this line toAddresses.add(c.Email); After adding this line it worked..I was using c.Email for setToAddreses. Thank You. – SFDC Mar 16 '17 at 1:11

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.