1

I need to create multiple contacts in single REST call with account's external id using standard REST API

I'm able to create contact with account external id using below rest URL but it does not support multiple contacts (it's singular)

/services/data/v47.0/sobjects/Contact

Example JSON

{

"FirstName":"Michael",  
"LastName":"Jordan",  
"email" : "[email protected]",  
"Account": {  
     "External_Id__c":"x165"  
}  

}

When I try composite rest, I'm not able to create contacts with account external id

/services/data/v47.0/composite/tree/contact/

Is there any option to create multiple contacts with account's external id using standard REST API?

2
  • Have you send in a list of records?, Please send the request in List of JSON as document suggest, See the Document
    – Sarvesh
    Mar 24, 2020 at 8:47
  • @Sarvesh it's not working with account external id Mar 24, 2020 at 9:20

1 Answer 1

3

The composite tree resource always creates new records, which is why it gives you that behavior. Instead, you'd use a composite request. This example gives you the closest behavior you have available. You'd start by using a GET /services/data/v48.0/sobjects/Account/External_Id__c/x165, which returns the account, then use that reference in further updates.

Here's your example, put into a form you can use.

URL: POST /services/data/v47.0/composite/

Body:

{
    "allOrNone": true,
    "compositeRequest": 
    [
        {
            "method": "GET",
            "url": "/services/data/v47.0/sobjects/Account/External_Id__c/x165",
            "referenceId": "x165"
        },
        {
            "method": "POST",
            "url": "/services/data/v47.0/composite/sobjects",
            "referenceId": "contacts",
            "body": 
            {
                "allOrNone": true,
                "records": 
                [
                    {
                        "attributes": { "type": "Contact", "referenceId": "johnDoe" },
                        "FirstName": "John",
                        "LastName": "Doe",
                        "AccountId": "@{x165.Id}"
                    },
                    {
                        "attributes": { "type": "Contact", "referenceId": "janeDoe" },
                        "FirstName": "Jane",
                        "LastName": "Doe",
                        "AccountId": "@{x165.Id}"
                    }
                ]
            }
        }
    ]
}

First, we retrieve the account by Id, and assign it a "reference". We then use the reference via @{x165.id} in subsequent records. You can include up to 25 subrequests in a request, five of which can be queries. This would be the ideal way to load many records that all have the same related records.

There is, unfortunately, no way to directly reference the external ID as you would in SOAP (as far as I'm aware), so this is probably the best solution. This limitation exists because it is possible to create nested records, and there isn't a way to distinguish if you meant to reference an existing record, or create a new one.

4
  • I really appreciate your help, Let me try this method Mar 24, 2020 at 13:44
  • @ sfdcfox How many records we can process in a compositeRequest call? Mar 27, 2020 at 9:55
  • @Nishadkallingal You can have 25 subrequests (with a limit of 5 different queries), each /composite/sobjects can handle 200 records. This puts an upper limit of 5,000 records with no queries, less depending on how many queries you need.
    – sfdcfox
    Mar 27, 2020 at 10:05
  • @sfdcfox How to use Patch in above nodes. when using patch how can we use externalID to Patch? { "allOrNone": true, "compositeRequest": [ { "method": "PATCH", "url": "/services/data/v60.0/sobjects/Account/SourceSystemIdentifier/1111", "referenceId": "patientUpdate", "body": { "FirstName": "kate ", "LastName": "ladan" } }, { "method": "GET", "url": "/services/data/v60.0/sobjects/Account/SourceSystemIdentifier/1111", "referenceId": "getPatient" },{ "method": "GET", "url":"/services/data/v60.0/sobjects/ContactPointPhone", "referenceId": "getPhone" }, { "method": "PATCH", "url": "/services/data/v6 Mar 21 at 22:15

You must log in to answer this question.

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