Get RowId from Parent Class

I´m having some issues here, I have a custom controller wich wrapps certain object and shows that object in a table, each row has their own update – delete commandLink, I want to access each record when they click on the link but, I cannot access to the Id from the parent Class, when I pass the parameter to the controller it can be called from the inner Class where the object is wrapped but from the parent class it shows as null. Any suggestions here??

this is how the table looks like

<table border="1"  cellpadding="4" width="650">
        <thead>
            <tr>
              <th BGCOLOR="#8FBC8F" colspan="7">CompanyName/CSWM Accounts, as of 03/31/2013</th>
            </tr>
        </thead>
        <tr>
            <th BGCOLOR="#6495ED">Account Number</th>
            <th BGCOLOR="#6495ED">Account Title</th>
            <th BGCOLOR="#6495ED">Account Designation</th>
            <th BGCOLOR="#6495ED">Account Type</th>
            <th BGCOLOR="#6495ED">Open Date</th> 
            <th BGCOLOR="#6495ED" colspan="2">Action</th>
        </tr>
       <apex:repeat value="{!Accounts}" var="c" id="table">
        <tr id="!rowNum}">
            <td>{!c.con.Account_Number__c}</td>
            <td>{!c.con.Name}</td>
            <td>{!c.con.Account_Designation__c}</td>
            <td>{!c.con.Account_Type__c}</td>
            <td><apex:outputText value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!c.con.Open_Date__c}"/></apex:outputText></td>
            <td><apex:commandLink value="Update" action="{!readCellMethod2}" reRender="table"/>

                <apex:param name="clickedRow" value="{!c.con.Id}" assignTo="{!c.clickedRowId}"/>
            </td>
            <td>
                <apex:commandLink value="Delete"/>
            </td>
        </tr>
        </apex:repeat> 
    </table>

this is part of the controller:

public with sharing class custController {

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


    public Employees__c employee {
        get{
            if(employee == null){
                employee = [SELECT Id, Name, Employee_ID__c FROM Employees__c WHERE User__c =: UserInfo.getUserId() ];
            }
            return employee;
        }
        set;
    }

    public List<cAccount> getAccounts() {

    if(accountList == null) {
        accountList = new List<cAccount>();
        for(Account__c c: [SELECT Name, Open_Date__c, Employee_Name__c, Account_Number__c, Account_Type__c, Account_Designation__c 
                            FROM Account__c 
                            WHERE Employee_Name__c =: employee.Id
                            ORDER BY CreatedDate DESC]) {
            accountList.add(new cAccount(c));
        }
    }
    return accountList;
}

    public class cAccount {

        public Account__c con {get; set;}
        //clickedRowId from inner class
        public String clickedRowId {get; set;}

         //this method works and shows the current rowId
         public PageReference readCellMethod(){
        System.debug('#### clickedRowId: ' + clickedRowId);         
        return null;
     }

         public cAccount(Account__c c) {
            con = c;
            clickedRowId = c.Id;
         }  
    }

    //this outer method shows null value
    public PageReference readCellMethod2(){

        System.debug('#### clickedRowId2: ' + clickedRowId2);           

        return null;
    }
 }

I want to access that variable from an outer method to assign the record value to the account to be updated in the next PageReference UpdateAccount

Answer

You should remove clickedrowid from your innerclass and at the top of your class define it like public id clickedrowid {get; set;}.
Then in your vf page change the assignTo to assignTo='{!clickedrowid}'.
Don’t use your inner class for the assignment. And also use the parameter in actions outside the innerclass.

I reworte it a bit because you don’t really need the innerclass;

   <apex:repeat value="{!Accounts}" var="c" id="table">
    <tr id="!rowNum}">
        <td>{!c.Account_Number__c}</td>
        <td>{!c.Name}</td>
        <td>{!c.Account_Designation__c}</td>
        <td>{!c.Account_Type__c}</td>
        <td><apex:outputText value="{0,date,MM'/'dd'/'yyyy}"><apex:param value="{!c.Open_Date__c}"/></apex:outputText></td>
        <td><apex:commandLink value="Update" action="{!readCellMethod2}" reRender="table"/>

            <apex:param name="clickedRow" value="{!c.Id}" assignTo="{!clickedRowId}"/>
        </td>
        <td>
            <apex:commandLink value="Delete"/>
        </td>
    </tr>
    </apex:repeat> 
</table>



public with sharing class custController {

public List<Account__c> accountList {get; set;}
public String clickedRowId {get; set;}



public Employees__c employee {
    get{
        if(employee == null){
            employee = [SELECT Id, Name, Employee_ID__c FROM Employees__c WHERE User__c =: UserInfo.getUserId() ];
        }
        return employee;
    }
    set;
}

public List<Account__c> getAccounts() {

if(accountList == null) {
    accountList = [SELECT Name, Open_Date__c, Employee_Name__c, Account_Number__c, Account_Type__c, Account_Designation__c 
                        FROM Account__c 
                        WHERE Employee_Name__c =: employee.Id
                        ORDER BY CreatedDate DESC]l

}
return accountList;
}

public pagereference readCellMethod2 () {
    system.debug('clickedRowId: '+clickedRowId);
    return null;
}
 }

Attribution
Source : Link , Question Author : Emilio , Answer Author : Marcel Meijer

Leave a Comment