0

I have a nested table, the third column displays the Child records related to the Parent Account. I am using a wrapper class to get the selected row but now i need to get the selected records from the Child object. Please see image. I was wondering if this is even possible, if so, Can anybody please help me with the controller. I will truly appreciate any help!

Page:

 <apex:page controller="Class_ParentChild" Tabstyle="Account">

 <apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons >
            <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
        </apex:pageBlockButtons>
        <!-- In our table we are displaying the Account records -->
        <apex:pageBlockTable value="{!Accounts}" var="c" id="table">
            <apex:column >
                <!-- This is our selected Boolean property in our wrapper class -->
                <apex:inputCheckbox value="{!c.ParentSelected}"/>
            </apex:column>
            <!-- This is how we access the Account values within the container/wrapper -->
            <apex:column value="{!c.con.Id}" />
            <apex:column value="{!c.con.Name}" />

            <!-- This is how we access the NRPRODUCTS values container/wrapper -->
           <apex:column >
           <apex:pageBlockTable value="{!c.con.NRProducts__r}" var="con" border="1"> 
           <apex:column >
                 <apex:inputCheckbox value="{!c.ChildSelected}"/>
            </apex:column>
                <apex:column value="{!con.Name}"/>
           </apex:pageBlockTable>
           </apex:column>

        </apex:pageBlockTable>


    </apex:pageBlock>
</apex:form>

Controller:

public class Class_ParentChild
{


public List<cAccount> accountList {get; set;}

public List<cAccount> getAccounts() {
    if(accountList == null) {
        accountList = new List<cAccount>();
        for(Account c: [select Id, Name,(Select Id,name From NRProducts__r) from Account]) {

            accountList .add(new cAccount(c));
        }
    }
    return accountList;
}


public PageReference processSelected() {

     //We create a new list of Account that we be populated only if they are selected
    List<Account> selectedAccounts = new List<Account>();


    //We will cycle through our list of Account

   for(cAccount cCon: getAccounts()) {

        if(cCon.ParentSelected == true) {
              selectedAccounts.add(cCon.con);


        }
    }


    accountList=null; 
    return null;
}


// This is our wrapper/container class.

public class cAccount {
    public Account con {get; set;}

    public Boolean ParentSelected {get; set;}
    public Boolean ChildSelected {get; set;}


    public cAccount(Account c) {
        con = c;

        ParentSelected = false;
        ChildSelected = false;
    }
}

}

Image:

enter image description here

2
  • Hello Carlos, as I understand your goal is to have possibility to pick individually each child record? Feb 11 '15 at 8:09
  • Hi Artur, thank you for responding.. yes that is my goal.
    – Carlos
    Feb 11 '15 at 16:54
0

To achieve this you should change approach.

First create wrapper class for Account(you already have one). Then create wrapper class for contacts.

Account wrapper class should contains list of contact wrapper objects, thats how you can select each one of them.

Here is some example - It may or may not work as it is (it was written in NotePade :) )

Class:

public wrapperAccount[] accs{get; set;}

[......]

// this methode collect all records
public PageReference processSelected() {

   for(wrapperAccount wa : accs) {
        if(wa.isSelected) {
            // do something
        }
        else{
            continue;
        }

        for(wrapperContact wc : wa.contacts){
            if(wc.isSelected){
                // do something
            }
        }
    }

    return null;
}

[......]
// wrapper class for Accounts 
public class wrapperAccount {
    public Account acc {get; set;}
    public wrapperContact[] contacts {get; private set;}
    public Boolean isSelected {get; set;}

    public cAccount(Account c) {
        acc = c;
        isSelected = false;
    }
}
// rapper class for Contacts
public class wrapperContact{
    public contact con{get; set;}
    public Boolean isSelected {get; set;}

    public wrapperContact(Contact c) {
        con = c;
        isSelected = false;
    }
}

Page:

<apex:pageBlockTable value="{!accs}" var="acc">
    <apex:column >
         <apex:inputCheckbox value="{!acc.isSelected}"/>
    </apex:column>
    <apex:pageBlockTable value="{!acc.contacts}" var="con"> 
        <apex:column >
            <apex:inputCheckbox value="{!con.isSelected}"/>
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlockTable>
1
  • Artur, thank you so much! I got it working with your logic. I really appreciate it! Have a great week ahead !!
    – Carlos
    Feb 16 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.