5

I have a visualforce page where I am showing all the attachments related to a case with a checkbox to know if the user has marked it. This is the related part of the VF page:

<apex:pageBlock title="Existing Case Attachments">
    <apex:pageBlockTable value="{!listAttachWrapper}" var="item">
        <apex:column title="Select" headerValue="Add Attachment" width="50px,50px">
            <apex:inputCheckbox value="{!item.IsChecked}">
                </apex:inputCheckbox>
        </apex:column>
        <apex:column headerValue="File">
            <apex:outputLink value="{!URLFOR($Action.Attachment.Download,item.att.Id)}"
                target="_blank">{!item.att.Name}</apex:outputLink>
        </apex:column>
        <apex:column value="{!item.att.Description}" />
    </apex:pageBlockTable>
</apex:pageBlock>

I also have my controller with the definition of the wrapper class:

public List<attWrapper> listAttachWrapper {get;set;}
public List <Attachment> selectedmems{get;set;}
public class attWrapper{
    public Attachment att {get; set;}
    public Boolean isChecked{get; set;}
    public attWrapper(Attachment att){
        this.att=att;
        if (isChecked==null){
            isChecked=false;
        }
    }
}

And in the constructor of the method, I get the elements and mark the ischecked field to false:

listAttachWrapper=new List<attWrapper>();
for (Attachment a:[SELECT Name,Description FROM Attachment WHERE ParentId=:case1.Id])
        listAttachWrapper.add(new attWrapper(a));

Finally I have the method that is called from the VF page, where I try to get which attachments have been market to true:

selectedmems=new List<Attachment>();
system.debug('List of members: '+listAttachWrapper);
for (attWrapper att: listAttachWrapper){
    if (att.IsChecked==true){
        selectedmems.add(att.att);
    }
}
system.debug('The list is:' + selectedmems);

I call the above code from the button:

<apex:pageBlockButtons > <apex:commandButton value="Send communication" action="{!communicateClient}" /> <apex:commandButton value="Cancel" action="{!cancel}" />         </apex:PageBlockButtons> 

The problem is that I get all the elements with the IsChecked field to false, even though I am selecting them in the VF page.

I have been for two days already with this, can anyone help please?

Thanks a lot!

Antonio


Sorry I forgot to paste the code of the button:

<apex:pageBlockButtons > <apex:commandButton value="Send communication" action="          {!communicateClient}" /> <apex:commandButton value="Cancel" action="{!cancel}" />     </apex:PageBlockButtons> 

That calls the method that tries to get the values of the list:

selectedmems=new List<Attachment>();
system.debug('List of members: '+listAttachWrapper);
for (attWrapper att: listAttachWrapper){
    if (att.IsChecked==true){
        selectedmems.add(att.att);
    }
}
system.debug('The list is:' + selectedmems);

Any help is appreciated.Thanks,

Antonio

4
  • How are you submitting to the controller - commandbutton, actionfunction etc? Also, are you rerendering the page or carrying out a full refresh. Jul 1, 2014 at 8:06
  • I am submitting with a Commandbutton:
    – Antonio
    Jul 1, 2014 at 9:31
  • <apex:pageBlockButtons > <apex:commandButton value="Send communication" action="{!communicateClient}" /> <apex:commandButton value="Cancel" action="{!cancel}" /> </apex:PageBlockButtons>
    – Antonio
    Jul 1, 2014 at 9:31
  • And also I am not rerendering the page as in the examples I found in internet they didnt do so....
    – Antonio
    Jul 1, 2014 at 10:00

3 Answers 3

1

You need to add an <apex:commandButton> or an <apex:commandLink> to your vf-page to submit the page. Also you have to bind the button or link to an action function in the controller.

An other way would be to use ajax, but I recommend to start with the method above.

2
  • Sorry, Could you please explain a little more on detail or with an example? Thanks a lot for your help!
    – Antonio
    Jul 1, 2014 at 9:29
  • I had the button already added, I forgot to paste it, sorry. Any other ideas on what I am missing?
    – Antonio
    Jul 2, 2014 at 8:36
1

Check with below code:

Controller:

public Class CheckCodeController{

  public List<attWrapper> listAttachWrapper {get;set;}
  public List <Attachment> selectedmems{get;set;}
  public class attWrapper{
    public Attachment att {get; set;}
    public Boolean isChecked{get; set;}
    public attWrapper(Attachment att){
      this.att=att;
      if (isChecked==null){
        isChecked=false;
      }
    }
  }

  public CheckCodeController(){
    listAttachWrapper=new List<attWrapper>();
    for (Attachment a:[SELECT Name,Description FROM Attachment LIMIT 10])
      listAttachWrapper.add(new attWrapper(a));
  }


  public void communicateClient(){
    selectedmems=new List<Attachment>();
    system.debug('List of members: '+listAttachWrapper);
    for (attWrapper att: listAttachWrapper){
      if (att.IsChecked==true){
        selectedmems.add(att.att);
      }
    }
    system.debug('The list is:' + selectedmems);
    system.assert(false, 'The list is:' + selectedmems);
  }
}

VF Page:

<apex:page controller="CheckCodeController">
<apex:form >
    <apex:pageBlock title="Existing Case Attachments">
        <apex:pageBlockTable value="{!listAttachWrapper}" var="item">
        <apex:column title="Select" headerValue="Add Attachment" width="50px,50px">
            <apex:inputCheckbox value="{!item.IsChecked}">
                </apex:inputCheckbox>
        </apex:column>
        <apex:column headerValue="File">Your output link code</apex:column>
        <apex:column value="{!item.att.Description}" />
    </apex:pageBlockTable>
    <apex:pageBlockButtons >
        <apex:commandButton value="Send communication" action="{!communicateClient}" />
    </apex:PageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page> 

I have tested and found working.

Happy Coding !!

0

I have tried an example on datatable though. It mite help you.

/contact wrapper/

public class contactWrapper
    {
        public Contact con{get; set;}
        public Boolean selected {get;set;}
        public contactWrapper(Contact c)
        {
            con= c;
            selected = false;
        }
    }

/get the selected contacts/

public with sharing class CheckboxTest{

List<Contact> selectedContacts = new List<Contact>();

public PageReference getSelected()
    {
        selectedContacts.clear();
        for(contactWrapper conwrapper : contactList)
        if(conwrapper.selected == true)
        selectedContacts.add(conwrapper.con);
        return null;
    }
}

Now selectedContacts holds the selected data.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .