0

How to pass the parameter or object value (could be custom object) to apex class from visualforce?

I saw some where they're using ApexPages.currentPage() etc, not sure how we can modify the below program.

The main issue which I am facing is I dont see any data coming on VF page, but query executes fine fetches the result. How to fixed this issue ?

public class ContactController {
    public String accountName {get; set;}
    public List<Contact> contacts {get; set;}

    public ContactController(){    
    }

    public void getContactUsingAccount(){
        contacts = [SELECT ID, FirstName, LastName, Phone FROM Contact where account.Name  = :accountName];
        System.debug('Contacts : '+contacts);
    }
}

and VF page

<apex:page controller="ContactController">
    <apex:form>
        <apex:pageBlock>
            Account Name : <apex:inputText value="{!accountName}" />
            <apex:commandButton value="Submit" reRender="outputContact" action="{!getContactUsingAccount}" />
        </apex:pageBlock>

        <apex:pageBlock>
            <b>Output Contact Details </b>
            <apex:outputText value="{!contacts}" id="outputContact" />
            <apex:pageBlockTable value="{!contacts}" var="c">
                <apex:column headerValue="FirstName" value="{!c.FirstName}" />
                <apex:column headerValue="LastName" value="{!c.FirstName}" />
                <apex:column headerValue="Phone" value="{!c.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

enter image description here

3 Answers 3

2

One item that jumped out at first look, you call reRender on a closed outputText

<apex:outputText value="{!contacts}" id="outputContact" />

Wouldn't you want to apply that to your pageBlockTable?

0
2

Following are the issues at your code.

  1. correct use of <apex:param> with assignTo.
  2. You have rendered the outputText outputContact from commandButton, but not rendered the table. That's why it is not refreshing the table data.

So, provide an id to the pageBlock table and try to rerender the pageblock table from commandButton.

Visualforce

<apex:form >
        <apex:pageBlock >
            Account Name : <apex:inputText value="{!accountName}" />
            <apex:commandButton value="Submit" reRender="outputContact,table1" action="{!getContactUsingAccount}">
                <apex:param name="accountName" assignTo="{!accountName}" value="{!accountName}"/>
            </apex:commandButton>
        </apex:pageBlock>


         <apex:pageBlock id="table1">
            <b>Output Contact Details </b>
            <apex:outputText value="{!contacts}" id="outputContact" />
            <apex:pageBlockTable value="{!contacts}" var="c">
                <apex:column headerValue="FirstName" value="{!c.FirstName}" />
                <apex:column headerValue="LastName" value="{!c.LastName}" />
                <apex:column headerValue="Phone" value="{!c.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
7
  • Hey Santanu - Thanks for this. I only get this error now. Unknown property 'accountName' referenced in AccountVF Commented May 12, 2017 at 5:51
  • This piece code works fine at my DE. Dont know how you are getting this Commented May 12, 2017 at 5:54
  • Let me check why its causing problem to me. Commented May 12, 2017 at 5:55
  • Good explanation. Now its working thanks. Any guidance on ApexPages.currentPage() if we can use PageReference and which one is better approach ? Commented May 12, 2017 at 6:05
  • Here getting value from pageReference is not good approach Commented May 12, 2017 at 6:08
0

for passing parameter values to controller you can use like below.

 <apex:pageBlock>
        Account Name : <apex:inputText value="{!accountName}" />

        <apex:commandButton value="Submit" reRender="outputContact" action="{!getContactUsingAccount}" />
        <apex:param name="accname" assignTo="{!accountName}" value="{!accountName}"/>
    </apex:pageBlock>
0

You must log in to answer this question.

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