Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Composite Graph

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.

    -- {
    --   "graphs": [
    --     {
    --       "graphId": "graph1",
    --       "compositeRequest": [
    --         {
    --           "method": "POST",
    --           "url": "/services/data/v{{version}}/sobjects/Account/",
    --           "referenceId": "newAccount",
    --           "body": {
    --             "Name": "ACME Inc.",
    --             "Custom__c": "Hello world"
    --           }
    --         },
    --         {
    --           "method": "GET",
    --           "url": "/services/data/v{{version}}/sobjects/Account/@{newAccount.id}",
    --           "referenceId": "newAccountInfo"
    --         }
    --       ]
    --     }
    --   ]
    -- }

    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, 'graphs[0].graphId', 'graph1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[0].method', 'POST'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[0].url', '/services/data/v{{version}}/sobjects/Account/'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[0].referenceId', 'newAccount'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[0].body.Name', 'ACME Inc.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[0].body.Custom__c', 'Hello world'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[1].method', 'GET'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[1].url', '/services/data/v{{version}}/sobjects/Account/@{newAccount.id}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'graphs[0].compositeRequest[1].referenceId', 'newAccountInfo'

    -- 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/graph', '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)

    -- {
    --   "graphs": [
    --     {
    --       "graphId": "graph1",
    --       "graphResponse": {
    --         "compositeResponse": [
    --           {
    --             "body": {
    --               "id": "0014H00002x6HUhQAM",
    --               "success": true,
    --               "errors": [
    --               ]
    --             },
    --             "httpHeaders": {
    --               "Location": "/services/data/v50.0/sobjects/Account/0014H00002x6HUhQAM"
    --             },
    --             "httpStatusCode": 201,
    --             "referenceId": "newAccount"
    --           },
    --           {
    --             "body": {
    --               "id": "0034H00002JsInQQAV",
    --               "success": true,
    --               "errors": [
    --               ]
    --             },
    --             "httpHeaders": {
    --               "Location": "/services/data/v50.0/sobjects/Contact/0034H00002JsInQQAV"
    --             },
    --             "httpStatusCode": 201,
    --             "referenceId": "newContact"
    --           }
    --         ]
    --       },
    --       "isSuccessful": true
    --     }
    --   ]
    -- }

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

    DECLARE @graphId nvarchar(4000)

    DECLARE @isSuccessful int

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @Id nvarchar(4000)

    DECLARE @Success int

    DECLARE @Location nvarchar(4000)

    DECLARE @httpStatusCode int

    DECLARE @referenceId nvarchar(4000)

    DECLARE @k int

    DECLARE @count_k int

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'graphs'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @graphId OUT, 'graphs[i].graphId'
        EXEC sp_OAMethod @jResp, 'BoolOf', @isSuccessful OUT, 'graphs[i].isSuccessful'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'graphs[i].graphResponse.compositeResponse'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'graphs[i].graphResponse.compositeResponse[j].body.id'
            EXEC sp_OAMethod @jResp, 'BoolOf', @Success OUT, 'graphs[i].graphResponse.compositeResponse[j].body.success'
            EXEC sp_OAMethod @jResp, 'StringOf', @Location OUT, 'graphs[i].graphResponse.compositeResponse[j].httpHeaders.Location'
            EXEC sp_OAMethod @jResp, 'IntOf', @httpStatusCode OUT, 'graphs[i].graphResponse.compositeResponse[j].httpStatusCode'
            EXEC sp_OAMethod @jResp, 'StringOf', @referenceId OUT, 'graphs[i].graphResponse.compositeResponse[j].referenceId'
            SELECT @k = 0
            EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_k OUT, 'graphs[i].graphResponse.compositeResponse[j].body.errors'
            WHILE @k < @count_k
              BEGIN
                EXEC sp_OASetProperty @jResp, 'K', @k
                SELECT @k = @k + 1
              END
            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 '{
    "graphs": [
        {
            "graphId": "graph1",
            "compositeRequest": [
                {
                    "method": "POST",
                    "url": "/services/data/v{{version}}/sobjects/Account/",
                    "referenceId": "newAccount",
                    "body": {
                        "Name": "ACME Inc.",
                        "Custom__c": "Hello world"
                    }
                },
                {
                    "method": "GET",
                    "url": "/services/data/v{{version}}/sobjects/Account/@{newAccount.id}",
                    "referenceId": "newAccountInfo"
                }
            ]
        }
    ]
}'
https://domain.com/services/data/v{{version}}/composite/graph

