[SalesForce] Referencing a Custom Field in a Master-Detail Relationship

I have a new custom object (let's call it fancyobject) that I want to have use Accounts as parents (i.e. there will be many fancyobjects for each account).

The purpose of this is to upload data from a product on a daily basis. The data upload will include an external unique ID, but not the account name. The external unique Id has been created in the Account object and listed as a unique identifier.

But I can't figure out how to make that unique ID the link in my master-detail relationship with the Accounts. I want the fancyobject to belong to an account. But I need the look-up link (to establish which account is the parent after upload) to be the unique ID field. Is there a way to do this?

Best Answer

Using apex you can populate parent lookup value without querying parent object using below syntax,

Account acc = new Account(ExternalId__c = 'EXTERNAL_ID_HERE');

//create an instance of Opportunity, and point the lookup to the instance created above.    
Facy_Object__c fancy = new Fancy__c(Name = 'Test fancy', Account__r = acc );

If you write this code as a trigger on before insert and before update, I think you can populate the lookup value. But it will not let you do from Salesforce UI, because page level validations will prevent you to call Save method.

Related Topic