I have set up a class that sends an email and I am using a VF Email Template that uses the Oppertunity fields. I have set up a custom field called Proposals_Senders_Email__c that is a text field.

I want to use Proposals_Senders_Email__c and the setToAddresses in my APEX class.

do I need to do a Query like this?

Opportunity o = [Select Proposals_Senders_Email__c FROM Opportunity WHERE Id=: OpportunityId];

Here is my code, I have been trying lots of things so I have had to comment out lots of things, for testing. Thank you.

 global class proteusProposalCon {
  global ApexPages.StandardController controller;
  global proteusProposalCon(ApexPages.StandardController controller) {
    this.controller = controller;

    global PageReference saveAndCongrat() {
    controller.save(); // This takes care of the details for you.
    PageReference congratsPage = Page.thankyou;
    return congratsPage;
  global List<Opportunity> listOfOpportunity {get; set;}
  global Opportunity OpportunityObj {get;set;}

  Webservice static void sendEmailFunction(){
    //Opportunity o = [Select Id, Contact_Name__c FROM Opportunity WHERE Id=: OpportunityId];

    //String ProteusStaffName = ApexPages.currentPage().getParameters().get('VFname');
    String ProteusStaffEmail = ApexPages.currentPage().getParameters().get('VFemail');

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

     String[] toAddresses = new String[] {'XXX@YYY.com'};

     //String[] toAddresses = new String[] {'XXX@YYY.com'}; 

    //String ProteusStaffName = ApexPages.currentPage().getParameters().get('VFname');
     //String[] ProteusStaffName2 = new String[] {ProteusStaffName};
     //String[] ccAddresses = new String[] {'XXX@YYY.com'};

     // Assume the record ID is contained in the the parameter named "id"

    Id WhatId = ApexPages.currentPage().getParameters().get('id');
    Id UserIdVar = ApexPages.currentPage().getParameters().get('userId');

    String myUserIdString = UserIdVar;
    // Set the target object ID on the email object
    mail.saveAsActivity = false;

     mail.setSenderDisplayName('Proteus Leadership');



     if (!Test.IsRunningTest()) Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });



If you are using a template, then you simply cannot use setToAddresses, setBccAddresses or setCcAddresses. If the person being emailed is also a contact, lead or user, then you can use targetObjectId, or targetObjectIds. Otherwise, I'm sorry but I do not know of any clean way around that restriction. Source

|improve this answer|||||
  • Thank you for clearing that up for me, that is why it wouldn't work. So If I wasn't going to use a template and if I wanted to use the setTextBody how would I go about it? Thank you. – proteuscanvas Aug 23 '14 at 7:15
  • In that case you would need to build up the body by either concatenating strings and values together or using String.Format(String, List<String>) to send via setHtmlBody or setPlainTextBody. – Bob Roberts Aug 23 '14 at 13:21
  • Bob -- the doc is wrong; you can most definitely setToAddresses(), setBccAddresses(), and setCcAddresses() with a template. I've posted a doc feedback with SFDC on this point. See salesforce.stackexchange.com/a/32102/2602 – cropredy Aug 24 '14 at 3:58
  • So if it is possible, how do I do it? I will need to make a list with a SOQL query and then how do I call the field? – proteuscanvas Aug 24 '14 at 9: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.