Chilkat Online Tools

SQL Server / Salesforce Platform APIs / Platform Event Schema by Event Name

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
    -- 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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- 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 @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://domain.com/services/data/v{{version}}/sobjects/:EVENT_NAME/eventSchema', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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 @http, 'LastStatus', @respStatusCode OUT

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

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

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

    -- {
    --   "name": "Sample__e",
    --   "namespace": "com.sforce.eventbus",
    --   "type": "expanded-record",
    --   "fields": [
    --     {
    --       "name": "data",
    --       "type": {
    --         "type": "record",
    --         "name": "Data",
    --         "namespace": "",
    --         "fields": [
    --           {
    --             "name": "schema",
    --             "type": "string"
    --           },
    --           {
    --             "name": "payload",
    --             "type": {
    --               "type": "record",
    --               "name": "Payload",
    --               "doc": "",
    --               "fields": [
    --                 {
    --                   "name": "CreatedDate",
    --                   "type": "string",
    --                   "doc": "CreatedDate:DateTime"
    --                 },
    --                 {
    --                   "name": "CreatedById",
    --                   "type": "string",
    --                   "doc": "CreatedBy:EntityId"
    --                 },
    --                 {
    --                   "name": "Message__c",
    --                   "type": [
    --                     "null",
    --                     "string"
    --                   ],
    --                   "doc": "Data:Text:00N4H00000Ecs0G",
    --                   "default": null
    --                 }
    --               ]
    --             }
    --           },
    --           {
    --             "name": "event",
    --             "type": {
    --               "type": "record",
    --               "name": "Event",
    --               "fields": [
    --                 {
    --                   "name": "replayId",
    --                   "type": "long"
    --                 }
    --               ]
    --             }
    --           }
    --         ]
    --       }
    --     },
    --     {
    --       "name": "channel",
    --       "type": "string"
    --     }
    --   ]
    -- }

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

    DECLARE @v_Type nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @Namespace nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @typeType nvarchar(4000)

    DECLARE @typeName nvarchar(4000)

    DECLARE @Doc nvarchar(4000)

    DECLARE @k int

    DECLARE @count_k int

    DECLARE @doc nvarchar(4000)

    DECLARE @default nvarchar(4000)

    DECLARE @json1 int

    DECLARE @i1 int

    DECLARE @count_i1 int

    DECLARE @strVal nvarchar(4000)

    DECLARE @name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'name'
    DECLARE @namespace nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @namespace OUT, 'namespace'
    DECLARE @v_type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'type'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'fields'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'fields[i].name'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'fields[i].type.type'
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'fields[i].type.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @Namespace OUT, 'fields[i].type.namespace'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'fields[i].type'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'fields[i].type.fields'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'fields[i].type.fields[j].name'
            EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'fields[i].type.fields[j].type'
            EXEC sp_OAMethod @jResp, 'StringOf', @typeType OUT, 'fields[i].type.fields[j].type.type'
            EXEC sp_OAMethod @jResp, 'StringOf', @typeName OUT, 'fields[i].type.fields[j].type.name'
            EXEC sp_OAMethod @jResp, 'StringOf', @Doc OUT, 'fields[i].type.fields[j].type.doc'
            SELECT @k = 0
            EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_k OUT, 'fields[i].type.fields[j].type.fields'
            WHILE @k < @count_k
              BEGIN
                EXEC sp_OASetProperty @jResp, 'K', @k
                EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'fields[i].type.fields[j].type.fields[k].name'
                EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'fields[i].type.fields[j].type.fields[k].type'
                EXEC sp_OAMethod @jResp, 'StringOf', @doc OUT, 'fields[i].type.fields[j].type.fields[k].doc'
                EXEC sp_OAMethod @jResp, 'StringOf', @default OUT, 'fields[i].type.fields[j].type.fields[k].default'

                EXEC sp_OAMethod @jResp, 'ObjectOf', @json1 OUT, 'fields[i].type.fields[j].type.fields[k]'
                SELECT @i1 = 0
                EXEC sp_OAMethod @json1, 'SizeOfArray', @count_i1 OUT, 'type'
                WHILE @i1 < @count_i1
                  BEGIN
                    EXEC sp_OASetProperty @json1, 'I', @i1
                    EXEC sp_OAMethod @json1, 'StringOf', @strVal OUT, 'type[i]'
                    SELECT @i1 = @i1 + 1
                  END
                EXEC @hr = sp_OADestroy @json1

                SELECT @k = @k + 1
              END
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

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


