Showing posts with label rest api. Show all posts
Showing posts with label rest api. Show all posts

Wednesday, April 25, 2018

Creating Multiple Record in single rest request -Composite API

Hi

we have seen how can we create a record using  single rest request using OOB rest feature as below
/services/data/v40.0/sbjects/account

Method - POST
Body  - JSON
{
  "Name" :"testaccount"
}

Response will return with Id-
{
"Id":"001xxxxxxxxxxx"
}


Now if there is a need to insert data in tree structure in single request then which OOB Rest API need to follow, For this purpose we have Composite Tree API
Account -->contact (need to inset account and Contact in single oob rest req)
Here you go for tree API -
Url- /services/data/v41.0/composite/tree/account(Considering account to be parent node)
Method -Post
Body-
{
"records" :[{
    "attributes" : {"type" : "Account", "referenceId" : "ref1"},
    "name" : "ACC1",
    "phone" : "1234567890",
    "website" : "www.testtst.com",
    "industry" : "Banking",
    "Contacts" : {
      "records" : [
        {
         "attributes" : {"type" : "Contact", "referenceId" : "ref2"},
         "lastname" : "TestFname",
         "Title" : "TestLName",
         "email" : "sample@test.com"
         },
         {
         "attributes" : {"type" : "Contact", "referenceId" : "ref3"},
         "lastname" : "TestDLname",
         "title" : "Dev",
         "email" : "sample@testdev.com",
         "Camping_Item__r" :
             {
                "records" : [{
                "attributes" : {"type" : "Camping_Item__c", "referenceId" : "refCamping"},
                "name" : "mohicamplist"
                }]
             }
         }]
      }
    }]
}

Now the above example shows how can we create a tree structure(account-->contact-->campinglist) in single rest call  which will take automatic references from parent to child record.


Now what if we need to create record in tree structure with population of references from parent to child dynamically- Then Composite request is solution .

-In this approach a series of rest request executes in a single call

Url-
/services/data/v41.0/composite/

{
"compositeRequest" : [
  {
  "method" : "POST",
  "url" : "/services/data/v38.0/sobjects/Account",
  "referenceId" : "refAccount",
  "body" : { "Name" : "mohit composit1 Acc" }
  },
  {
  "method" : "POST",
  "url" : "/services/data/v38.0/sobjects/Contact",
  "referenceId" : "refContact",
  "body" : {
    "LastName" : "Sample composit Contact1",
    "AccountId" : "@{refAccount.id}"
    }
  },
  {
  "method" : "POST",
      "url" : "/services/data/v38.0/sobjects/Camping_Item__c",
       "referenceId" : "campingitem",
        "body" : {
        "name" : "ampingFnl",
        "Account__c" : "@{refAccount.id}",
         "Contact__c" : "@{refContact.id}",
         }
  } ]
}