+ Start a Discussion
SMasterSMaster 

How to use Checkbox in visualforce page for selecting the Records

Hi,

 

I have created a visualforce page where i am displaying all the Notes and Attachments.

 

With every record i have also used an Inputcheckbox.. i just want to determine the record id of the selected record on the checkbox click....

 

samarsamar

Hi SMaster,

I think wrapper class will solve your problem. for reference look at the following link

http://wiki.developerforce.com/index.php/Checkbox_in_DataTable

SMasterSMaster

Hi Samar,

 

i have tried implementing it using wrapper class, but unable to do so. Please suggest where am i doing wrong..

 

my apex class is:

 

public class testmail

{

 ApexPages.StandardController controller;   

 

 public class cOpp

 {

 public Attachment q {get;set;}

 public Boolean selected {get; set;}

 public cOpp(Attachment r)

 { 

 q = r;

 selected = false;

 }

 }

 public List<cOpp> OppList {get; set;} 

 public List<cOpp> getContacts() {

 if(OppList == null)

 {

 return OppList;

 }

   

 

 public Id oppr   

 {    get;set;    }       

 

 public testmail(ApexPages.StandardController ctrl)   

 {      

 oppr = ctrl.getRecord().Id;        

 }

 

 

 public PageReference emailAtt()

 {

 

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

        String[] toAddresses = new String[]{'ashish.garg@headstrong.com'};

       // String[] toAddresses = 'oppr.SMEs_Email__c';

 

        mail.setToAddresses(toAddresses);

        mail.setReplyTo('ashish.garg@headstrong.com');

        mail.setSenderDisplayName('CRM Support');

        //mail.setSubject('Mail with Attachment');

        mail.setBccSender(false);

        mail.setUseSignature(false);

        //mail.setPlainTextBody('Your Email has been sent'); 

        mail.setTargetObjectId('005Q0000000FR7f');

        mail.setTemplateId('00XQ0000000QULj');

        mail.saveAsActivity = false;

 

 

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

       

        String thisOpp = ApexPages.currentPage().getParameters().get('Id');

       

      //Set email file attachments

        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

        string currentid = '00PQ0000000hmVh';

        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])

        {

        // 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;

}

}

 

 

 

samarsamar

Hi SMaster,

I think you have not got the wrapper class well. It seems lots of thing in mess in your class. Also post your vf page, so that I can lead you to a right dirction.

SMasterSMaster

Hi Samar,

 

Yes, it is getting really difficult for me to implement the concept of wrapper class. Please get the following VF page:

 

 

<apex:page standardController="opportunity_proposals__c" extensions="testmail">
  <apex:form >
   <apex:pageBlock title="Attachments">
   <apex:pageBlockButtons >
            <apex:commandButton value="Send" action="{!emailAtt}"/>
        </apex:pageBlockButtons>


     <apex:pageBlockTable value="{!opportunity_proposals__c.Attachments}" var="Att">
     <apex:column headerValue="Select">
            <apex:inputCheckbox value="{!opportunity_proposals__c.for_sendemailwithattachment__c}"/>
        </apex:column>
       <apex:column value="{!Att.createddate}"/>
       <apex:column value="{!Att.name}"/>
       <apex:column value="{!Att.description}"/>
     </apex:pageBlockTable>
     
      </apex:pageBlock>

  </apex:form>
</apex:page>

SMasterSMaster

Hi Samar,

 

by using an existing code.. if i click the "send" button on the visualforce page all the file inside that record goes as attachments with the email...

 

however, i need to select the file first (i.e. from the visualforce page) and when i click the "send" button only the selected file should as email attachment...

samarsamar

Hi SMaster,

Sorry but you are going completly wrong.

Please go through the link I have sent you in my earlier post. Try to understand how the code is working. How a wrapper class work.

Follow the folllowing steps:

1. Define the wrapper class in the main class with a boolean variable and the attachment object variable.(You have done thing correctly)

2. Define a getter method for the wapper class with return type list of the wrapper class. Query for the attachments in the get method and create a list for the wrapper class.

3. Iterate through the list of the wrapper class in the page . Use the boolean varible of the wapper class as a value in the inputCheckbox.

4. In the send email method iterate through the wrapper class list and get all the attachment which have the boolean varible true.

 

Hope this steps will help you to understand the wrapper class and solve your issue.

 

SMasterSMaster

Hi Samar,

 

