1

I am new to VisualForce and I am not able to get my head around how do I pass value from vf page to my apex class when I have list of data.

I am adding my code snippets below,

Vf Page:

<apex:page controller="PublicController">
<apex:form>
    <apex:pageBlock id="one">
        <apex:pageBlockTable value="{!credential}" var="account" id="theTable">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                <apex:outputText value="{!account.Username__c}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">Pass</apex:facet>
                <apex:outputText value="{!account.Password__c}"/>
            </apex:column>
            <apex:column > 
                <apex:commandButton value="click" action="{!LoginHere}"> 
                    <apex:param name="accid" value="{!account.Id}" assignTo="{!accid}" /> 
                </apex:commandButton>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

</apex:page>

Apex Class:

public class PublicController {

List<Credential__c> Credential=new List<Credential__c>();
public string accid {set;get;}

public void LoginHere(){
    System.debug(accid);
    for(Credential__c currCredential : credential){
        if(currCredential.Id == accid){
            System.debug('CurrCredential'+currCredential);
            break;
        }
    }
}

public List<Credential__c> getCredential(){

     Credential = [SELECT id,Username__c,Password__c FROM Credential__c];
     return Credential;
    
}

}

Do let me know if you need anything else from my side for better clarification.

0

2 Answers 2

1

Using some help I was able to find the issue,

I was missing rerender="resultPanel" in apex:commandButton.

Thank you for all the help!!!

0

List<Credential__c> Credential is not visible to the Visualforce runtime, because it is not written as public List<Credential__c> Credential { get; set; }. The variable must be public and have a getter and setter. In addition, if that were true, then getCredential() would no longer work. Also, you can use a Map to eliminate the for-if loop.

Your final code should look like:

public class PublicController {
  public List<Credential__c> credential { get; set; }
  public string accid {set;get;}

  // Public default constructor
  public void PublicController() {
    Credential = [SELECT id,Username__c,Password__c FROM Credential__c];
  }
  public void LoginHere(){
    System.debug(accid);
    Credential__c currentCredential = new Map<Id, Credential__c>(credential).get(accId);
  }
}

Alternatively, you could have written:

public void LoginHere() {
  Credential__c currentCredential = [
    SELECT Id, Username__c, Password__c
    FROM Credential__c
    WHERE Id = :accId
  ];
}

This would work with your current code:

public class PublicController {
  public string accid {set;get;}
  public void LoginHere() {
    Credential__c currentCredential = [
      SELECT Id, Username__c, Password__c
      FROM Credential__c
      WHERE Id = :accId
    ];
  }  
  public List<Credential__c> getCredential(){
       return [SELECT id,Username__c,Password__c FROM Credential__c];
  }
}

This second form has the advantage of not wasting Visualforce View State, which makes the page very marginally faster on large lists.

You must log in to answer this question.

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