Chilkat Online Tools

DataFlex / Salesforce Platform APIs / Composite Graph

Back to Collection Items

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoJResp
    Integer iRespStatusCode
    String sGraphId
    Boolean iIsSuccessful
    Integer j
    Integer iCount_j
    String sId
    Boolean iSuccess
    String sLocation
    Integer iHttpStatusCode
    String sReferenceId
    Integer k
    Integer iCount_k
    Integer i
    Integer iCount_i
    String sTemp1
    Boolean bTemp1

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // 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"
    //         }
    //       ]
    //     }
    //   ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "graphs[0].graphId" "graph1" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[0].method" "POST" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[0].url" "/services/data/v{{version}}/sobjects/Account/" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[0].referenceId" "newAccount" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[0].body.Name" "ACME Inc." To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[0].body.Custom__c" "Hello world" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[1].method" "GET" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[1].url" "/services/data/v{{version}}/sobjects/Account/@{newAccount.id}" To iSuccess
    Get ComUpdateString Of hoJson "graphs[0].compositeRequest[1].referenceId" "newAccountInfo" To iSuccess

    // Adds the "Authorization: Bearer <access_token>" header.
    Set ComAuthToken Of hoHttp To "<access_token>"
    Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"

    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp "https://domain.com/services/data/v{{version}}/composite/graph" "application/json" vJson To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
    Set ComEmitCompact Of hoJResp To False

    Showln "Response Body:"
    Get ComEmit Of hoJResp To sTemp1
    Showln sTemp1

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Send Destroy of hoResp

    // 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

    Move 0 To i
    Get ComSizeOfArray Of hoJResp "graphs" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJResp To i
        Get ComStringOf Of hoJResp "graphs[i].graphId" To sGraphId
        Get ComBoolOf Of hoJResp "graphs[i].isSuccessful" To iIsSuccessful
        Move 0 To j
        Get ComSizeOfArray Of hoJResp "graphs[i].graphResponse.compositeResponse" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJResp To j
            Get ComStringOf Of hoJResp "graphs[i].graphResponse.compositeResponse[j].body.id" To sId
            Get ComBoolOf Of hoJResp "graphs[i].graphResponse.compositeResponse[j].body.success" To iSuccess
            Get ComStringOf Of hoJResp "graphs[i].graphResponse.compositeResponse[j].httpHeaders.Location" To sLocation
            Get ComIntOf Of hoJResp "graphs[i].graphResponse.compositeResponse[j].httpStatusCode" To iHttpStatusCode
            Get ComStringOf Of hoJResp "graphs[i].graphResponse.compositeResponse[j].referenceId" To sReferenceId
            Move 0 To k
            Get ComSizeOfArray Of hoJResp "graphs[i].graphResponse.compositeResponse[j].body.errors" To iCount_k
            While (k < iCount_k)
                Set ComK Of hoJResp To k
                Move (k + 1) To k
            Loop

            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop



End_Procedure

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}"
    }
  ]
}