function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matyas CsabaMatyas Csaba 

CREATE MULTIPLE RECORDS USING REST API USING EXTERNAL ID for LOOKUP-ed Object

Hello Reader

I  have a Custom object  (called API_Order__c) which has  lookup  field to Account
My plan is  to insert  MULTIPLE  records  in 1  REST API call and I am trying to build  the good JSON
I am not able to do this. I tried  the stanbdard REST  and I tried  to  use the COmposite/tree  REST

Seee below what I havew tried:

TEST 1

I want to  create  1 record  using REST command AND I want to reference the Account with External ID.
I use the below  REST
Method: POST
URL: /services/data/v39.0/sobjects/API_Order__c/

{
  "SourceFile__c" : "DE_1A_MSV3",
  "Order_Date__c" : "2017-07-07",
  "Account__r" : 
         {
         "External_ID_vod__c":"WDEF01663170"
         },
  "Account_Name__c" : "Adler-Apotheke",
  "NumberOfExpectedItemLines__c" : "2", 
  "External_Id__c" : "API_O_1003"
}

this is WORKING

​TEST 2
BUT  if I want to insert  MULTIPLE  records  I do not  know  what is  the right syntax


[
{
  "SourceFile__c" : "Value 1",
  "Order_Date__c" : "2017-07-07",
  "Account__r" : 
         {
         "External_ID_vod__c":"XXXX"
         },
  "External_Id__c" : "API_O_1003"
},
{
  "SourceFile__c" : "Value 2",
  "Order_Date__c" : "2017-07-08",
  "Account__r" : 
         {
         "External_ID_vod__c":"YYYY"
         },
  "External_Id__c" : "API_O_1003"
}
]

I god  JSON parsng ERROR

TEST 3

I tried to use  the COMPOSITE  REST  call  to insert Multiple records and if I am passing the Salesforce ID  of  the  Account lookup is OK 

POST
/services/data/v39.0/composite/tree/API_Order__c

{
"records" :[
  {
  "attributes" : {"type" : "API_Order__c", "referenceId" : "ref1"},
  "SourceFile__c" : "DE_1A_MSV3",
  "Order_Date__c" : "2017-07-07",
  "Account__c" : "0015E000004jH2I"
  }, 
  {
  "attributes" : {"type" : "API_Order__c", "referenceId" : "ref2"},
  "SourceFile__c" : "Val2",
  "Order_Date__c" : "2017-07-07",
  "Account__c" : "0015E000004jH2I"
   }]
}

TEST 4
BUT IF  I TRY  TO REFERENC ETHE ACCOUNT WITH EXTER NAL ID  I FAIL

POST
/services/data/v39.0/composite/tree/API_Order__c

{
"records" :[
  {
  "attributes" : {"type" : "API_Order__c", "referenceId" : "ref1"},
  "SourceFile__c" : "DE_1A_MSV3",
  "Order_Date__c" : "2017-07-07",
  "Account__r" : 
         {
         "External_ID_vod__c":"WDEF01663170"
         },
  "Account_Name__c" : "Adler-Apotheke",
  "NumberOfExpectedItemLines__c" : "2", 
  "External_Id__c" : "API_O_1001"
  },
  {
  "attributes" : {"type" : "API_Order__c", "referenceId" : "ref2"},
  "SourceFile__c" : "DE_1A_MSV3",
  "Order_Date__c" : "2017-07-07",
  "Account__r" : 
         {
         "External_ID_vod__c":"WDEF01663170"
         },
  "Account_Name__c" : "Adler-Apotheke",
  "NumberOfExpectedItemLines__c" : "2",
  "External_Id__c" : "API_O_1002"
   }]
}

I have the ERROR  message: Cannot reference a foreign key field Account__r.


Any help?  

Thanks in advance Csaba
@DEVS@DEVS
I'm facing the same issue with below erroe message. Raised a case with Salesforce, will update if I hear anything from them.
hasErrors: true
results
[Item 1]
referenceId: ref1
errors
[Item 1]
statusCode: INVALID_FIELD
message: Cannot reference a foreign key field Account.
fields
[Item 2]
statusCode: INVALID_FIELD
message: Cannot reference a foreign key field RecordType.
fields
David TLGDavid TLG

I have the same error...

{
    "records": [
        {
            "Shipping_Address__c": "XXXXXX",
            "Order_Details__r": {
                "records": [
                    {
                        "External_Id__c": 2238,
                        "Total_Price__c": 128.0489798717,
                        "SKU__c": "T4322102",
                        "Sales__c": 0,
                        "Order_Detail_Name__c": "blablabla ",
                        "Price__c": 128.0489798717,
                        "Product__c": null,
                        "attributes": {"referenceId": "ref2", "type": "Order_Detail__c"},
                        "Product_Price__c": 128.0489798717,
                        "Quantity__c": 1
                    },
                    {
                        "External_Id__c": 2239,
                        "Total_Price__c": 35.979531,
                        "SKU__c": "225478",
                        "Sales__c": 0,
                        "Order_Detail_Name__c": "CCCCCC",
                        "Price__c": 35.979531,
                        "Product__c": null,
                        "attributes": {
                            "referenceId": "ref3",
                            "type": "Order_Detail__c"
                        },
                        "Product_Price__c": 35.979531,
                        "Quantity__c": 1
                    }
                ]
            },
            "Order_Id__c": 225,
            "Shipping_Country__c": "ES",
            "External_Id__c": 1770,
            "Payment_Method__c": "Bank Transfer",
            "Comments__c": "",
            "Shipping_Postal_Code__c": "18000",
            "Language__c": "",
            "Status__c": 1,
            "Account__r": {"External_Id__c":"215"},
            "Shipping_City__c": "CCCC",
            "Order_Total__c": 203.314498154757,
            "attributes": {
                "referenceId": "ref1",
                "type": "Order__c"
            },
            "Document_Type__c": 1
        }
    ]
}

