[SalesForce] Salesforce REST API Composite tree fails – Cannot reference a foreign key field xxx__r

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

/services/data/v35.0/composite/tree/Case/myrelation__c/12345

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 !

Best Answer

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

EDIT:

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

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_sobject_tree.htm

Related Topic