Postman Collection Item JSON

{
  "name": "Composite Graph",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"graphs\": [\n        {\n            \"graphId\": \"graph1\",\n            \"compositeRequest\": [\n                {\n                    \"method\": \"POST\",\n                    \"url\": \"/services/data/v{{version}}/sobjects/Account/\",\n                    \"referenceId\": \"newAccount\",\n                    \"body\": {\n                        \"Name\": \"ACME Inc.\",\n                        \"Custom__c\": \"Hello world\"\n                    }\n                },\n                {\n                    \"method\": \"GET\",\n                    \"url\": \"/services/data/v{{version}}/sobjects/Account/@{newAccount.id}\",\n                    \"referenceId\": \"newAccountInfo\"\n                }\n            ]\n        }\n    ]\n}"
    },
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/composite/graph",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "composite",
        "graph"
      ]
    },
    "description": "Composite graphs provide an enhanced way to perform composite requests, which execute a series of REST API requests in a single call.\nRegular composite requests allow you to execute a series of REST API requests in a single call. And you can use the output of one request as the input to a subsequent request.\n\nComposite graphs extend this by allowing you to assemble a more complicated and complete series of related objects and records.\n\nComposite graphs also enable you to ensure that the steps in a given set of operations are either all completed or all not completed. This avoids requiring you to check for a mix of successful and unsuccessful results.\n\nRegular composite requests have a limit of 25 subrequests. Composite graphs increase this limit to 500. This gives a single API call much greater power.\n\nhttps://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_graph_introduction.htm"
  },
  "response": [
    {
      "name": "Successful Composite Graph",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"graphs\": [\n        {\n            \"graphId\": \"graph1\",\n            \"compositeRequest\": [\n                {\n                    \"method\": \"POST\",\n                    \"url\": \"/services/data/v{{version}}/sobjects/Account/\",\n                    \"referenceId\": \"newAccount\",\n                    \"body\": {\n                        \"Name\": \"ACME Inc.\"\n                    }\n                },\n                {\n                    \"method\": \"POST\",\n                    \"url\": \"/services/data/v{{version}}/sobjects/Contact/\",\n                    \"referenceId\": \"newContact\",\n                    \"body\": {\n                        \"FirstName\": \"John\",\n                        \"LastName\": \"Doe\",\n                        \"AccountId\": \"@{newAccount.id}\"\n                    }\n                }\n            ]\n        }\n    ]\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/composite/graph",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "composite",
            "graph"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "cache-control",
          "value": "no-cache,must-revalidate,max-age=0,no-store,private"
        },
        {
          "key": "content-type",
          "value": "application/json;charset=UTF-8"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"graphs\": [\n        {\n            \"graphId\": \"graph1\",\n            \"graphResponse\": {\n                \"compositeResponse\": [\n                    {\n                        \"body\": {\n                            \"id\": \"0014H00002x6HUhQAM\",\n                            \"success\": true,\n                            \"errors\": []\n                        },\n                        \"httpHeaders\": {\n                            \"Location\": \"/services/data/v50.0/sobjects/Account/0014H00002x6HUhQAM\"\n                        },\n                        \"httpStatusCode\": 201,\n                        \"referenceId\": \"newAccount\"\n                    },\n                    {\n                        \"body\": {\n                            \"id\": \"0034H00002JsInQQAV\",\n                            \"success\": true,\n                            \"errors\": []\n                        },\n                        \"httpHeaders\": {\n                            \"Location\": \"/services/data/v50.0/sobjects/Contact/0034H00002JsInQQAV\"\n                        },\n                        \"httpStatusCode\": 201,\n                        \"referenceId\": \"newContact\"\n                    }\n                ]\n            },\n            \"isSuccessful\": true\n        }\n    ]\n}"
    }
  ]
}