Response:
{
    "hasErrors": true,
    "results": [
        {
            "referenceId": "ref1",
            "errors": [
                {
                    "statusCode": "INVALID_FIELD",
                    "message": "Cannot reference a foreign key field Customer__r.",
                    "fields": []
                }
            ]
        }
    ]

It seems like is needed send a "reference" in "Lookup" field relationship?

Dana FurmanDana Furman

Hi All,

Did anyone have solution for this?

Here is mu code, and i'm getting the same error for each related object...

{
"records" :[{
"attributes" : {"type" : "Document__c", "referenceId" : "ref1"},
"External_ID__c" : "3",
"Account_Connected__r" : {"External_ID__c" : "222"},
"Connected_to_Opp__r" : {"External_ID__c" : "1123434523"},
"Contact__r" : {"External_ID__c" : "1"},
"Property_Connected__r": {"External_ID__c" : "1"},
"Case_Connected__r": {"External_ID__c" : "1"},
"Tenant_in_Property_Connected__r" : {"External_ID__c" : "1123123"},
"Document_Description__c" : "blabla",
},{
"attributes" : {"type" : "Document__c", "referenceId" : "ref2"},
"External_ID__c": "4",
"Account_Connected__r": {"External_ID__c" : "1"},
"Connected_to_Opp__r": {"External_ID__c" : "1123434523"},
"Contact__r" : {"External_ID__c" : "1"},
"Property_Connected__r": {"External_ID__c" : "1"},
"Case_Connected__r": {"External_ID__c" : "1"},
"Tenant_in_Property_Connected__r" : {"External_ID__c" : "1123123"},
"Document_Description__c" : "blabla",
}]
}

Thank you!
Dana

 

Anant Agarwal 15Anant Agarwal 15
Hi All,

I am facing the same issue, has anyone found the solution of this?
Adrian KasparAdrian Kaspar
It seems that when using "composite/tree" it just doesn't work to create a record with child records and link a parent record at the same time.
I solved the problem by dividing the call into multiple subrequests and using the composite resource (/vXX.X/composite). This makes it possible to create the record and link it to the parent in a first step, then get the ID and finally create the child records - all in one request.

You will find a detailed description and examples here:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_composite.htm
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_composite_record_manipulation.htm
ArunaAruna
Hello every one I a also running into same issue.
in our case we do not want to create the child record we need to reference the child record when we are creating parent record with external id.
{
"records" :[{
    "attributes" : {"type" : "testobj__c", "referenceId" : "ref1"},
      "testchild__r" :
      {  
         "testchildExternalfiled" : "Abc123"
      },
    "testparentfield1__c" : "test data1",
   "testparentfield12_c" : "456fghgfh",
   "testparentfield13__c" : 24,
    }]
}

I am also getting

"hasErrors": true,
"results": [{
"referenceId": "ref1",
"errors": [{
"statusCode": "INVALID_FIELD",
"message": "Cannot reference a foreign key field testchild__r.",
"fields": []
}]}]}
can any one please help this very heigh priority now. If possible please working JSON script.

Thank you.
 
AlvaroCostaAlvaroCosta

Hello, I had the same scenario using: composite/tree/sOjects

"statusCode": "INVALID_FIELD",
"message": "Cannot reference a foreign key field Object__r.",

I solved my problem using only /composite, but I truly want to understand the limitation of composite/tree/.
Here it is how I overcame:

POST: services/data/vXX.0/composite
{
    "allOrNone": true,
    "compositeRequest": [
        {
            "method": "POST",
            "url": "/services/data/vXX.0/sobjects/Object1",
            "referenceId": "ref1",
            "body": {
                "Object__r": {
                    "ExternalId__c": "001"
                },
                "Field__c": "ABC"
            }
        },
        {
            "method": "POST",
            "url": "/services/data/vXX.0/sobjects/Object1",
            "referenceId": "ref2",
            "body": {
                "Object__r": {
                    "ExternalId__c": "002"
                },
                "Field__c": "ABC"
            }
        }
    ]
}

 

Renan BotinhaoRenan Botinhao

Hello!

In 2023, I still facing the same issue.

Did anyone find a way to use /composite/tree/sOjects with External ID or /composite is the only way until now?

Thank you in advanced!

Sumit Mishra 47Sumit Mishra 47

Hi @renan, Using composite graph its possible.  Please find example below where in to create contact i am using soft reference of external id field 
ACCode__c of Account.

{{urDOMAIN}}/services/data/v{{version}}/composite/graph 

<urDOMAIN>, <BearerToken> --> replace with actuals

curl --location '<urDOMAIN>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BearerToken>' \
--data '{
    "graphs": [
        {
            "graphId": "1",
            "compositeRequest": [
                 
                {
                    "url": "/services/data/v50.0/sobjects/Contact/",
                    "body": {
                        "FirstName": "Test 1",
                        "LastName": "TE-01-Acc1-CON-1",
                        "Account": {
                            "ACCode__c": "01"
                        }

                    },
                    "method": "POST",
                    "referenceId": "reference_id_contact_1"
                }
            ]
        }
    ]
}'