• Mike Young 12
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Being new to the Apex code world, I have managed, with some help from several forums, to get the following Class and VF page working.  However, I am totally lost on how to write a test for it.  The page allows the user to address an email and include user selected Attachments from Notes and Attachments from a custom object, Sales_Order__c.  How do I test this with the class, etc.?  Can someone supply some code to get me going in the right direction?
 
VF Page
 
<apex:page standardController="Sales_Order__c" extensions="email_class" showHeader = "false" sidebar = "false">
    <apex:form >
        <apex:pageBlock title="Addressing / Message">
        <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
        <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/> 
        <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>           
        <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
        
            <apex:pageBlock title="Check to include attachment with lease approval. LQR is Required!">
                <apex:pageBlockTable value="{!Sales_Order__c.Attachments}" var="Att">
                    <apex:column headerValue="Check to Include">
                    <apex:inputCheckbox value="{!Sales_Order__c.Review_SC_Complete__c}"/>
                    </apex:column>
                    <apex:column value="{!Att.name}"/>
                    <apex:column value="{!Att.contenttype}"/>
                    <apex:column value="{!Att.createddate}"/>
               </apex:pageBlockTable><p/>
      </apex:pageblock>
      <apex:commandButton value="Send Email" action="{!send}"/>
      <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlock>
    </apex:form>   
</apex:page>
 
Apex Class
 
public class email_class{
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string SOid {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
  
    public email_class(ApexPages.StandardController controller) {
        SOid = ApexPages.currentPage().getParameters().get('id');
    }
   
    Public PageReference send(){
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
        mail.setToAddresses(new String[] {emailTo});
        mail.setCcAddresses(new String[] {emailCC});   //set the cc address
        mail.setSubject(subject);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody(email_body);
        mail.setWhatId(SOid);// Set email file attachments
 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
 
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :SOid]){  // Add to attachment file list 
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); 
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);// Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
    }
}
   
Being new to the Apex code world, I have managed, with some help from several forums, to get the following Class and VF page working.  However, I am totally lost on how to write a test for it.  The page allows the user to address an email and include user selected Attachments from Notes and Attachments from a custom object, Sales_Order__c.  How do I test this with the class, etc.?  Can someone supply some code to get me going in the right direction?
 
VF Page
 
<apex:page standardController="Sales_Order__c" extensions="email_class" showHeader = "false" sidebar = "false">
    <apex:form >
        <apex:pageBlock title="Addressing / Message">
        <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
        <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/> 
        <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>           
        <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
        
            <apex:pageBlock title="Check to include attachment with lease approval. LQR is Required!">
                <apex:pageBlockTable value="{!Sales_Order__c.Attachments}" var="Att">
                    <apex:column headerValue="Check to Include">
                    <apex:inputCheckbox value="{!Sales_Order__c.Review_SC_Complete__c}"/>
                    </apex:column>
                    <apex:column value="{!Att.name}"/>
                    <apex:column value="{!Att.contenttype}"/>
                    <apex:column value="{!Att.createddate}"/>
               </apex:pageBlockTable><p/>
      </apex:pageblock>
      <apex:commandButton value="Send Email" action="{!send}"/>
      <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlock>
    </apex:form>   
</apex:page>
 
Apex Class
 
public class email_class{
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string SOid {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
  
    public email_class(ApexPages.StandardController controller) {
        SOid = ApexPages.currentPage().getParameters().get('id');
    }
   
    Public PageReference send(){
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
        mail.setToAddresses(new String[] {emailTo});
        mail.setCcAddresses(new String[] {emailCC});   //set the cc address
        mail.setSubject(subject);
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody(email_body);
        mail.setWhatId(SOid);// Set email file attachments
 
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
 
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :SOid]){  // Add to attachment file list 
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); 
            efa.setFileName(a.Name);
            efa.setBody(a.Body);
            fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);// Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
    }
}
   
I have created a Visualforce page and Apex Class to be able to send an email from an Opportunity with Attachments that are in the Notes & Attachments section. The page allows them to pick an attachment to send with the email. The email will send but does not include the attachment. I know I am missing something, any help would be appriciated.

Apex Class
public class email_class{

    public email_class(ApexPages.StandardController controller) {

    }
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
       
    Public PageReference send(){
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
            mail.setToAddresses(new String[] {emailTo}); 
            mail.setCcAddresses(new String[] {emailCC});   //set the cc address
            mail.setSubject(subject);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(email_body);
            mail.setWhatId(opportunityId);// Set email file attachments 
            
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

            for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
                efa.setFileName(a.Name); 
                efa.setBody(a.Body); 
                fileAttachments.add(efa);}
                mail.setFileAttachments(fileAttachments);// Send email
                
                
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
        }
}



VF page
<apex:page standardController="Opportunity" extensions="email_class">
    <apex:form >
           
        <apex:pageBlock title="Email Details">
            <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
            <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/>  
            <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>            
            <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
          
        <apex:pageBlock title="Attachments">
        <apex:pageBlockTable value="{!opportunity.Attachments}" var="Att">
            <apex:column headerValue="Select">
            <apex:inputCheckbox value="{!opportunity.Send_Email_with_Quote__c}"/>
            </apex:column>
       <apex:column value="{!Att.createddate}"/>
       <apex:column value="{!Att.name}"/>
       <apex:column value="{!Att.description}"/>
       </apex:pageBlockTable><p/>
       </apex:pageblock>
                
       <apex:commandButton value="Send Email" action="{!send}"/>
       <apex:commandButton value="Canel" action="{!cancel}"/>
            
       </apex:pageBlock>
                           
    </apex:form>    
</apex:page>