Thursday, August 14, 2014

How to open child records list from custom button on parent object?

How to open child records list from custom button on parent object?

Hi,

In this post i am giving an example of how to display contact records in a VF page when we click on custom button on Accounts page. Once we click on Custom page it will call Visualforce page and display related contacts for the Account.

Step1: 

Goto Setup-->Customize-->Account--> Buttons Links and Actions-->Click on New Button Or Link-->Create a button with Label and Enter the code in Script Area.
"window.open('/apex/ShowContactsForAccountVF?acId={!Account.Id}'); " --> click on Save.

save image

 

Step2:
Once you are ready with custom button Goto Account Details Record-->Edit Page Layout-->Go to Button Section-->Drag the "Show Contacts" Button to Custom Buttons Area-->Save.


save image


once you save the page layout the button will display in Account Detail page like this.

save image


Step 3:
Develop controller Class for Visualforce page, here we need to capture the parameter of Account id from the Url like this  "accID=  ApexPages.currentPage().getParameters().get('acId');"

public class ShowContactsForAccountCLS {
 Private Id accID;
 public List<Contact> contactList{get;set;}
 public ShowContactsForAccountCLS(){
   contactList = new List<Contact>();
   accID=  ApexPages.currentPage().getParameters().get('acId');
   contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];
 }
}

Step 4: 
Develop a visualforce page with Custom controller and save it. here the page name should be same as in the button script. in my custom button script i gave the url as "/apex/ShowContactsForAccountVF?acId={!Account.Id}", So My page name should be ShowContactsForAccountVF.

Visualforce Page :

<apex:page controller="ShowContactsForAccountCLS">
<apex:pageBlock > 
 <apex:pageblockTable value="{!contactList}" var="cl">
   <apex:column value="{!cl.FirstName}"/>
   <apex:column value="{!cl.LastName}"/>
   <apex:column value="{!cl.Phone}"/>
   <apex:column value="{!cl.Email}"/>
 </apex:pageblockTable>
</apex:pageBlock>
</apex:page>


Output : once you click on "Show Contacts" on Account Detail page it will open the contact details like this.






That's it...


1 comments:

Post a Comment

 
| ,