Chilkat Online Tools

SQL Server / EPC 2.0 - Partner Sandbox - 19.4 November [External] / [Partner] Create a Transaction Event

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

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

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

    -- {
    --   "text": "EVENT_DESCRIPTION",
    --   "type": "EVENT_TYPE",
    --   "comments": "EVENT_COMMENTS",
    --   "resources": [
    --     {
    --       "id": "{{partner_resource_id}}",
    --       "name": "{{partner_resource_name}}",
    --       "mimeType": "{{partner_resource_mimeType}}"
    --     }
    --   ]
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'EVENT_DESCRIPTION'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'EVENT_TYPE'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'comments', 'EVENT_COMMENTS'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'resources[0].id', '{{partner_resource_id}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'resources[0].name', '{{partner_resource_name}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'resources[0].mimeType', '{{partner_resource_mimeType}}'

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

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/', '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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer {{partner_token}}"
	-H "Content-Type: application/json"
	-d '{
    "text": "EVENT_DESCRIPTION",
    "type": "EVENT_TYPE",
    "comments": "EVENT_COMMENTS",
    "resources": [
        {
            "id": "{{partner_resource_id}}",
            "name": "{{partner_resource_name}}",
            "mimeType": "{{partner_resource_mimeType}}"
        }
    ]
}'
https://domain.com/

Postman Collection Item JSON

{
  "name": "[Partner] Create a Transaction Event",
  "event": [
    {
      "listen": "test",
      "script": {
        "type": "text/javascript",
        "exec": [
          "// Extract Event identifier from response body",
          "var jsonData = JSON.parse(responseBody);",
          "var eventId = jsonData.id;",
          "",
          "// Set to an environment variable",
          "postman.setEnvironmentVariable(\"event_id\", eventId);"
        ]
      }
    }
  ],
  "_postman_id": "13de08d0-ce97-4bfa-9b95-518e06b6becc",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Authorization",
        "type": "text",
        "value": "Bearer {{partner_token}}"
      },
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"text\": \"EVENT_DESCRIPTION\",\n    \"type\": \"EVENT_TYPE\",\n    \"comments\": \"EVENT_COMMENTS\",\n    \"resources\": [\n        {\n            \"id\": \"{{partner_resource_id}}\",\n            \"name\": \"{{partner_resource_name}}\",\n            \"mimeType\": \"{{partner_resource_mimeType}}\"\n        }\n    ]\n}"
    },
    "url": "{{api_host}}/partner/v2/transactions/{{transaction_id}}/events"
  },
  "response": [
  ]
}