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

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?

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>
  • Hey Santanu - Thanks for this. I only get this error now. Unknown property 'accountName' referenced in AccountVF – user4567570 May 12 '17 at 5:51
  • This piece code works fine at my DE. Dont know how you are getting this – Santanu Boral May 12 '17 at 5:54
  • Let me check why its causing problem to me. – user4567570 May 12 '17 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 ? – user4567570 May 12 '17 at 6:05
  • Here getting value from pageReference is not good approach – Santanu Boral May 12 '17 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>

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.