function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ravi-SFDCRavi-SFDC 

first error: INVALID_FIELD, Foreign key external ID: tec.com not found for field abc_Email_ID__c

Requirement :- I have two objects Nomination__c and Participant__c. Hight level these object are related to Training.

Participants get nominated for Trainings. Nomination has capture all details of Participants so that if participants does not exist then it create new participants. abc_Email_ID__c is the field which we use to check if participant exist or not. "abc_Email_ID__c" is external id in Nomination and Participant__c.

 

I want ot use cascade inset functionality in Berfore Insert trigger on Nomination.Idea is insert Participant__c and associate atutomatically with Nomination when Nomination record is inserted.

Below is the sample code i am trying to run through Anonymous block when i am running it gives me following error

 

|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD, Foreign key external ID: testing119@abc.com not found for field abc_Email_ID__c in entity Participant__c: []

 

Any idea why i am getting this error

 

code :-

 

Nomination__c nomiObj = new Nomination__c();
nomiObj.Employee_Name__c='testing119';
nomiObj.abc_Email_ID__c='testing119@abc.com';
nomiObj.Training_Name__c='a01Z000000O8EeS';
Participant__c partObj = new Participant__c();
            
            partObj.abc_Email_ID__c=nomiObj.abc_Email_ID__c;
             
             nomiObj.EmployeeID__r=partObj;
insert nomiObj;

JHayes SDJHayes SD

Is the field 'abc_Email_ID__c' on Nomination__c a string? If it's a lookup field, 'testing119@abc.com' won't work as it isn't a valid record ID.  

Ravi-SFDCRavi-SFDC
Its a Email (Unique) type field.
JHayes SDJHayes SD
What about on Participant__c ?
Ravi-SFDCRavi-SFDC
Same type Email (External ID) (Unique)
Avidev9Avidev9
Ok since you are doing an cascade insert there should be a nomination record with this value abc_Email_ID__c='testing119@abc.com';

I dont think such a record is present and hence lookup fails and the code is throwing error.
Please check if you have a record with the above value
Ravi-SFDCRavi-SFDC
Didnt get you. Idea is to insert both object record in one go. When i am inserting nomination , I also want to insert Participant based on external id which is email.
If you see my code -- line number 3, I am setting email address in nomination and with same email address i want to create Participant object. In line number 8 I am inserting nomination with the assumption that it will also insert Participant

1 Nomination__c nomiObj = new Nomination__c();
2 nomiObj.Employee_Name__c='testing119';
3 nomiObj.abc_Email_ID__c='testing119@abc.com';
4 nomiObj.Training_Name__c='a01Z000000O8EeS';
5 Participant__c partObj = new Participant__c();

6 partObj.abc_Email_ID__c=nomiObj.abc_Email_ID__c;

7 nomiObj.EmployeeID__r=partObj;
8 insert nomiObj;
Avidev9Avidev9

You never inserted Paticipant in your code

The Idea is it is pretty straight forward

Nomination__c nomiObj = new Nomination__c();
nomiObj.Employee_Name__c='testing119';
nomiObj.abc_Email_ID__c='testing119@abc.com';
nomiObj.Training_Name__c='a01Z000000O8EeS';
insert nomiObj;

Participant__c partObj = new Participant__c();
partObj.Nomination__c = nomiObj.Id; //I am assuming Nomination__c is the api name of the lookup field from participant to nomination

insert partObj ;

 

". In line number 8 I am inserting nomination with the assumption that it will also insert Participant" do you have any link to this ? Kinda salesforce doc or blog, that states it is possible ?