Saturday, June 14, 2014

View All the Account Related Contacts in single VF page.


Hi,

I have one requirement that like Actvities Viewall functionality my client is asking me to display all the contact's in a single page. for example:-

Go to Account's Tab and click on account -->go to Activity History Related list and click on View all button.


Once you click on "View All" button it open a page with all the Activities.



like that we need to display a page with all the contact's of that account. to achieve for this we develop a VF page and Command link button.

Go to setup-> customize-> Contact->Buttons Links-> Create a  "New Button Link" like in the below screen shot.

save image
save image

once you create a button place the button in related list. and develop a VF page name as "Showallcontacts" 
save image

VF Page :

<apex:page controller="ShowAllContact" tabStyle="Account">
  <apex:form >
     <apex:sectionHeader title="Contact Details"/>
       
                 <center><apex:commandButton value="cancel" action="{!cancel}" /></center>
       
   <apex:repeat value="{!contlist}" var="c">
    <apex:pageBlock >
        <apex:pageBlockSection columns="1" >
            <apex:outputField value="{!c.FirstName}" />
            <apex:outputText value="{!c.LastName}" />
        </apex:pageBlockSection>
      </apex:pageBlock>
     
   </apex:repeat>
  <center><apex:commandButton value="cancel" action="{!cancel}" /></center>
  </apex:form>

</apex:page>


Controller Class:

public class ShowAllContact {

 string accid= ApexPages.currentPage().getParameters().get('accid');


public list<Contact> contlist{get;set;}

public ShowAllContact(){
  if(accid !=null )
  //contlist= [select id,firstname,lastname,phone,email from contact where accountid='0019000000wFr7E'];
  contlist= [select id,firstname,lastname,phone,email from contact where accountid=:accid];
  }
 
      public PageReference cancel() {
      URL currentURL = URL.getCurrentRequestUrl();
      system.debug('currentURL.getPath()->'+currentURL.getPath());
      account acc = new account();
       // Send the user to the detail page for the new account.
        PageReference acctPage = new PageReference('/'+accid);
        acctPage.setRedirect(true);
       
        return acctPage;

       
    }
   
}



once you develop all the code you can view all the contacts for an Account like this.

save image


 That's it.....
Enjoy






2 comments:

rajvivek said...

Hi srini,

Can you explain what is happening in the javascript of button.

what is typeOf(srcup==function) & alwer() ??

SalesforceTrudge said...

I realize this is a very old post, but (if possible) could you show the test class? I've got this working in my sandbox, but I cannot deploy until I have a test class with the proper code coverage. Thanks!

Post a Comment

 
| ,