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.
-- {
-- "compositeRequest": [
-- {
-- "method": "POST",
-- "url": "/services/data/v{{version}}/sobjects/Account",
-- "referenceId": "refAccount",
-- "body": {
-- "Name": "Sample Account"
-- }
-- },
-- {
-- "method": "POST",
-- "url": "/services/data/v{{version}}/sobjects/Contact",
-- "referenceId": "refContact",
-- "body": {
-- "LastName": "Sample Contact",
-- "AccountId": "@{refAccount.id}"
-- }
-- }
-- ]
-- }
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, 'compositeRequest[0].method', 'POST'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[0].url', '/services/data/v{{version}}/sobjects/Account'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[0].referenceId', 'refAccount'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[0].body.Name', 'Sample Account'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[1].method', 'POST'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[1].url', '/services/data/v{{version}}/sobjects/Contact'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[1].referenceId', 'refContact'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[1].body.LastName', 'Sample Contact'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'compositeRequest[1].body.AccountId', '@{refAccount.id}'
-- 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/', '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)
-- {
-- "compositeResponse": [
-- {
-- "body": {
-- "id": "001...",
-- "success": true,
-- "errors": [
-- ]
-- },
-- "httpHeaders": {
-- "Location": "/services/data/v53.0/sobjects/Account/001..."
-- },
-- "httpStatusCode": 201,
-- "referenceId": "refAccount"
-- },
-- {
-- "body": {
-- "id": "0032o00003WauzJAAR",
-- "success": true,
-- "errors": [
-- ]
-- },
-- "httpHeaders": {
-- "Location": "/services/data/v53.0/sobjects/Contact/003..."
-- },
-- "httpStatusCode": 201,
-- "referenceId": "refContact"
-- }
-- ]
-- }
-- Sample code for parsing the JSON response...
-- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
DECLARE @Id nvarchar(4000)
DECLARE @Success int
DECLARE @Location nvarchar(4000)
DECLARE @httpStatusCode int
DECLARE @referenceId nvarchar(4000)
DECLARE @j int
DECLARE @count_j int
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'compositeResponse'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'compositeResponse[i].body.id'
EXEC sp_OAMethod @jResp, 'BoolOf', @Success OUT, 'compositeResponse[i].body.success'
EXEC sp_OAMethod @jResp, 'StringOf', @Location OUT, 'compositeResponse[i].httpHeaders.Location'
EXEC sp_OAMethod @jResp, 'IntOf', @httpStatusCode OUT, 'compositeResponse[i].httpStatusCode'
EXEC sp_OAMethod @jResp, 'StringOf', @referenceId OUT, 'compositeResponse[i].referenceId'
SELECT @j = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'compositeResponse[i].body.errors'
WHILE @j < @count_j
BEGIN
EXEC sp_OASetProperty @jResp, 'J', @j
SELECT @j = @j + 1
END
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 '{
"compositeRequest": [
{
"method": "POST",
"url": "/services/data/v{{version}}/sobjects/Account",
"referenceId": "refAccount",
"body": {
"Name": "Sample Account"
}
},
{
"method": "POST",
"url": "/services/data/v{{version}}/sobjects/Contact",
"referenceId": "refContact",
"body": {
"LastName": "Sample Contact",
"AccountId": "@{refAccount.id}"
}
}
]
}'
https://domain.com/services/data/v{{version}}/composite/
Postman Collection Item JSON
{
"name": "Composite",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"compositeRequest\": [\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v{{version}}/sobjects/Account\",\n \"referenceId\": \"refAccount\",\n \"body\": {\n \"Name\": \"Sample Account\"\n }\n },\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v{{version}}/sobjects/Contact\",\n \"referenceId\": \"refContact\",\n \"body\": {\n \"LastName\": \"Sample Contact\",\n \"AccountId\": \"@{refAccount.id}\"\n }\n }\n ]\n}"
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/composite/",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"composite",
""
]
},
"description": "Executes a series of REST API requests in a single call. You can use the output of one request as the input to a subsequent request. The response bodies and HTTP statuses of the requests are returned in a single response body. The entire request counts as a single call toward your API limits.\nThe requests in a composite call are called subrequests. All subrequests are executed in the context of the same user. In a subrequest’s body, you specify a reference ID that maps to the subrequest’s response. You can then refer to the ID in the url or body fields of later subrequests by using a JavaScript-like reference notation.\n\nhttps://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_composite.htm"
},
"response": [
{
"name": "Successful Composite",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"compositeRequest\": [\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v53.0/sobjects/Account\",\n \"referenceId\": \"refAccount\",\n \"body\": {\n \"Name\": \"Sample Account\"\n }\n },\n {\n \"method\": \"POST\",\n \"url\": \"/services/data/v53.0/sobjects/Contact\",\n \"referenceId\": \"refContact\",\n \"body\": {\n \"LastName\": \"Sample Contact\",\n \"AccountId\": \"@{refAccount.id}\"\n }\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{_endpoint}}/services/data/v{{version}}/composite/",
"host": [
"{{_endpoint}}"
],
"path": [
"services",
"data",
"v{{version}}",
"composite",
""
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Mon, 20 Nov 2023 15:18:26 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=33/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 \"compositeResponse\": [\n {\n \"body\": {\n \"id\": \"001...\",\n \"success\": true,\n \"errors\": []\n },\n \"httpHeaders\": {\n \"Location\": \"/services/data/v53.0/sobjects/Account/001...\"\n },\n \"httpStatusCode\": 201,\n \"referenceId\": \"refAccount\"\n },\n {\n \"body\": {\n \"id\": \"0032o00003WauzJAAR\",\n \"success\": true,\n \"errors\": []\n },\n \"httpHeaders\": {\n \"Location\": \"/services/data/v53.0/sobjects/Contact/003...\"\n },\n \"httpStatusCode\": 201,\n \"referenceId\": \"refContact\"\n }\n ]\n}"
}
]
}