1

I am trying to insert a record into a child custom object using the external id of the parent custom object. I am almost clear on how to setup the external id field in the parent object, but very confused with how to setup a foreign key in my child object. I am attaching the pics here which has details of the master and child custom objects. What I am i supposed to add to the child custom object to link its records to the parent object.

Also here is the code I got from another post in stackoverflow, but that didn't work in my case

enter image description heresumchans__City_Master__c city = new sumchans__City_Master__c(Name='[![enter image description here][1]][1]Vancouver',sumchans__PROVINCE__c = 'BC', sumchans__City_Name_Ext_Id__c = 'Vancouver');
insert city;
sumchans__City_Stat__c cityStat = new sumchans__City_Stat__c(Name = 'Vancouver', sumchans__ON_BILLINGS__c = 50, sumchans__City_Master__c=new sumchans__City_Master__c(sumchans__City_Name_Ext_Id__c='Vancouver'));
insert cityStat;

enter image description here

Sumchans
  • 3,088
  • 6
  • 32
  • 59
  • Why do you use ExternalID? Because **External ID** field normally used to reference an ID from another external system. – Noor A Shuvo Feb 02 '20 at 07:19

2 Answers2

1

Create a lookup field instead. Because External ID field used to reference an ID from another external system.

If you want to relate the foreign key with and relational DBMS, you need to create a lookup relationship type field in the child object. There you need to assign the Parent Object in the Related To field.

Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48
1

Ext. ids are meant to be used in integrations, Data Loader... They can be used in Apex too but the syntax looks bit funny. Use them in Apex if you must save on queries or want to go "dear salesforce, I don't remember if I saved this object already, I know unique identifier is 123, go figure it out yourself whether it needs insert or update".

If you insert parent and child in same transaction - just user the Id generated into parent record. If they're separate, a while ago you did this:

insert new sumchans__City_Master__c city = new sumchans__City_Master__c(Name=Vancouver',
    sumchans__PROVINCE__c = 'BC',
    sumchans__City_Name_Ext_Id__c = 'Vancouver'
);

Then later in another transaction you can do this without querying:

sumchans__City_Stat__c cityStat = new sumchans__City_Stat__c(Name = 'Vancouver', 
    sumchans__ON_BILLINGS__c = 50, 
    sumchans__City_Master__r = new sumchans__City_Master__c(
         sumchans__City_Name_Ext_Id__c='Vancouver'
    )
);
insert cityStat;

Note that I used __r instead of __c. __c would be your real foreign key, Id field, 15 or 18 characters. __r would be a reference to another object (with ext. id or query result for example), similar to writing SELECT FirstName, Email, Account.Name FROM Contact.

In Apex it has to be as a object referece (so either query or cheat with new keyword, just to create it in memory. You won't actually save that "parent"). In things like data loader you can map values bit like myCsvExtIdColumn -> sumchans__City_Master__r.sumchans__City_Name_Ext_Id__c

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • The reason why I used external ID is because I would be needing to save to child object alone later with the ID from the parent. As Salesforce id's are not easy to find that's why I used external ID. Also when using external IDs how do we create a foreign key in the child, is it a master detail relationship or lookup relationship. I was following Bob buzzards blog. – Sumchans Feb 02 '20 at 12:35
  • You still need to make a real relationship, lookup or M-D, you choose. External id in parent will "only" give you bit of flexibility by populating the link by ext I'd like I have shown or by the real SF parent record's Id. Start with lookup and see how it works. In your business case it's not possible to have children without parent, right? Perhaps M-D will be closer to what you need, there are some considerations to both relationship types. – eyescream Feb 02 '20 at 14:09
  • Thanks @eyescream, That above code using external id worked out for me, also checking how do I pull related records from the child object using this realtionship. This is what I tried SELECT Id,Name,sumchans__PROVINCE__c,sumchans__City_Stat__r.sumchans__ON_BILLINGS__c FROM sumchans__City_Master__c where sumchans__City_Name_Ext_Id__c='Winnipeg' – Sumchans Feb 02 '20 at 20:17
  • Thanks @eyescreak, I was able to figure it out. This is what I tried SELECT Id,Name,sumchans__PROVINCE__c,(SELECT sumchans__ON_BILLINGS__c FROM sumchans__City_Stats__r) FROM sumchans__City_Master__c where sumchans__City_Name_Ext_Id__c='Vancouver' – Sumchans Feb 02 '20 at 20:37