5

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)
  • Foobar__c:
    • Has Organization__c field as Master-Detail(Organization)
    • (A bunch of other fields)

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

1

1 Answer 1

1

I think you only have two options here. The first is a "chatty" solution where your first step is to query Organization__c for the record ID.

/services/data/v20.0/query/?q=SELECT+Id+from+Organization__c+where+Organization__c+=+'<yourExternalId>'

Given that record Id, you should be able to then POST your foobar__c records directly now that you have both the SF record Id and the External Id.

It's two API calls, but should work.

As an alternative, you can write a Custom Apex REST endpoint that effectively does the same lookup from Organization__c.Organization__c to SF record Id.

I don't think that there is any other way to get around looking up the SF Record Id of the Master.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .