2

I have a Custom Object in which a lookup field(Acclook) is related to account has been created. On a VF page We have an Inputfield which is a lookup(Acclook) for account. Once we click the Search button, based on the lookup field(Acclook) the related data(custom Object data) should be retrieved and displayed in a table. kindly guide me with any sample code or links if any.

0
1

You will want to create a command button to perform the search and use the reRender to rerender the results section. The following is a basic framework, but is definitely not the completed working version. That's up to you to do. :)

Your Visualforce will have some code like the following:

<apex:pageBlock>
<!-- Search --> 
  <apex:pageBlockSection title="Search Input" id="searchInput">
    <apex:inputField value="{!yourObj.AccountLookup__c}" />
    <apex:pageBlockSectionItem >
      <apex:outputText value=""/>
      <apex:commandButton 
            action="{!search}" 
            value="search" 
            reRender="searchResults"/>
     </apex:PageBlockSectionItem>
   </apex:pageBlockSection>

<!-- Results -->       
  <apex:outputPanel id="searchResults">
    <apex:pageBlockSection title="Search Results" rendered="{!yourResults.size > 0}">
        <!-- Display the results in whatever table you want, 
             possibly an apex:pageBlockTable 
        -->
    </apex:pageBlockSection>
  </apex:outputPanel>
</apex:pageBlock>

Your Apex Controller will have need to have a search method that returns a PageReference. In this case you can just return null to stay on the same page and only have the partial refresh of the sections you indicated should be reRendered. Here is some of the body of the controller.

YourObject__c yourObj;

public YourController() {
   yourObj = new YourObject__c(); // or get it from DB, somehow.
}

public PageReference search() {
  // Do your search based on the input value of 

  if (yourObj.AccountLookup__c == null) {
    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Account is required to search'));
  } else {
    // query the DB
    // set the yourResults list to have the results
    // alternatively, you might just have some 
    // fields that you are setting and displaying
  }
  return null;
}

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.