Chilkat Online Tools

SQL Server / Salesforce Platform APIs / SObject Tree

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "records": [
    --     {
    --       "attributes": {
    --         "type": "Account",
    --         "referenceId": "ref1"
    --       },
    --       "name": "SampleAccount1",
    --       "phone": "1234567890",
    --       "website": "www.salesforce.com",
    --       "numberOfEmployees": "100",
    --       "industry": "Banking",
    --       "Contacts": {
    --         "records": [
    --           {
    --             "attributes": {
    --               "type": "Contact",
    --               "referenceId": "ref2"
    --             },
    --             "lastname": "Smith",
    --             "Title": "President",
    --             "email": "sample@salesforce.com"
    --           },
    --           {
    --             "attributes": {
    --               "type": "Contact",
    --               "referenceId": "ref3"
    --             },
    --             "lastname": "Evans",
    --             "title": "Vice President",
    --             "email": "sample@salesforce.com"
    --           }
    --         ]
    --       }
    --     },
    --     {
    --       "attributes": {
    --         "type": "Account",
    --         "referenceId": "ref4"
    --       },
    --       "name": "SampleAccount2",
    --       "phone": "1234567890",
    --       "website": "www.salesforce.com",
    --       "numberOfEmployees": "52000",
    --       "industry": "Banking",
    --       "childAccounts": {
    --         "records": [
    --           {
    --             "attributes": {
    --               "type": "Account",
    --               "referenceId": "ref5"
    --             },
    --             "name": "SampleChildAccount1",
    --             "phone": "1234567890",
    --             "website": "www.salesforce.com",
    --             "numberOfEmployees": "100",
    --             "industry": "Banking"
    --           }
    --         ]
    --       },
    --       "Contacts": {
    --         "records": [
    --           {
    --             "attributes": {
    --               "type": "Contact",
    --               "referenceId": "ref6"
    --             },
    --             "lastname": "Jones",
    --             "title": "President",
    --             "email": "sample@salesforce.com"
    --           }
    --         ]
    --       }
    --     }
    --   ]
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].attributes.type', 'Account'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].attributes.referenceId', 'ref1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].name', 'SampleAccount1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].phone', '1234567890'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].website', 'www.salesforce.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].numberOfEmployees', '100'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].industry', 'Banking'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[0].attributes.type', 'Contact'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[0].attributes.referenceId', 'ref2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[0].lastname', 'Smith'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[0].Title', 'President'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[0].email', 'sample@salesforce.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[1].attributes.type', 'Contact'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[1].attributes.referenceId', 'ref3'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[1].lastname', 'Evans'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[1].title', 'Vice President'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[0].Contacts.records[1].email', 'sample@salesforce.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].attributes.type', 'Account'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].attributes.referenceId', 'ref4'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].name', 'SampleAccount2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].phone', '1234567890'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].website', 'www.salesforce.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].numberOfEmployees', '52000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].industry', 'Banking'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].attributes.type', 'Account'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].attributes.referenceId', 'ref5'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].name', 'SampleChildAccount1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].phone', '1234567890'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].website', 'www.salesforce.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].numberOfEmployees', '100'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].childAccounts.records[0].industry', 'Banking'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].Contacts.records[0].attributes.type', 'Contact'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].Contacts.records[0].attributes.referenceId', 'ref6'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].Contacts.records[0].lastname', 'Jones'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].Contacts.records[0].title', 'President'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'records[1].Contacts.records[0].email', 'sample@salesforce.com'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/services/data/v{{version}}/composite/tree/:SOBJECT_API_NAME', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "hasErrors": false,
    --   "results": [
    --     {
    --       "referenceId": "ref1",
    --       "id": "001..."
    --     },
    --     {
    --       "referenceId": "ref4",
    --       "id": "001..."
    --     },
    --     {
    --       "referenceId": "ref2",
    --       "id": "003.."
    --     },
    --     {
    --       "referenceId": "ref3",
    --       "id": "003..."
    --     },
    --     {
    --       "referenceId": "ref5",
    --       "id": "001..."
    --     },
    --     {
    --       "referenceId": "ref6",
    --       "id": "003..."
    --     }
    --   ]
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @referenceId nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @hasErrors int
    EXEC sp_OAMethod @jResp, 'BoolOf', @hasErrors OUT, 'hasErrors'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'results'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @referenceId OUT, 'results[i].referenceId'
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'results[i].id'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
"records" :[{
    "attributes" : {"type" : "Account", "referenceId" : "ref1"},
    "name" : "SampleAccount1",
    "phone" : "1234567890",
    "website" : "www.salesforce.com",
    "numberOfEmployees" : "100",
    "industry" : "Banking",
    "Contacts" : {
      "records" : [{
         "attributes" : {"type" : "Contact", "referenceId" : "ref2"},
         "lastname" : "Smith",
         "Title" : "President",
         "email" : "sample@salesforce.com"
         },{
         "attributes" : {"type" : "Contact", "referenceId" : "ref3"},
         "lastname" : "Evans",
         "title" : "Vice President",
         "email" : "sample@salesforce.com"
         }]
      }
    },{
    "attributes" : {"type" : "Account", "referenceId" : "ref4"},
    "name" : "SampleAccount2",
    "phone" : "1234567890",
    "website" : "www.salesforce.com",
    "numberOfEmployees" : "52000",
    "industry" : "Banking",
    "childAccounts" : {
      "records" : [{
        "attributes" : {"type" : "Account", "referenceId" : "ref5"},
        "name" : "SampleChildAccount1",
        "phone" : "1234567890",
        "website" : "www.salesforce.com",
        "numberOfEmployees" : "100",
        "industry" : "Banking"
        }]
      },
    "Contacts" : {
      "records" : [{
        "attributes" : {"type" : "Contact", "referenceId" : "ref6"},
        "lastname" : "Jones",
        "title" : "President",
        "email" : "sample@salesforce.com"
        }]
      }
  }]
}'
https://domain.com/services/data/v{{version}}/composite/tree/:SOBJECT_API_NAME

