I get an error stating "Cannot reference a foreign key field Parent_Case__r" whenever i am trying to use a relation field in the JSON request to REST API.

I have a lookup relation on one the child objects which i am trying to make use of in the following JSON request.

My Json structure is as follows :

"records" :[{
"attributes" : {"type" : "Case", "referenceId" : "ref1"},
"name" : "SampleCase",
"Case_Type__c" : "Email2Case",
"Child_Parent_Relationships__r" : {
"records" : [{
"attributes" : {"type" : "myrelation__c", "referenceId" : "ref2"},
"Parent_Case__r": {"Case_Number__c" : "12354747"},
"External_Id__c": "52363565325632",
"Sample__c": "Helloworld",
"Status__c": "Active"

I am firing this request to the Composite Resource End-point for instance


I have checked for some limitations such as the follows

  1. Up to five records of different types
  2. Up to a total of 200 records across all trees
  3. sObject trees upto 5 levels deep traversal

These criteria's doesn't seem to concern this scenario. Additionally i have checked other permissions related to the API and sObjects that could be causing this issue.

Kindly assist if i am missing something in this request. Or how do i use relationship in creating nested records during the REST call.

Any help will be appreciated. Thanks in advance !

2 Answers 2


I think you don't need to pass Parent_Case__r in request.

Salesforce will automatically relate the myrelation__c with case based on the hierarchy that you mentioned in JSON. In your case it will be automatically associated with case name "SampleCase"

if you are trying to relate myrelation__c to any other case record by specifying the Parent_Case__r then it not possible.

Take a look at: https://help.salesforce.com/apex/HTViewSolution?id=000214070&language=en_US


The limitations re documented as:

The request can contain the following:
Up to a total of 200 records across all trees
Up to five records of different types
SObject trees up to five levels deep


  • Thank you for the reply. kindly clarify the following -- the doc says you can go five levels deeper using this composite tree. By that statement i am traversing from Case to myrelation__c (level 1) and then i am using Parent_Case__r ( a lookup to Case itself - Level 2 ). And i am trying to update a value case_number__c. Any guess why it is not possible
    – Sumuga
    Nov 27, 2015 at 18:39
  • Please see my edits. 2 points to note 1) You can refer Ids but can not refer fields like case number. 2) Documentation does not talk about traversal Nov 30, 2015 at 6:33
  • you will have to handle this in your trigger of myrelation__c and can not be done with composite/tree API. Nov 30, 2015 at 6:35

TreeSave can only be used for insert, You can not update values with it. The relationships are also, only from Parent-to-child (5 levels deep). You can't go back to parent with TreeSave.

The other problem you are facing is with the syntax of your input.

"Child_Parent_Relationships__r" : { "records" : [{ ... }] }

has the correct format and you need to modify the format of

"Parent_Case__r": {"Case_Number__c" : "12354747"},

to make it insert a new parent_case__c if that is a child of myrelation__c.

You must log in to answer this question.

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