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?

  • 1
    So do you mean that, once you insert the fancyobject records with unique id, the account lookup field in the fancyobject should autopopulate based on the unique id value? Jun 11 '15 at 19:41
  • Yes - originally I was going to just write a workflow to do that, but (I think) that for the upload to be saved there has to be a Master Object (i.e. specific account) populated. So then that might not work (assuming I am correct on that).
    – Greg P
    Jun 11 '15 at 19:44
  • 2
    Workflow will not work for this. You have to write trigger for this. Which autopopulates the account lookup field based on unique id value that will be in the fancy object record. Let me know if you face any difficulties while writing trigger. I have done this scenario couple of times. Jun 11 '15 at 19:47

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.


You don't need to write a trigger if you are able to make the matches before doing a bulk upload. You can get away with a process builder that fires a really quick Flow. Anytime a "fancy object" doesn't have an account lookup filled out, it queries the Accounts object with that ID as its filter criteria. It grabs that Id and then updates the "fancy object". Quite literally a two element Flow - Record Lookup for the ID and a Record Update to map the Account.Id field.

  • 1
    But in future if they enter fancy object records manually then trigger could help it in autopopulating. Correct me if I am wrong. Jun 11 '15 at 20:07
  • 1
    Also not sure if that would work for master detail relationship, since I believe the process builder won't fire until after the record is saved and it can't be saved until the value is set.
    – Jenny B
    Jun 11 '15 at 21:30
  • 1
    You could have a Custom "New Fancy Object" button that you press that has the 'needed' fields, and then before it 'creates' the record, it does this scan. So, that bypasses the 'master-detail' issue. Jun 12 '15 at 4:34
  • 1
    SFDC_BigDog, you are right. The trigger does the same thing in the long run. I'm simply presenting a simpler solution, but it does have its drawbacks with anything bulk Jun 12 '15 at 4:36
  • Thank you both for your help - I think between these two they are the answers I need. Really appreciate it!
    – Greg P
    Jun 15 '15 at 19:15

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.