Postman Collection Item JSON

{
  "name": "SObject Tree",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\"records\" :[{\n    \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref1\"},\n    \"name\" : \"SampleAccount1\",\n    \"phone\" : \"1234567890\",\n    \"website\" : \"www.salesforce.com\",\n    \"numberOfEmployees\" : \"100\",\n    \"industry\" : \"Banking\",\n    \"Contacts\" : {\n      \"records\" : [{\n         \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref2\"},\n         \"lastname\" : \"Smith\",\n         \"Title\" : \"President\",\n         \"email\" : \"sample@salesforce.com\"\n         },{\n         \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref3\"},\n         \"lastname\" : \"Evans\",\n         \"title\" : \"Vice President\",\n         \"email\" : \"sample@salesforce.com\"\n         }]\n      }\n    },{\n    \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref4\"},\n    \"name\" : \"SampleAccount2\",\n    \"phone\" : \"1234567890\",\n    \"website\" : \"www.salesforce.com\",\n    \"numberOfEmployees\" : \"52000\",\n    \"industry\" : \"Banking\",\n    \"childAccounts\" : {\n      \"records\" : [{\n        \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref5\"},\n        \"name\" : \"SampleChildAccount1\",\n        \"phone\" : \"1234567890\",\n        \"website\" : \"www.salesforce.com\",\n        \"numberOfEmployees\" : \"100\",\n        \"industry\" : \"Banking\"\n        }]\n      },\n    \"Contacts\" : {\n      \"records\" : [{\n        \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref6\"},\n        \"lastname\" : \"Jones\",\n        \"title\" : \"President\",\n        \"email\" : \"sample@salesforce.com\"\n        }]\n      }\n  }]\n}"
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/composite/tree/:SOBJECT_API_NAME",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "composite",
        "tree",
        ":SOBJECT_API_NAME"
      ],
      "variable": [
        {
          "key": "SOBJECT_API_NAME",
          "value": "Account"
        }
      ]
    },
    "description": "Creates one or more sObject trees with root records of the specified type. An sObject tree is a collection of nested, parent-child records with a single root record.\nIn the request data, you supply the record hierarchies, required and optional field values, each record’s type, and a reference ID for each record. Upon success, the response contains the IDs of the created records. If an error occurs while creating a record, the entire request fails. In this case, the response contains only the reference ID of the record that caused the error and the error information.\n\nThe request can contain the following:\nUp to a total of 200 records across all trees\nUp to five records of different types\nSObject trees up to five levels deep\nBecause an sObject tree can contain a single record, you can use this resource to create up to 200 unrelated records of the same type.\n\nWhen the request is processed and records are created, triggers, processes, and workflow rules fire separately for each of the following groups of records.\nRoot records across all sObject trees in the request\nAll second-level records of the same type—for example, second-level Contacts across all sObject trees in the request\nAll third-level records of the same type\nAll fourth-level records of the same type\nAll fifth-level records of the same type\n\nhttps://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_sobject_tree.htm"
  },
  "response": [
    {
      "name": "Successful SObject Tree",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\"records\" :[{\n    \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref1\"},\n    \"name\" : \"SampleAccount1\",\n    \"phone\" : \"1234567890\",\n    \"website\" : \"www.salesforce.com\",\n    \"numberOfEmployees\" : \"100\",\n    \"industry\" : \"Banking\",\n    \"Contacts\" : {\n      \"records\" : [{\n         \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref2\"},\n         \"lastname\" : \"Smith\",\n         \"Title\" : \"President\",\n         \"email\" : \"sample@salesforce.com\"\n         },{\n         \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref3\"},\n         \"lastname\" : \"Evans\",\n         \"title\" : \"Vice President\",\n         \"email\" : \"sample@salesforce.com\"\n         }]\n      }\n    },{\n    \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref4\"},\n    \"name\" : \"SampleAccount2\",\n    \"phone\" : \"1234567890\",\n    \"website\" : \"www.salesforce.com\",\n    \"numberOfEmployees\" : \"52000\",\n    \"industry\" : \"Banking\",\n    \"childAccounts\" : {\n      \"records\" : [{\n        \"attributes\" : {\"type\" : \"Account\", \"referenceId\" : \"ref5\"},\n        \"name\" : \"SampleChildAccount1\",\n        \"phone\" : \"1234567890\",\n        \"website\" : \"www.salesforce.com\",\n        \"numberOfEmployees\" : \"100\",\n        \"industry\" : \"Banking\"\n        }]\n      },\n    \"Contacts\" : {\n      \"records\" : [{\n        \"attributes\" : {\"type\" : \"Contact\", \"referenceId\" : \"ref6\"},\n        \"lastname\" : \"Jones\",\n        \"title\" : \"President\",\n        \"email\" : \"sample@salesforce.com\"\n        }]\n      }\n  }]\n}"
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/composite/tree/:SOBJECT_API_NAME",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "composite",
            "tree",
            ":SOBJECT_API_NAME"
          ],
          "variable": [
            {
              "key": "SOBJECT_API_NAME",
              "value": "Account"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 20 Nov 2023 15:23:41 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Robots-Tag",
          "value": "none"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "Sforce-Limit-Info",
          "value": "api-usage=41/15000"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"hasErrors\": false,\n    \"results\": [\n        {\n            \"referenceId\": \"ref1\",\n            \"id\": \"001...\"\n        },\n        {\n            \"referenceId\": \"ref4\",\n            \"id\": \"001...\"\n        },\n        {\n            \"referenceId\": \"ref2\",\n            \"id\": \"003..\"\n        },\n        {\n            \"referenceId\": \"ref3\",\n            \"id\": \"003...\"\n        },\n        {\n            \"referenceId\": \"ref5\",\n            \"id\": \"001...\"\n        },\n        {\n            \"referenceId\": \"ref6\",\n            \"id\": \"003...\"\n        }\n    ]\n}"
    }
  ]
}