0

I am trying to add custom visualforce page in standard contact page layout. For that I have created one VF page that has standardController set to Contact in order view it in contact's page layout.

<apex:page standardController="Contact">

</apex:page>

Now, the problem that I am facing is, I have a custom object name Call History Settings and I want to access the fields of Call_History_Setting__c object into the VF page that I have created with standardController

<apex:page standardController="Contact">

    //access Call_History_Setting__c

</apex:page>

I tried to add Call_History_Setting__c as lookup field in Contact object but could not access fields of Call_History_Setting__c in VF Page , for example Checked_Phone_Number_Fields__c is one of the fields of Call_History_Setting__c

<apex:page standardController="Contact">

    {! Contact.Call_History_Setting__c.Checked_Phone_Number_Fields__c}

    //Error unknown field Checked_Phone_Number_Fields__c

</apex:page>
1

This is addressed by including a controller extension. Create a controller extension, a POTATO (Plain Old Apex Object) Apex class with a signature and constructor of the form:

public with sharing class MyExtension {
    public MyExtension(ApexPages.StandardController stdController) {
        ...

That controller can obtain the contextual object (the contact) using:

Contact contact = (Contact) stdController.getRecord();

It can then perform whatever queries it needs to and/or provide whatever action functions/properties required to support your custom insert into the standard Contact page by making use of the controller extension by updating your page with:

<apex:page standardController="Contact" extensions="MyExtension" ...

And making use of the properties/action functions/whatever you expose on the controller extension.

See the documentation for more on controller extensions.

That said, if you are able, I suggest you move to using LWC components instead. These are actually easier to create and maintain and look (and perform) better generally. They are, also, Salesforce's replacement for the replacement for Visualforce, which tells you that Visualforce isn't going to get any more investment and will eventually be phased out...

| improve this answer | |
  • using this i will be able to access records of another custom object ? and do i need to create lookup field for that ? – Hunt Oct 3 '19 at 9:41
  • 1
    You can perform whatever queries you need to get hold of whatever data you want to present. There would typically be some form of relationship between the Contact and the other data, but it doesn't have to be direct - you just have to be able to get the data queried in one or more steps... – Phil W Oct 3 '19 at 9:43
  • 1
    E.g. using the contact retrieved from the standard controller in a query like [SELECT Id, X__c, Y__c, Z__c FROM MyObject__c WHERE Contact__c = :contact.Id] – Phil W Oct 3 '19 at 9:46
  • 1
    But... if you are new to Visualforce and you are supporting orgs running Lightning Experience, I would suggest you avoid further Visualforce learning and instead invest your efforts in using LWC to create the page extensions you need instead. – Phil W Oct 3 '19 at 9:47
  • Let me give a try with the extension – Hunt Oct 3 '19 at 10:07

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.