I have the following objects:

  • Salesforce Contact
  • Custom Object Contact
  • Custom Object Training Record

A "Custom Object Contact" has a Lookup to a "Salesforce Contact". A "Custom Object Contact" will only ever be related to one "Salesforce Contact".

A "Custom Object Training Record" has a Lookup to a "Custom Object Contact". A "Custom Object Training Record" will only ever relate to one "Custom Object Contact". Multiple "Custom Object Training Records" will be associated with a "Custom Object Contact".

On the "Salesforce Contact" I would like to provide a related list or APEX page to show the "Custom Object Training Records". I'm pretty sure I can't do this with a simple Related List as the object is not directly related. Is this possible with APEX?

2 Answers 2


In your VF page simply use the following query example:

[Select Id, ...other fields... From Custom_Object_Training__c Where 
    Custom_Object_Contact__r.Contact_Lookup__c = :SFContactId]

Since you know the main Salesforce Contact Id, you can simply filter the records from the training object via the relationships. No need to start at the SF contact.

If you need to display fields from the Contact or custom contact, create formula fields to pull that data into the training record or use a separate query depending on your needs


Create a lookup on the training record to the Salesforce Contact and use PB (If it can populate a lookup - not sure) or a simple trigger to populate it. Then you can use the standard related list functionality. May be much simpler


You actually can get that related list on the standard Salesforce Contact...with a little work.

Since your Training_Record__c is related to a single Contact__c, and Contact__c is related to a single standard Contact, you could make a lookup field on Training_Record__c that points to Contact.

Once you have that, you can add the related list to your standard Contact page layout(s).

This field would need to be populated via trigger (or pretty much anything that isn't a workflow rule + field update, because WFR field updates cannot use a formula to assign the value for lookup fields), but the code is simple enough.

// I assume that the relationship field on Training_Record__c is 'Contact__c'.
// I also assume that the relationship field between Contact__c and Contact is also 
//   called 'Contact__c'
// It takes extra effort to mentally juggle whether or not you're using the standard
//   or custom version of an object, that's why re-using standard object names for
//   custom objects is a bad idea.
trigger SetLookupToStdContact on Training_Record__c (before insert, before update){
    // If your entire relationship hierarchy isn't established at the time that you
    //   insert Training_Record__c records, then you'll need another trigger
    //   to update child Training__c records when your Contact__c is related to a
    //   standard Contact.

    // before insert, the Training_Record__c records won't have their Id, but
    //   they can have Contact__c populated.
    Set<Id> customContactIds = new Set<Id>();
    for(Training_Record__c tr :trigger.new){


    Map<Id, Contact__c> customContactsMap = new Map<Id, Contact__c>([SELECT Id, Contact__c FROM Contact__c WHERE Id IN :customContactIds]);

    // Because we're doing this in before triggers, we can update the record in
    //   trigger.new to avoid needing DML
    for(Training_Record__c tr :trigger.new){
            tr.Standard_Contact__c = customContactsMap.get(tr.Contact__c).Contact__c;
  • I forget, can PB populate a Lookup field? If not I will remove that from my answer
    – Eric
    Aug 16, 2017 at 16:27
  • @Eric I've no idea. I've only used PB enough to know that it's generally a good idea to avoid PB if possible.
    – Derek F
    Aug 16, 2017 at 16:29
  • Yell that from the rooftop! So true
    – Eric
    Aug 16, 2017 at 16:30
  • PB would theoretically be okay if there's no other triggers or things like that. PB is usually fine for just "basic" stuff. Just don't combine PB with triggers.
    – sfdcfox
    Aug 16, 2017 at 16:43

You must log in to answer this question.

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