[SalesForce] create records across multiple Objects related to each other using Salesforce’s API

I'll preface this by saying I'm not a developer but I'm working with our engineering team to help automate our process. Thier question to me is can we create multiple related records across multiple objects and have them related to each other using Salesforce's API.

Here's an Example:

  1. Create Case 1
  2. Create Contact 1
  3. Create Case Relationship1 (junction object that relates Contact1 to Case 1)
  4. Create Contact 2
  5. Create Case Relationship (relate Contact2 to Case 1)

Is this doable without returning IDs then creating records based on the ID or can we send all of this to Salesforce to create at the same time?

If so is there specific documentation on how to do this?

Thanks!

Best Answer

I believe the thing you are looking for is the composite resource available through Salesforce's REST API.

The "composite" resource allows you to both:

  • Execute multiple subrequests as part of a single top-level request
  • Reference data from previous requests in a subsequent subrequests

Two pages of documentation I'd point you to are:

The two links end up giving you very similar information. In the interest of ease of reference (since links have a habit of going dead over time), the important bits are as follows.

The url you'd use to make a composite request is https://<your instance here>.salesforce.com/services/data/<api version>/composite/

My sandbox, for example, is on the "cs52" instance, and the current version of Salesforce's APIs is "v43.0", so if I wanted to make a request to my sandbox, the url I'd use would be https://cs52.salesforce.com/services/data/v43.0/composite/

The example JSON body given in that second link is:

{
    "allOrNone" : true,
    "compositeRequest" : [{
        "method" : "PATCH",
        "url" : "/services/data/v38.0/sobjects/Account/001xx000003DIpcAAG",
        "referenceId" : "UpdatedAccount",
        "body" : {  
            "Name" : "Salesforce",
            "BillingStreet" : "Landmark @ 1 Market Street",
            "BillingCity" : "San Francisco",
            "BillingState" : "California",
            "Industry" : "Technology"
        }
    },{
        "method" : "POST",
        "referenceId" : "NewContact",
        "url" : "/services/data/v38.0/sobjects/Contact/",
        "body" : {  
            "lastname" : "John Doe",
            "Phone" : "1234567890"
        }
    },{
        "method" : "POST",
        "referenceId" : "JunctionRecord",
        "url" : "/services/data/v38.0/sobjects/AccountContactJunction__c",
        "body" : {  
            "accountId__c" : "001xx000003DIpcAAG",
            "contactId__c" : "@{NewContact.id}"
        }
    }]
}

For each subrequest, there's also documentation that covers what you can specify. This page also tells us the following...

You can have up to 25 subrequests in a single call. Up to 5 of these subrequests can be query operations, including Query, QueryAll, and “Query More” requests to obtain the next batch of query results.

An inline recap of the other information on that page:

  • method: GET for reading a record, POST for creating a record, PATCH for updating, DELETE for deleting
  • referenceId: an arbitrary name you give to each sub-request that allows you to reference them in subsequent sub-requests. You use this with the @{} syntax and dot-notation to access a specific field
  • url: a relative path, and will probably be of the form /services/data/<api version>/sobjects/<SObject name>/
  • body: a list of field names of your target object, and the values you want to set those fields to (you don't need to specify every field on your SObject, just the ones you want to set)
Related Topic