0

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!

3 Answers 3

6

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)
4

In the REST API, you can use the sObject Tree resource. This allows you to create up to 200 related records by sending a simple JSON to the Salesforce API. You can also use the Salesforce Composite REST API to bundle up to 25 API calls, including up to 10 queries, into a single request. This API has the ability to generate references so you don't need to query the ID values back within the same request.

2

Yes, You can create multiple related records in a single insert with the use of ExternalId.

Refer Relating Records by Using an External ID

And refer my blog post Insert Multiple Parent and Child Records with External Id

Here is an example from documentation:

Opportunity newOpportunity = new Opportunity(
    Name='OpportunityWithAccountInsert',
    StageName='Prospecting',
    CloseDate=Date.today().addDays(7));

// Create the parent record reference.
// An account with external ID = 'SAP111111' already exists.
// This sObject is used only for foreign key reference
// and doesn't contain any other fields.
Account accountReference = new Account(
    MyExtID__c='SAP111111');                

// Add the account sObject to the opportunity.
newOpportunity.Account = accountReference;

// Create the opportunity.
Database.SaveResult results = Database.insert(newOpportunity);
1
  • 1
    This is technically correct, but OP is asking for this to be done through an API, not Apex.
    – Derek F
    Commented Jul 9, 2018 at 14:58

You must log in to answer this question.

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