Based on your suggestion i have modified the code but still unable to find... where am i doing mistake...

 

please if possible correct the code to achieve the same.... i shall really be thankful to you..

 

 

public class Checkbox_Class 
{

        public Id oppr     {    get;set;    }         
        public Checkbox_Class(ApexPages.StandardController ctrl)    
        {       
        oppr = ctrl.getRecord().Id;         
        }
       
       
        List<Attachmentwrapper> AttachmentList = new List<Attachmentwrapper>();
        List<Attachment> selectedAttachments = new List<Attachment>();
             
        public List<Attachmentwrapper> getAttachments()
        {
            List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
           
            {       
            // 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);
           // AttachmentList.add(new Attachmentwrapper(a));
           // return AttachmentList;
         }
         
        public PageReference getSelected()
        {
           
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[]{'SMaster@gmail.com'};
         mail.setToAddresses(toAddresses);
         mail.setReplyTo('SMaster@gmail.com');
         mail.setSenderDisplayName('CRM Support');
         mail.setBccSender(false);
         mail.setUseSignature(false);
         mail.setTargetObjectId('005Q0000000FR7f');
         mail.setTemplateId('00XQ0000000QULj');
         mail.saveAsActivity = false;
                          
         selectedAttachments.clear();
         for(Attachmentwrapper accwrapper : AttachmentList)
         if(accwrapper.selected == true)
         selectedAttachments.add(accwrapper.acc);
         return null;
        }
         
        public List<Attachment> GetSelectedAttachments()
        {
            if(selectedAttachments.size()>0)
            return selectedAttachments;
            else
            return null;
        }    
         
        public class Attachmentwrapper
        {
            public Attachment acc{get; set;}
            public Boolean selected {get; set;}
            public Attachmentwrapper(Attachment a)
            {
                acc = a;
               ;
            }
        }
    }

SMasterSMaster

Please get the correct code....

 

 

public class Checkbox_Class 
{

        public Id oppr     {    get;set;    }         
        public Checkbox_Class(ApexPages.StandardController ctrl)    
        {       
        oppr = ctrl.getRecord().Id;         
        }
       
       
        List<Attachmentwrapper> AttachmentList = new List<Attachmentwrapper>();
        List<Attachment> selectedAttachments = new List<Attachment>();
             
        public List<Attachmentwrapper> getAttachments()
        {
            List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
            for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
           
            {       
            // 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);
           // AttachmentList.add(new Attachmentwrapper(a));
           // return AttachmentList;
         }
         
        public PageReference getSelected()
        {
           
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[]{'SMaster@gmail.com'};
         mail.setToAddresses(toAddresses);
         mail.setReplyTo('SMaster@gmail.com');
         mail.setSenderDisplayName('CRM Support');
         mail.setBccSender(false);
         mail.setUseSignature(false);
         mail.setTargetObjectId('005Q0000000FR7f');
         mail.setTemplateId('00XQ0000000QULj');
         mail.saveAsActivity = false;
                          
         selectedAttachments.clear();
         for(Attachmentwrapper accwrapper : AttachmentList)
         if(accwrapper.selected == true)
         selectedAttachments.add(accwrapper.acc);
         return null;
        }
         
        public List<Attachment> GetSelectedAttachments()
        {
            if(selectedAttachments.size()>0)
            return selectedAttachments;
            else
            return null;
        }    
         
        public class Attachmentwrapper
        {
            public Attachment acc{get; set;}
            public Boolean selected {get; set;}
            public Attachmentwrapper(Attachment a)
            {
                acc = a; 
                selected = false;;
            }
        }
    }

samarsamar

Change the getAttachments method

 

public List<Attachmentwrapper> getAttachments()
    {
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {       
            AttachmentList.add(new Attachmentwrapper(a));
            return AttachmentList;
        }
    }

 

 

In the sendEmail method iterate through the AttachmentList and check the value of "selected" variable of the wapper class if it is true add the attachment to mail.

e.g.

for(att:AttachmentList){
    if(att.selected){
    Attachment = att.acc;
//add the attachment to the mail
} 

}

 

Hope it will help you.

 

SMasterSMaster

Hi Samar,

 

I have made the changes you said.. but still its giving an error while saving the class:

 

 

 

public class Checkbox_Class 
{

