function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jpbenjpben 

Contact list on Case

I would like a VF page block in the Case page.  It will have a list of Contacts related to the Account that has been selected on the Case.  Can this be done with a standard contoller or do i need a custom controller.  Can anyone assist with a sample code or point me to a resource. Thanks. 

Best Answer chosen by Admin (Salesforce Developers) 
wajidawajida

Hi Here is your solution. you can then embed this vf page in the case layout

 

public class ContactsForCase {
  public case theCase;
  public Account acc{get; set;} 
  
  public ContactsForCase (apexpages.standardcontroller controller){
   
     theCase = [select id, accountid from case where id =:apexpages.currentpage().getparameters().get('id')];
         
     acc = [select id, name from account where id =:theCase.accountid];    
   }
   public list<contact> getContacts(){
  
    list<contact> myContact = [select id, accountid, name, phone, email from contact where accountid = :acc.id];
    return myContact;
     }    
}

 

 

 

 

 

 

<apex:page standardController="case" extensions="ContactsForCase">
  <apex:pageBlock >
    <apex:pageBlockSection >
       <apex:pageBlockTable value="{!contacts}" var="c">
          <apex:column headerValue="Name">{!c.name}
           </apex:column> 
           <apex:column headerValue="Email">{!c.email}
           </apex:column>
             
           <apex:column headerValue="Phone">{!c.phone}
           </apex:column>   
       </apex:pageBlockTable>
     </apex:pageBlockSection>
  </apex:pageBlock>
</apex:page>

All Answers

AdrianCCAdrianCC

Hello,

 

apex:relatedList seems to be the simplest way. Use the standardController for Account and pass the AccountId as a parameter in the URL.

Link: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_relatedList.htm

 

Thanks,

Adrian

jpbenjpben

This is not quite what I was looking for. I need the VF page on the Case page.   I can't use Account as the standard controller. 

AdrianCCAdrianCC

Sorry, I was thinking that you can somehow fool the layout editor into taking a vf page with a custom link... where you could of added the related Account's Id. You're right it is not possible.

 

So the other solution is to use the standard controller of the case - > this ensures that the page can be added to the layout. See here: http://blogs.developerforce.com/systems-integrator/2008/11/adding-a-visualforce-page-to-a-page-layout.html

 

You'll need an extension. Link : http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

 

In the constructor of the extension you'll need to retrieve via a soql the Contacts:

public List<Contact> contactsList 		{get; set;}

public myControllerExtension(ApexPages.StandardController stdController) {
    this.cs = (Case)stdController.getRecord();

    List<Case> casesList = [SELECT Id, AccountId FROM Case WHERE Id=:cs.Id LIMIT 1];//I'm not sure if the stdController contains ready available all the fields, especially if they're not used in the page
    contactsList = [SELECT Id, FirstName, LastName FROM Contact WHERE AccountId=:casesList[0].AccountId];
}

 In your page you'll need to show the list either using apex:repeat or apex:dataTable. Link: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_dataTable.htm

 

Hope this helps,

Adrian

wajidawajida

Hi Here is your solution. you can then embed this vf page in the case layout

 

public class ContactsForCase {
  public case theCase;
  public Account acc{get; set;} 
  
  public ContactsForCase (apexpages.standardcontroller controller){
   
     theCase = [select id, accountid from case where id =:apexpages.currentpage().getparameters().get('id')];
         
     acc = [select id, name from account where id =:theCase.accountid];    
   }
   public list<contact> getContacts(){
  
    list<contact> myContact = [select id, accountid, name, phone, email from contact where accountid = :acc.id];
    return myContact;
     }    
}

 

 

 

 

 

 

<apex:page standardController="case" extensions="ContactsForCase">
  <apex:pageBlock >
    <apex:pageBlockSection >
       <apex:pageBlockTable value="{!contacts}" var="c">
          <apex:column headerValue="Name">{!c.name}
           </apex:column> 
           <apex:column headerValue="Email">{!c.email}
           </apex:column>
             
           <apex:column headerValue="Phone">{!c.phone}
           </apex:column>   
       </apex:pageBlockTable>
     </apex:pageBlockSection>
  </apex:pageBlock>
</apex:page>

This was selected as the best answer
jpbenjpben

Thanks