+ Start a Discussion
Anto HotelbedsAnto Hotelbeds 

inputcheckbox and wrapper class

Hi,

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

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
Best Answer chosen by Anto Hotelbeds
Anto HotelbedsAnto Hotelbeds
Great! I made the change as you said and I also changed the visualfoce because I had the button code in one form and the wrapper class in another <apex:form> and now its working ok!

So now the visualforce page looks like:

<apex:page standardController="Case" showHeader="true"
    extensions="CaseExtensionXML">

    <!-- Section Header -->
    <apex:sectionHeader title="XML Case Communicate Client"
        subtitle="{!Case.CaseNumber}" />

    <!-- Form -->
    <apex:form title="Communicate Client">
        <apex:pageblock title="Case Description and Comments">
            <apex:pageBlockSection id="CaseDescription" columns="2"
                collapsible="true" title="Case Description">
                <!-- cambiar color letras header -->
                <apex:facet name="header">
                    <span style="color: #222222">Case Description</span>
                </apex:facet>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Case Description" />
                    <apex:outputField value="{!Case.Description}" />
                </apex:pageBlockSectionItem>
                <!-- This script sets up the section to be collapsible -->
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseDescription}');
             </script>
            </apex:pageBlockSection>

            <apex:pageBlockSection id="CaseComments" columns="1"
                collapsible="true" title="Case Comments">
                <apex:facet name="header">
                    <span style="color: #222222">Case Comments</span>
                </apex:facet>
                <apex:include pageName="CommentsTable2" />
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseComments}');
             </script>
            </apex:pageBlockSection>

        </apex:pageblock>

        <apex:pageBlock title="Communicate Client" id="XML_Communicate_Client"
            mode="Edit">
            <!-- Error messages -->
            <apex:pageMessages />

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

                <apex:pageBlockSection columns="1">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Type of Communication" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputField value="{!Case.Type_of_Communication__c}" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>


                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Comments" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputTextarea value="{!Case.Comments_for_Admin__c}"
                                    style="width:700px; height:300px;" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Send Case Historical Data?" />
                        <apex:inputField value="{!Case.Send_historical_data__c}" />
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
        </apex:pageBlock>


    <apex:pageBlock title="Existing Case Attachments" id="attachpageblock">
        <apex:pageBlockTable value="{!listAttachWrapper}" var="item" id="attachtable">
            <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>
    </apex:form>
</apex:page>


To whom it may help, remember to keep the button and the list where you are using the wrapper class under the same <apex:form>!!

Thanks to eveyrone for your help!

All Answers

kiranmutturukiranmutturu
could you please give the complete code base of page and class?
Anto HotelbedsAnto Hotelbeds
Sure. This is the visualforce page:

<apex:page standardController="Case" showHeader="true"
    extensions="CaseExtensionXML">

    <!-- Section Header -->
    <apex:sectionHeader title="XML Case Communicate Client"
        subtitle="{!Case.CaseNumber}" />

    <!-- Form -->
    <apex:form title="Communicate Client">
        <apex:pageblock title="Case Description and Comments">
            <apex:pageBlockSection id="CaseDescription" columns="2"
                collapsible="true" title="Case Description">
                <!-- cambiar color letras header -->
                <apex:facet name="header">
                    <span style="color: #222222">Case Description</span>
                </apex:facet>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Case Description" />
                    <apex:outputField value="{!Case.Description}" />
                </apex:pageBlockSectionItem>
                <!-- This script sets up the section to be collapsible -->
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseDescription}');
             </script>
            </apex:pageBlockSection>

            <apex:pageBlockSection id="CaseComments" columns="1"
                collapsible="true" title="Case Comments">
                <apex:facet name="header">
                    <span style="color: #222222">Case Comments</span>
                </apex:facet>
                <apex:include pageName="CommentsTable2" />
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseComments}');
             </script>
            </apex:pageBlockSection>

        </apex:pageblock>

        <apex:pageBlock title="Communicate Client" id="XML_Communicate_Client"
            mode="Edit">
            <!-- Error messages -->
            <apex:pageMessages />

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

                <apex:pageBlockSection columns="1">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Type of Communication" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputField value="{!Case.Type_of_Communication__c}" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>


                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Comments" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputTextarea value="{!Case.Comments_for_Admin__c}"
                                    style="width:700px; height:300px;" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Send Case Historical Data?" />
                        <apex:inputField value="{!Case.Send_historical_data__c}" />
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
        </apex:pageBlock>




        <apex:pageBlock title="Upload Attachments">
            <apex:repeat value="{!newAttachments}" var="newAtt">
                <apex:pageBlockSection columns="3">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="File" />
                        <apex:inputFile value="{!newAtt.body}" filename="{!newAtt.name}" />
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Description" />
                        <apex:inputText value="{!newAtt.Description}" />
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:repeat>
            <apex:commandButton value="Add More" action="{!addMore}" />
        </apex:pageBlock>
    </apex:form>
    <apex:form >
    <apex:pageBlock title="Existing Case Attachments">
        <apex:pageBlockTable value="{!listAttachs}" 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>
    </apex:form>
