You need to sign in to do that
Don't have an account?
VF Page Account Contact Role on Contact Standard Page Layout
Hi Everyone,
I am trying to build a VF Page that will sit in a standard Contact Page Layout. I know that Account Contact Role relates to Account and there is a related list on Accounts. What we are looking for is a way to show these Account Contact Roles on the Contact.
My best assumption is to create a VF Page that can be added to the standard page layout with a apex class related to the Account Contact Role where the ContactId from the AccountContactRole object is equal to the current Contact Id.
The following code is my VF page and Apex Class. I feel there may already be a solution for this so I am totally open to new suggestions or fixes to my current code and logic.
Thank You Very Much.
<apex:page standardcontroller="Contact" recordSetVar="contacts" extensions="AccountContactRoles"> <apex:pageBlock title="Account Contact Roles"> <apex:form id="theForm1"> <apex:pageBlockSection > <apex:dataList value="{!AccountContactRoles}" var="acr" type="1"> {!acr.Id} </apex:dataList> </apex:pageBlockSection> <apex:panelGrid columns="2"> <apex:commandLink action="{!previous}">Previous</apex:commandlink> <apex:commandLink action="{!next}">Next</apex:commandlink> </apex:panelGrid> </apex:form> </apex:pageBlock> </apex:page>
This is my VF Page Coding
public with sharing class AccountContactRoles { private final Contact acr; public Id cntId{get;set;} public AccountContactRoles(ApexPages.StandardSetController controller){ this.acr = (Contact)controller.getRecord(); } public ApexPages.Standardsetcontroller acrRecords{ get{ if(acrRecords == null){ cntId = ApexPages.currentPage().getParameters().get('Id'); acrRecords = new ApexPages.Standardsetcontroller( Database.getQueryLocator([Select Id, AccountId, ContactId, Role, IsPrimary From AccountContactRole Where ContactId=:cntId])); } return acrRecords; } private set; } public List<AccountContactRole> getAccountContactRoles(){ return (List<AccountContactRole>) acrRecords.getRecords(); } }
This is my Apex Class.
Assuming you don't need paging, you can assign a List of AccountContactRole objects to a property in your Apex class. The following worked for me:
Apex class:
Visualforce page:
You can insert your Visualforce page into the Contact layout via the layout editor in the UI.
Regards,
Jeremy
All Answers
Assuming you don't need paging, you can assign a List of AccountContactRole objects to a property in your Apex class. The following worked for me:
Apex class:
Visualforce page:
You can insert your Visualforce page into the Contact layout via the layout editor in the UI.
Regards,
Jeremy
This worked great. Thank you.
Could I ask for your help with my test class? My code is below but I can't get it to cover my class.
Glad it worked out! Check out the following document: http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Specifically, go to the section titled 'Test Methods and Visualforce Controllers'. You need to use the ApexPages class to create an instance of the page, which will in turn cover you for the controller.
Regards, jh
Thank you. By the way, here is a copy of the final code for the page and class.
If you are just displaying AccountContactRoles on the Contact page, you actually don't need any Apex at all. You can do it all with straight Visualforce.
Here's a pretty similar page that I have used in the past.
David,
I really like the idea of not having a class, my only problems is ordering the results. I will need to place an order to the records or even better have the ability to sort the columns. From everything that I have read I would need to have a class to make either happen.
Thoughts????
Thanks Dwayne
Depending on your comfort level with javascript or jQuery, you could just sort the table on the client side. I don't this will be a very large list, yes?
Possibility of up to 50 or so records. Our contacts can traverse multiple accounts giving them the ability to have many account contact roles.