END
GO

Curl Command

curl -X GET
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
https://domain.com/services/data/v{{version}}/sobjects/:EVENT_NAME/eventSchema

Postman Collection Item JSON

{
  "name": "Platform Event Schema by Event Name",
  "request": {
    "method": "GET",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "url": {
      "raw": "{{_endpoint}}/services/data/v{{version}}/sobjects/:EVENT_NAME/eventSchema",
      "host": [
        "{{_endpoint}}"
      ],
      "path": [
        "services",
        "data",
        "v{{version}}",
        "sobjects",
        ":EVENT_NAME",
        "eventSchema"
      ],
      "query": [
        {
          "key": "payloadFormat",
          "value": "",
          "description": "(Optional query parameter. Available in API version 43.0 and later.) The format of the returned event schema. This parameter can take one of the following values.\nEXPANDED—The JSON representation of the event schema, which is the default format when payloadFormat is not specified in API version 43.0 and later.\nCOMPACT—A format that adheres to the open-source Apache Avro specification for the record complex type (see Apache Avro Format). Subscribers use the compact schema format to deserialize compact events received in binary form.",
          "disabled": true
        }
      ],
      "variable": [
        {
          "key": "EVENT_NAME",
          "value": "",
          "description": "API Name of the event"
        }
      ]
    },
    "description": "Set, reset, or get information about a user password. This resource is available in REST API version 24.0 and later."
  },
  "response": [
    {
      "name": "Platform Event Schema by Event Name",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "url": {
          "raw": "{{_endpoint}}/services/data/v{{version}}/sobjects/:EVENT_NAME/eventSchema",
          "host": [
            "{{_endpoint}}"
          ],
          "path": [
            "services",
            "data",
            "v{{version}}",
            "sobjects",
            ":EVENT_NAME",
            "eventSchema"
          ],
          "query": [
            {
              "key": "payloadFormat",
              "value": "",
              "description": "(Optional query parameter. Available in API version 43.0 and later.) The format of the returned event schema. This parameter can take one of the following values.\nEXPANDED—The JSON representation of the event schema, which is the default format when payloadFormat is not specified in API version 43.0 and later.\nCOMPACT—A format that adheres to the open-source Apache Avro specification for the record complex type (see Apache Avro Format). Subscribers use the compact schema format to deserialize compact events received in binary form.",
              "disabled": true
            }
          ],
          "variable": [
            {
              "key": "EVENT_NAME",
              "value": "Sample__e",
              "description": "API Name of the event"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 03 Jul 2023 13:06:08 GMT"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=63072000; includeSubDomains"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "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=2/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    \"name\": \"Sample__e\",\n    \"namespace\": \"com.sforce.eventbus\",\n    \"type\": \"expanded-record\",\n    \"fields\": [\n        {\n            \"name\": \"data\",\n            \"type\": {\n                \"type\": \"record\",\n                \"name\": \"Data\",\n                \"namespace\": \"\",\n                \"fields\": [\n                    {\n                        \"name\": \"schema\",\n                        \"type\": \"string\"\n                    },\n                    {\n                        \"name\": \"payload\",\n                        \"type\": {\n                            \"type\": \"record\",\n                            \"name\": \"Payload\",\n                            \"doc\": \"\",\n                            \"fields\": [\n                                {\n                                    \"name\": \"CreatedDate\",\n                                    \"type\": \"string\",\n                                    \"doc\": \"CreatedDate:DateTime\"\n                                },\n                                {\n                                    \"name\": \"CreatedById\",\n                                    \"type\": \"string\",\n                                    \"doc\": \"CreatedBy:EntityId\"\n                                },\n                                {\n                                    \"name\": \"Message__c\",\n                                    \"type\": [\n                                        \"null\",\n                                        \"string\"\n                                    ],\n                                    \"doc\": \"Data:Text:00N4H00000Ecs0G\",\n                                    \"default\": null\n                                }\n                            ]\n                        }\n                    },\n                    {\n                        \"name\": \"event\",\n                        \"type\": {\n                            \"type\": \"record\",\n                            \"name\": \"Event\",\n                            \"fields\": [\n                                {\n                                    \"name\": \"replayId\",\n                                    \"type\": \"long\"\n                                }\n                            ]\n                        }\n                    }\n                ]\n            }\n        },\n        {\n            \"name\": \"channel\",\n            \"type\": \"string\"\n        }\n    ]\n}"
    }
  ]
}