When using REST API composite JSON, how to refer with external id
- an existing record
- to existing Master-Detail parent from a newly created child
We have the following objects:
Organization__c
:- Has
Organization__c
that is an External ID- ID comes from another system
- A bit confusing since the field name is the same
- (A bunch of other fields)
- Has
Foobar__c
:- Has
Organization__c
field as Master-Detail(Organization) - (A bunch of other fields)
- Has
We're trying to create Foobar objects from another system that knows only external id for Organization.
We're trying to POST to services/data/v42.0/composite/tree/Foobar__c
with "Organization__r": {"Organization__c": "123456"}
for organization reference but get the following error: {"hasErrors":true,"results":[{"referenceId":"ref-1","errors":[{"statusCode":"INVALID_FIELD","message":"Cannot reference a foreign key field Organization__r.","fields":[]}]}]}
I tried also with "Organization__c": {"Organization__c": "123456"}
but got: [{"message":"Cannot deserialize instance of reference from START_OBJECT value { or request may be missing a required field at [line:1, column:398]","errorCode":"JSON_PARSER_ERROR"}]
My hypothesis here is that by using Organization__c
I'm trying to create a new Organization record and that fails because there are fields missing.
UPDATE/ADDITION: Approach with Organization__r": {"Organization__c": "123456"}
seems to work both
- When inserting a single Foobar object with REST API (POST to
/services/data/v42.0/sobjects/Foobar__c
) - With Bulk API
- See e.g. https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/datafiles_xml_rel_fields.htm under "Relationship Fields for Custom Objects"