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?
System.debug(JSON.serialize(record));Maybe you just need to make a new instance of the object and manually populate fields.attributesand other object structure. When I re-create that in JavaScript and send it back to Apex, I get back theUnable to Identify / Read SObject