+ Start a Discussion
BrianWKBrianWK 

Populate String based on Checkboxes in DataTable

Good afternoon everyone. I hope someone can point me in the right direction.
 
I'm creating a visual force page that e-mails a 2nd Visualpage rendered in PDF. All that code works fabuliously - but it's been requested that our users can select from a related list the e-mails to send. What I have is a field for "Additional Email Address" and a datatable that displays a related list of "Product Contact Roles" (Basically a object with Contact fields on it).
 
I've managed to get a checkbox in the datatable, I know I can pass e-mail strings by manually typing them in my "Additional Email Address" field - but I"m lost at how do I insert the E-mails listed in the datatable based on the check boxes. I thought of using Onclick or making a button but I'm not quite sure how to actually pass that information.
 
So what is the best option to accomplish this? Do I need to use the check boxes to pass the information to the controller? I assume the onclick option in the inputcheckbox would have to be javascript (which I'm not very familiar with).
 
Here's what I have so far - excuse the messy code I've been going back and forth doing alot of experimentation. Thanks for the help!
 
Page Code:
Code:
<apex:page standardcontroller="Implementation__c" extensions="EmailSentri7StatusController" >
    <apex:form >
        <apex:sectionheader title="Stuff"/>
        <apex:pageblock title="Email" mode="view">            
                <apex:pageblockbuttons location="top">
                    <apex:commandbutton action="{!EmailStatus}" value="Send"/>
                </apex:pageblockbuttons>
            <apex:pageblocksection >
            <!-- Email Address to Send -->
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Contact Email Address:" for="emailaddressfield">
                    </apex:outputlabel>
                    <apex:inputtext value="{!emailAddress}" id="emailAddressField" size="32"/>
                </apex:pageblocksectionitem>
                <!-- Email for CSS String -->
                <apex:pageblocksectionitem >
                    <apex:outputlabel value="Additional Email Address:" for="Cssaddressfield">
                    </apex:outputlabel>
                    <apex:inputtext value="{!CssAddress}" id="CssAddressField" size="32"/>
                </apex:pageblocksectionitem>
                <!-- This is to add the subject -->
                <apex:pageblocksectionitem >                    
                    <apex:outputlabel value="Email Subject:" for="EmailSubjectField">
                    </apex:outputlabel>
                    <apex:inputtextArea value="{!emailSubject}" id="EmailSubjectField" cols="75" />                    
                </apex:pageblocksectionitem>
                <!-- This is to add the Body -->
                <apex:pageblocksectionitem >                    
                    <apex:outputlabel value="Email Body:" for="EmailBodyField">
                    </apex:outputlabel>
                    <apex:inputtextArea value="{!emailBody}" id="EmailBodyField" cols="75" rows="10"/>                    
                </apex:pageblocksectionitem>
            </apex:pageblocksection>
            <!-- List Product Contact Roles on Implementation -->
            <apex:pageblocksection title="Product Contact Roles" columns="1">
                <apex:datatable value="{!Implementation__c.Product_Contact_Role__r}" var="each" styleclass="list">  
                    <!-- Check box that hopefully has the e-mail as a value - not sure what to use onclick or something else -->
                    <apex:column headervalue="checkbox">
                        <apex:inputCheckbox value="{!each.Contact__r.Email}" />               
                    </apex:column>             
                    <apex:column headervalue="Contact Name" value="{!each.Contact__r.Name}" />
                    <apex:column headervalue="Email" value="{!each.Contact__r.Email}" />
                    <apex:column headervalue="Role" value="{!each.Role__c}" />
                    <apex:column headervalue="Implementer" value="{!each.Implementer__c}" /> 
                </apex:datatable>
               
            </apex:pageblocksection>        
        </apex:pageblock>
 </apex:form>    
</apex:page>

 

And my controller:

Code:
public class EmailSentri7StatusController 
{
    private ApexPages.StandardController controller;
    private String emailAddress;
    private String emailSubject;
    private string emailBody;
    private string cssAddress;
   
    
    public EmailSentri7StatusController(ApexPages.StandardController controller) 
    {
        this.controller = controller;
    }

//Testing used for apex:checkboxes and apex:selectoptions example on page 110
public PageReference test() {
return null;
}
public list<SelectOption> getitems()
{
 list<selectOption> options = new list<selectoption>();
 ID ImplementationId = this.controller.getRecord().id;
 Product_Contact_Role__c[] PCR = [Select p.Contact__r.Email From Product_Contact_Role__c p where p.Implementation__c =:ImplementationId];
 for (integer i=0; i<pcr.size(); i++)
 {
  options.add(new SelectOption(PCR[i].Contact__r.email,PCR[i].Contact__r.email));
 }
 return options;
}
string[] emails = new string[]{};
public string[] getEmails() 
{
 return Emails;
}
Public void setEmails(String[] emails)
{
 this.emails = emails;
}

 //This gets an CSs Address
    public String getCssAddress()
  
    {
        return cssAddress;
    }  

  //this sets the email address found
    public void setCssAddress(String CssAddress)
    {
        this.CssAddress = CssAddress;
    }  
 
   //Dynamic Body Line: This gets an email Subject
    public String getEmailBody()  
    {        
        if(emailBody==null)
        {
      emailBody = 'Please find attached your project status' ;
        }
        return emailBody;
    }
  //this sets the Subject
    public void setEmailBody(String emailBody)
    {
        this.emailBody = emailBody;
    }
  //Dynamic Subject Line: This gets an email Subject
    public String getEmailSubject()  
    {  
        //Build Implementation Object for Subject line
  // ID of the implementation
        ID ImplementationId = this.controller.getRecord().id;
  Implementation__c EmailImplementation = [Select i.Name From Implementation__c i where i.id = :ImplementationId];        
        if(EmailSubject==null)
        {
      EmailSubject = 'Sentri7 Status Update ' + EmailImplementation.Name ;
        }
        return EmailSubject;
    }
  //this sets the Subject
    public void setEmailSubject(String emailSubject)
    {
        this.emailSubject = emailSubject;
    }
     
  //This gets an email Address
    public String getEmailAddress()
  
    {
     //Set EmailAddress if Null in visualforce page S7Emailtest
        if(emailAddress==null)
        {
         
         //This was my test to send to me
      Contact EmailContact =[Select c.Email From Contact c where c.id = '0035000000O12Zo'];
      emailaddress =  EmailContact.Email;
        }

        return emailAddress;
    }
  

  //this sets the email address found
    public void setEmailAddress(String emailAddress)
    {
        this.emailAddress = emailAddress;
    }
    
    public PageReference emailStatus()
    {
        // ID of the implementation
        ID ImplementationId = this.controller.getRecord().id;
        //Build Contact Object 
           Contact EmailContact =[Select c.id From Contact c where c.email = :emailAddress];
        
        // Retrieve the PDF as a Blob from the Visualforce page
        PageReference S7Email = Page.S7Email;
        S7Email.getParameters().put('id', ImplementationId);
        Blob pdf = s7Email.getContent();    
      
        // Create the email to send to client
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();        
       
        //set email that's provided
        mail.setToAddresses( new String[] {emailAddress});
        
        //SET SUBJECT LINE        
        //OriginalCode: mail.setSubject('Sentri7 Status Update ' + EmailImplementation.Name);
        mail.setSubject(emailSubject);
        
        //Set BODY
        //Original Code: mail.setPlainTextBody('Please find attached your project status');      
        //mail.setHtmlBody('this works but not s7email');    
        mail.setPlainTextBody(emailBody);
        
        //set Attachment   
        Messaging.EmailFileAttachment mailAttachment;
        mailAttachment = new Messaging.EmailFileAttachment();
        mailAttachment.setFileName('Sentri7Status.pdf');
        mailAttachment.setBody(pdf);
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { mailAttachment });
        
        //Set cc email removed
        mail.setCcAddresses(new string[] {'FILL with CssEmails'});
        //Set BCC email removed
        mail.setBccAddresses(new string[] {'bcc email'});
        
        //save as activity
        mail.setTargetObjectId(EmailContact.Id);
        mail.setWhatId(ImplementationId);
        mail.setSaveAsActivity(true);
       
        // Send the email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
        return ApexPages.currentPage();
        //test to return the attachment
        //return s7Email;      
    }
}


 

 


SuzanyuSuzanyu
From my understanding what you want to achieve is to add selected contacts' emails into a mail list(whatever) by clicking the checkboxes in a datatable.
 
That can be done via put checkbox in front of each line record on your page.
 
there  is a way to do checkbox selection
 
 
good luck