I have two objects, Parent__c
and Child__c
, Child__c
has a lookup field to Parent__c
calledParentId__c
.
I have overridden the New and Edit pages for Child__c
using a Visualforce page with the Child__c
standard controller.
<apex:page standardController="Child__c" extension="ParentChildExtension">
<apex:sectionHeader title="{!Child__c.ParentId**r.Name}"/>
</apex:page>
If I click the Edit button on the Child__c
related list to access my page, the apex:sectionHeader
title is populated. However when I use the New button it is not.
I've tried using the addFields
method in my controller to add the Name
(and other fields) from Parent__c
that I'm interested in but this still does not work when using the New button.
Edit Button Behaviour
Visualforce Page
New Button Behaviour
Visualforce Page
This is easily solved by querying for the Name
field of Parent__c
in a controller extension (if it's missing), but is there a way of doing this that without using a controller extension and/or a SOQL query (and preferably without resorting to the "lkid hack" mentioned in Keith C's answer)?
public with sharing class ParentChildExtension
{
private final Child__c child;
private String parentName;
public ParentChildExtension(ApexPages.StandardController stdController)
{
this.child = (Child__c)stdController.getRecord();
this.parentName = child.ParentId**r.Name;
}
public String getParentName()
{
if(parentName == null)
{
parentName = [SELECT Name FROM Parent__c WHERE Id = :child.ParentId__c].Name;
}
return parentName;
}
}
EDIT: Added stipulation that I would prefer to avoid the "lkid hack" if possible, and that I've tried using the addFields
method in my controller.