One of the custom objects I have is rendered as a Related List of Opportunity. This custom object also has a custom field of Opportunity lookup type. To create a record of this type I go to Opportunity -> Related List and click on New button.

What I want is : At the time of record creation I want to assign default values to certain fields. The way it should work is Mailing Address field should get its value from related Opportunity's primary Contact. I can achieve this in a trigger by executing a query like

SELECT MailingAddress 
  from CONTACT 
  WHERE id IN (SELECT contactID 
                FROM OpportunityContactRole 
                WHERE opportunityID = <variable> and isPrimary = true)

However, I want to do it by assigning a formula to the default value to Mailing Address filed directly. Is it possible?

Second part of this question is... While trying to set the default value for this custom field I tried something like Opportunity__c.Mailing_Address__c (This is a custom field on Opportunity), however the formula validation error I got was

Error: Field Opportunity__c does not exist. Check Spelling. Opportunity__c in fact exists as a lookup Opportunity Field in my custom object.

Any pointers are greatly appreciated.


5 Answers 5


A default value will not work in this case, because default values can't access values on a parent record. They are set at the schema level, and don't have the context of where the record is created (e.g. from which parent object).

Overriding the New action with a Visualforce page would work, but is not entirely optimal because this also overrides New everywhere it appears (not just on the custom object related list on the opportunity page layout). You would have to write logic in your controller to check which context New is being called in.

You could create a custom URL button that uses URLFOR() to call a Visualforce page, and only use it on the Opportunity page layout. You would then pass in the parent opportunity ID as a parameter to the VF page. The controller would then have to get the parameter and use it to set the field value in the VF page. (This is not the same as URL hacking, in which the field identifier is added to the query string and passed into the standard page layout. URL hacking is unsupported and will break as Salesforce moves toward Aura/Lightning for desktop pages.)

Another option, if you have Publisher Actions enabled in Chatter, is to create an object-specific quick action on the Opportunity object to create the custom object, and add it to the Chatter publisher. Effective Spring '15 you can span multiple levels of objects in predefined values, which would allow you to reach the Contact object on the Opportunity. You can't access the MailingAddress field directly (since it's an Address field type) but you can concatenate the MailingStreet, MailingCity and MailingState in the formula, which would look something like (for a TextArea):

Opportunity.Contact__r.MailingStreet & BR() 
& Opportunity.Contact__r.MailingCity & ", " & Opportunity.Contact__r.MailingState

The result would be something like this: enter image description here

The advantage to this method is (1) it's supported, (2) it doesn't require any Apex, and (3) it works in Salesforce1 (custom URL buttons aren't supported in S1 yet). The disadvantage is that you have to train users to use the quick action instead of the New button on the related list, but you could hide the New button on the related list to enforce that.

  • Oh - and to answer your second part, Opportunity__c is a custom field, but when you want to access fields on the related obejct you need to traverse the relationship, not the field itself. Relationships use the __r suffix. Feb 10, 2015 at 21:59

The best way I have encountered is detailed in this blog post:


Quick explanation: The best way to do what you want to do with a good user experience is to URL hack. Triggers don't tell the end user what fields will be populated for them and building a custom Visualforce page gets rid of many native Salesforce features.

The problem with URL hacking is that the parameters you need to use in the URL hack change between environments, so they always break in new Sandboxes or if you push from a Sandbox to Production.

The blog post above describes how to URL hack via an intermediate page that intelligently figures out what the field IDs are based on API names. Once you add the classes mentioned and then override the 'new' button as described with your own button, you can reliably pre-populate fields on the new object, or any new object for that matter. It is more work, but then your pre-populate functionality will not break between environments.

For your second question: try using Opportunity__r.Mailing_Address__c as Rao pointed out.


Prepopulating fields to other but (primitive) default values as you can define at setup will not work using a trigger. The trigger will be executed too late - namely short after you press save.

You have to override the New action with a visualforce page. Then in the controller you can create the logic and action function you need to populate your fields.


At the time of record creation I want to assign default values to certain fields

There are so many threads here that talk on how to assign default value to certain fields on a edit screen. You can go trigger route but passing params via URL would be convenient since the user gets to see what value is being written on the field.

ref: URL Hack: Pass additional parameters into the saveURL

however the formula validation error I got was "Error: Field Opportunity__c does not exist

To access related objects you have to use opportunity__r.[opportunity_Field_name] and not __c


Jason, Uwe, Shannon, Rao

Thanks a lot for your replies and time. Instead of going URL hack or publisher action route, I creaded a controller and VF page. That way I could address two additional requirements.

  1. Display clickable URL links at the time of record creation and
  2. Display the customized VF Page for one (out of four) different record types, while renderring system provided page for other three record types

Here is my code

" extensions="ctrlExt">

if({!redirect}) window.top.location.href='/a03/e?RecordType={!CustomObject.recordtypeid}&nooverride=1'
<apex:form >
     ... Custom VF Code for the record type ...

and here is the controller

public class ctrlExt { customObj__c c; public ctrlExt(ApexPages.StandardController ctrl) { c = (customObject__c) ctrl.getRecord();

public Boolean getRedirect() {
    RecordType rt = [<Query to get record type from RecordType table>];
    if (<Intended Record Type Name>.equals(rt.name))
        return false;
        return true;


Here is the link I found useful while writing this code

https://developer.salesforce.com/forums?id=906F000000094WRIAY One last note: when I tried to set default value for the address using Opportunity__r, I got error saying Opportunity__r does not exist.

Thanks again for your support, Shridhar

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .