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
IGLIGL 

Problem with sendmail

Hi to all,

we are trying to begin with apex in my org, we need to have a vf page with this objects:

- A datatable with the opportunitys (done)

-Checkbox for each register (done)

-Subject and body textboxes for a email sending(done)

-A button wich send an email to de selected opportunities(done)

 

the problem has come with the last requeriment, we need a listbox with EmailTemplates, its done but when i show this textbox the send mail button dont send the email.

 

Here is my controller:

 

public with sharing class EmailfromCheks
 { 
  
 //public EmailTemplate Template;
 String[] Templates = new String[]{};
 public List<SelectOption> getItems()  {
     // if (TemplateList == null)
     //  {
            List<EmailTemplate> Template =
            [Select Id, Name From EmailTemplate Where IsActive = true order by FolderId limit 30];
      List<SelectOption> TemplateList = new List<SelectOption>();
     
        for (EmailTemplate c : Template)
         {
            TemplateList.add(new SelectOption( c.id, c.name ));
         }
     //}
     return TemplateList;
      }
 public String[] getTemplates() {
            return Templates;
        }
           
        public void setTemplates(String[] Templates) {
            this.Templates = Templates;
        }
 
  /*  public EmailTemplate getEmailTemplate()
      {
       if(Template == null) Template = new EmailTemplate();
       return Template;
      }
    public String[] selectedTemplate=new String[]{};
    public String[] getSelectedTemplate()
      {
     return selectedTemplate;
      }
    public void setSelectedTemplate(String[] sel)
      {
     selectedTemplate=sel;
      }
    public List<SelectOption> TemplateList;
    public List<SelectOption> getTemplate ()
      {
      if (TemplateList == null)
       {
            List<EmailTemplate> Template =
            [Select Id, Name From EmailTemplate Where IsActive = true order by FolderId limit 30];
         TemplateList = new List<SelectOption>();
        for (EmailTemplate c : Template)
         {
            TemplateList.add(new SelectOption( c.id, c.name ));
         }
     }
     return TemplateList;
      }*/
    public String subject { get; set; }
    public String body { get; set; }
    public List<cOpportunity> OppLista;
    public List<cOpportunity> getOppLista()
      { 
       if(OppLista == null) {
       System.debug('Empieza');
       OppLista = new List<cOpportunity>(); 
       for(Opportunity c : [select Id, Name, EmailContactoCurso__c
       from Opportunity
       where Opportunity.CodigoDeCurso__c = :ApexPages.currentPage().getParameters().get('id')])
          { 
            OppLista.add(new cOpportunity(c)); 
          } 
       }
          return OppLista; 
      }
  public PageReference RefreshOpps()
  {
  List<Opportunity> selectedcOpp = new List<Opportunity>(); 
   for(cOpportunity cOpp : OppLista)
     { 
     if(cOpp.selected == true)
        { 
         selectedcOpp.add(cOpp.Opp); 
        } 
     } 
    return null;
  }
  public PageReference Send()
  {
 //    System.debug('Declaro');
    List<Opportunity> selectedcOpp = new List<Opportunity>(); 
   
   for(cOpportunity cOpp : getOppLista())
    { 
     if(cOpp.selected == true)
        { 
    System.debug('oportunidad seleccionada');
         selectedcOpp.add(cOpp.Opp); 
        } 
     } 
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 String addresses;
    System.debug('These are the selected Contacts...'); 
    for(Opportunity cOpp : selectedcOpp)
        { 
        if(addresses != null)
          { 
          addresses += ':' + cOpp.EmailContactoCurso__c; 
          } 
        else
          {
          addresses = cOpp.EmailContactoCurso__c;
          } 
              String[] toAddresses = addresses.split(':', 0);
        System.debug('direcciones:' + toAddresses);
    //email.setTemplateId(selectedTemplate[0]);
        email.setSubject(subject);
        email.setToAddresses( toAddresses );
        email.setPlainTextBody(body);
        // Sends the email 
           Messaging.SendEmailResult [] r =
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
     
     } 
       return null; 
     } 
  public  class cOpportunity
    {
     public Opportunity Opp {get; set;} 
     public Boolean selected {get; set;} 
     public cOpportunity(Opportunity c)
       { 
        Opp = c; 
       ; 
       } 
    }     
 }

 

and here my vf page:

 

<apex:page controller="EmailfromCheks">
<apex:form >
<apex:selectList value="{!Templates}" size="1">
<apex:selectOptions value="{!Items}"/> if i comment this line the page send the email
<apex:actionSupport event="onchange" rerender="features"/>
</apex:selectList>
 <apex:pageBlock >
       <apex:pageBlockTable value="{!OppLista}" var="c" id="table">
         <apex:column >
              <!-- This is our selected Boolean property in our wrapper class -->
              <apex:inputCheckbox value="{!c.selected}"/>
         </apex:column>
          <!-- This is how we access the contact values within our cContact container/wrapper -->
         <apex:column value="{!c.Opp.Name}" />
         <apex:column value="{!c.Opp.EmailContactoCurso__c }" />
    </apex:pageBlockTable>
 </apex:pageBlock>
<br /><br />
            <apex:outputLabel value="Subject" for="Subject"/>:<br />    
            <apex:inputText value="{!subject}" id="Subject" maxlength="80"/>
            <br /><br />
            <apex:outputLabel value="Body" for="Body"/>:<br />    
            <apex:inputTextarea value="{!body}" id="Body"  rows="10" cols="80"/>          
            <br /><br /><br />
            <apex:commandButton value="Send Email" action="{!Send}" />
           </apex:form>
 </apex:page>

dmchengdmcheng

It looks like you are not setting selectedTemplate to the value that is selected in the VF picklist, so this line will have a null value:

  //email.setTemplateId(selectedTemplate[0]);

IGLIGL

Thanks for the response, i have commented this line because its doesnt work, i know the email with this line commented dont use this template but it should be sended whit text plain body and subject.

(if i commented the line that i wrote in red the email its sended).

 

Its being a really headache for me.

Thank you very much.

 

dmchengdmcheng

Yes, but you are still not assigning the selected picklist value to any variable within the class, so if you want to use a template, it is still not going to work.