        public Id oppr     {    get;set;    }         
        public Checkbox_Class(ApexPages.StandardController ctrl)    
        {       
        oppr = ctrl.getRecord().Id;         
        }
       
       
        List<Attachmentwrapper> AttachmentList = new List<Attachmentwrapper>();
        List<Attachment> selectedAttachments = new List<Attachment>();
       
        public List<Attachmentwrapper> getAttachments()
        {
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {      
            AttachmentList.add(new Attachmentwrapper(a));
            return AttachmentList;
                               
        }
        }
         
        public PageReference getSelected()
        {
           
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[]{'SMaster@Gmail.com'};
         mail.setToAddresses(toAddresses);
         mail.setReplyTo('SMaster@Gmail.com');
         mail.setSenderDisplayName('CRM Support');
         mail.setBccSender(false);
         mail.setUseSignature(false);
         mail.setTargetObjectId('005Q0000000FR7f');
         mail.setTemplateId('00XQ0000000QULj');
         mail.saveAsActivity = false;
                          
         //selectedAttachments.clear();
         for(Attachmentwrapper accwrapper : AttachmentList)
         {
         if(accwrapper.selected == true)
         {
         selectedAttachments.add(accwrapper.acc);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         }
         else
         {
         return null;
         }
         }       
        public List<Attachment> GetSelectedAttachments()
        {
            if(selectedAttachments.size()>0)
            return selectedAttachments;
            else
            return null;
        }    
       
        public class Attachmentwrapper
        {
            public Attachment acc{get; set;}
            public Boolean selected {get; set;}
            public Attachmentwrapper(Attachment a)
            {
                acc = a; 
               selected = false; 
            }
        }
    }

samarsamar

You are doing wrong in your getSelected method. You have not attached the attachment in your mail. and trying to send. and again the mail will go only if the first record is selected. Do the correction in your logic. By the way, what error you are getting?

SMasterSMaster

while saving the class.. i am getting the following error:

 

Error: Compile Error: expecting a semi-colon, found '('

 

and its giving an error on the following method:

 

public List<Attachment> GetSelectedAttachments()

 

.....

 

 

if possible please make the changes accordingly....

samarsamar

you dont need the GetSelectedAttachments() method at all

samarsamar

the error is because of the method getSelected() is not returning anything if the last loop doesnot go inside the else part.

SMasterSMaster

Hi Samar,

 

I am getting the following error while saving the apex class:

 

Error: Compile Error: Loop variable must be an SObject or list of Attachment

 

please suggest...

 

I am using the following code:

 

public class Checkbox_Class 
{


        public class Attachmentwrapper
        {
            public Attachment acc{get; set;}
            public Boolean selected {get; set;}
            public Attachmentwrapper(Attachment a)
            {
                acc = a;
               ;
            }
        }

        public Id oppr     {    get;set;    }         
        public Checkbox_Class(ApexPages.StandardController ctrl)    
        {       
        oppr = ctrl.getRecord().Id;         
        }
       
       
        List<Attachmentwrapper> AttachmentList = new List<Attachmentwrapper>();
        List<Attachment> selectedAttachments = new List<Attachment>();
       
        public List<Attachmentwrapper> getAttachments()
        {
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {      
            AttachmentList.add(new Attachmentwrapper(a));
            return AttachmentList;
                               
        }
        }
         
        public PageReference getSelected()
        {
           
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[]{'SMaster@gmail.com'};
         mail.setToAddresses(toAddresses);
         mail.setReplyTo('SMaster@gmail.com');
         mail.setSenderDisplayName('CRM Support');
         mail.setBccSender(false);
         mail.setUseSignature(false);
         mail.setTargetObjectId('005Q0000000FR7f');
         mail.setTemplateId('00XQ0000000QULj');
         mail.saveAsActivity = false;
                          
         //selectedAttachments.clear();
         for(Attachmentwrapper accwrapper : AttachmentList)
         {
         if(accwrapper.selected == true)
         {
         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);

        
        
         selectedAttachments.add(accwrapper.acc);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
         return null;

         }
                  }       
       /* public List<Attachment> GetSelectedAttachments()
        {
            if(selectedAttachments.size()>0)
            return selectedAttachments;
            else
            return null;
        }  */  
       
            }
}

 

SMasterSMaster

Hi Samar,

 

Please suggest the possible solution..to do this

ethanoneethanone
Is there test code for the link you posted (http://wiki.developerforce.com/index.php/Checkbox_in_DataTable)?