</apex:page>

And the related part of the class( i have change it a little bit to try more solutions, but still not working):

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 List<attWrapper> getlistAttachs(){
    	if (listAttachWrapper==null){
    		listAttachWrapper=new List<attWrapper>();
    		for (Attachment a:[SELECT Name,Description FROM Attachment WHERE ParentId=:case1.Id])
				listAttachWrapper.add(new attWrapper(a));
    	}
    	return listAttachWrapper;
    }

The command button code:

//check validations and sends email with comments to main contact on the account if exists
     public pageReference communicateClient(){
            //check validations
            Integer NumInternalCases=[SELECT COUNT() FROM Case WHERE Internal_Status__c <> 'Closed' AND ParentId=:case1.Id AND (RecordTypeId=:Schema.SObjectType.Case.RecordTypeInfosByName.get('XML - Internal Deparment').RecordTypeId OR RecordTypeId=:Schema.SObjectType.Case.RecordTypeInfosByName.get('Web Support Case').RecordTypeId OR RecordTypeId=:Schema.SObjectType.Case.RecordTypeInfosByName.get('Sales Support Case').RecordTypeId)];
            //List<Case> InternalCases=new List<Case>([SELECT Id FROM Case WHERE ]);
            if(case1.Comments_for_Admin__c=='' || case2.AccountId == NULL || case1.Type_of_Communication__c==NULL){
                ApexPages.Message myMsg =  new ApexPages.Message(ApexPages.Severity.FATAL,'Please fill in all mandatory information.');
                ApexPages.addMessage(myMsg);
                return ApexPages.currentPage();
            }else if (NumInternalCases==0 && case1.Type_of_Communication__c=='Case Update Waiting for Internal Department'){
                ApexPages.Message myMsg =  new ApexPages.Message(ApexPages.Severity.FATAL,'You cannot make a Case Update Waiting for Internal Department if there are no Internal Cases opened');
                ApexPages.addMessage(myMsg);
                return ApexPages.currentPage();
            }else{ 
                //send email to case main contact
                //system.debug('el listado de adjuntos memTmpLst: '+memTmpLst);
                //system.debug('el listado de adjuntos memList: '+memList);
                //selectedmems=new List<Attachment>();
                List <Attachment> selectAtts=new List<Attachment>();
                system.debug('Lista de miembros: '+getlistAttachs());
                for (attWrapper att: getlistAttachs()){
                	if (att.IsChecked==true){
                		selectAtts.add(att.att);
                	}
                }
                system.debug('el listado es:' + selectAtts);
}

Any help is appreciated.Thanks!

Antonio
Amritesh SahuAmritesh Sahu
Hi Antonio,

In method public List<attWrapper> getlistAttachs(), dont fetch and return at the same time.
When you perform any action, the page reloads, then pageblocktable loads and getlistAttachs() get invoked, hence it fetches the default values in the listAttachWrapper.

public List<attWrapper> getlistAttachs(){
        return listAttachWrapper;
    }
This should be the code for getlistAttachs() method.

if (listAttachWrapper==null){
            listAttachWrapper=new List<attWrapper>();
            for (Attachment a:[SELECT Name,Description FROM Attachment WHERE ParentId=:case1.Id])
                listAttachWrapper.add(new attWrapper(a));
        }
Include the above code in contsructor or other method.

Hope this Helps.

Regards,
Amritesh
Anto HotelbedsAnto Hotelbeds
Hi

I did the change as indicated. I added in the constructor method the second part:

//constructor -- gets case values
    public CaseExtensionXML(ApexPages.StandardController stdController) {
        this.case1 = (Case)stdController.getRecord();
        if (case1.ID!=NULL){
                //,Send_Comments_to_Contact__c
                this.case2= [Select CreatedDate,Id,XML_Email_Contact_1__c,XML_Email_Contact_2__c,XML_Email_Contact_3__c,XML_Email_Contact_4__c,
                Last_Comment__c,Emails_Comments__c,Date_Time_Last_Communication_Sent__c,ParentId,Temp_Module_Case_Type__c,ContactId,Comments_for_Admin_History__c,Description,XML_Email_Contact_5__c,AccountId,CaseNumber,RecordTypeId,OwnerId,Case_Type_ClientIntegrations_temporary__c,Module_Release__c,Module__c,Status, Solution_Sub_Type__c,Comments_for_Admin__c,Case_subtype__c,Subject,CCopied_Addresses__c,To_Addresses__c,Temp_Module_Case_Type_Case_Subtype__c,SuppliedEmail  From Case Where (Id=:case1.Id)];
        }
        //Date CreatedDate=[SELECT CreatedDate From Case Where (Id=:case1.Id)];
        this.controller = controller;
        string URLTemp=string.valueOf(url.getCurrentRequestUrl());      
        if (URLTemp.contains('NewXMLCaseSM')){
            String AccountID=ApexPages.currentPage().getParameters().get('def_account_id');
            case1.AccountId=AccountID;
        }
        if (URLTemp.contains('ShowSubtype')){
                ShowSubtype=True;
        }
        
        if (URLTemp.contains('MarkNoXML') && case2.ContactId==NULL) this.EmailAddress=case2.SuppliedEmail;
        
        if (apexpages.currentPage().getParameters().get('SolutionSubtype')!=NULL){
                case1.Solution_Sub_Type__c=apexpages.currentPage().getParameters().get('SolutionSubtype');
        }
        if (apexpages.currentPage().getParameters().get('MainParentCase')!=NULL){
                case1.ParentId=apexpages.currentPage().getParameters().get('MainParentCase');
        }
        
        // instantiate the list with a single attachment
        newAttachments=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsIT=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsDestOffice=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsSuppInt=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsWebSupp=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsSalesSupp=new List<Attachment>{new Attachment()};
        // instantiate the list with a single attachment
        newAttachmentsCliOpt=new List<Attachment>{new Attachment()};
        
        if (listAttachWrapper==null){
                listAttachWrapper=new List<attWrapper>();
                for (Attachment a:[SELECT Name,Description FROM Attachment WHERE ParentId=:case1.Id])
                                listAttachWrapper.add(new attWrapper(a));
        }
    }

And substitue the getListAttachs by the following:

public List<attWrapper> getlistAttachs(){

        return listAttachWrapper;
    }

But I am still getting false in the listAttachWrapper as you can see in the debug log:

List of members: (attWrapper:[att=Attachment:{Name=CAtegorizacion Priotiry CRC.xlsx, Id=00PL00000013qoIMAQ}, isChecked=false], attWrapper:[att=Attachment:{Name=Belbin Questionnaire_crm team.doc, Id=00PL00000013sAtMAI}, isChecked=false])

I really dont understand what I am doing wrong.....
Anto HotelbedsAnto Hotelbeds
Any other idea please?
Amritesh SahuAmritesh Sahu
Hi Anto,

Replace 
<apex:pageBlockTable value="{!listAttachs}" var="item">
to
<apex:pageBlockTable value="{!listAttachWrapper}" var="item">

This should solve the issue.

Regards.
Anto HotelbedsAnto Hotelbeds
Great! I made the change as you said and I also changed the visualfoce because I had the button code in one form and the wrapper class in another <apex:form> and now its working ok!

So now the visualforce page looks like:

<apex:page standardController="Case" showHeader="true"
    extensions="CaseExtensionXML">

    <!-- Section Header -->
    <apex:sectionHeader title="XML Case Communicate Client"
        subtitle="{!Case.CaseNumber}" />

    <!-- Form -->
    <apex:form title="Communicate Client">
        <apex:pageblock title="Case Description and Comments">
            <apex:pageBlockSection id="CaseDescription" columns="2"
                collapsible="true" title="Case Description">
                <!-- cambiar color letras header -->
                <apex:facet name="header">
                    <span style="color: #222222">Case Description</span>
                </apex:facet>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Case Description" />
                    <apex:outputField value="{!Case.Description}" />
                </apex:pageBlockSectionItem>
                <!-- This script sets up the section to be collapsible -->
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseDescription}');
             </script>
            </apex:pageBlockSection>

            <apex:pageBlockSection id="CaseComments" columns="1"
                collapsible="true" title="Case Comments">
                <apex:facet name="header">
                    <span style="color: #222222">Case Comments</span>
                </apex:facet>
                <apex:include pageName="CommentsTable2" />
                <script>
                function customToggleSection(el){
                    if(typeof twistSection === 'function'){
                        var theTwist = document.getElementById(el);
                        if(theTwist){
                            twistSection(theTwist.childNodes[0].childNodes[0]);
                        }
                     }
                 }

                 customToggleSection('{!$Component.CaseComments}');
             </script>
            </apex:pageBlockSection>

        </apex:pageblock>

        <apex:pageBlock title="Communicate Client" id="XML_Communicate_Client"
            mode="Edit">
            <!-- Error messages -->
            <apex:pageMessages />

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

                <apex:pageBlockSection columns="1">
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Type of Communication" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputField value="{!Case.Type_of_Communication__c}" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>


                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Comments" />
                        <apex:outputPanel >
                            <div class="requiredInput">
                                <div class="requiredBlock"></div>
                                <apex:inputTextarea value="{!Case.Comments_for_Admin__c}"
                                    style="width:700px; height:300px;" />
                            </div>
                        </apex:outputPanel>
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Send Case Historical Data?" />
                        <apex:inputField value="{!Case.Send_historical_data__c}" />
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
        </apex:pageBlock>


    <apex:pageBlock title="Existing Case Attachments" id="attachpageblock">
        <apex:pageBlockTable value="{!listAttachWrapper}" var="item" id="attachtable">
            <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>
    </apex:form>
</apex:page>


To whom it may help, remember to keep the button and the list where you are using the wrapper class under the same <apex:form>!!

Thanks to eveyrone for your help!

This was selected as the best answer