0

So I want to be able to leverage the ability to populate lookup fields via an External Id when creating a record from LWC. Typically this is how one would do that in Apex:

Child_Object__c c = new Child_Object__c(); c.Parent_Lookup__r = new Parent_Object__c(External_Id__c = 'foo'); insert c; //Parent_Lookup__c has its value populated now 

So with that, I have a simple @AuraEnabled method that takes in a simple sObject and performs an insert:

@AuraEnabled public static void insertRecord(Child_Object__c record){ insert record; } 

And this is how I am creating that record in my LWC:

const record = { sobjectType: "Child_Object__c" }; record["Custom_Field__c"] = 'foo'; record["Parent_Lookup__r"] = { sobjectType: "Parent_Object__c", External_Id__c: "bar" }; 

But when I pass this through, I am getting the error:

More than 1 field provided in an external foreign key reference in entity

So I then remove the sObjectType key from the JS Object, and then I get Unable to identify SObject

Has anyone been able to perform this?

4
  • It'd be interesting to see the result of System.debug(JSON.serialize(record)); Maybe you just need to make a new instance of the object and manually populate fields. Commented Nov 5 at 14:59
  • Which JS object did you remove the sobjectype key from? Commented Nov 6 at 2:26
  • @prem22 from this record["Parent_Lookup__r"] = { sobjectType: "Parent_Object__c", External_Id__c: "bar" }; Commented Nov 7 at 12:52
  • @DerekF when I did that, it returns a much larger for the Parent Lookup relationship with attributes and other object structure. When I re-create that in JavaScript and send it back to Apex, I get back the Unable to Identify / Read SObject Commented Nov 7 at 12:53

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.