Chilkat Online Tools

SQL Server / Squadcast API V3 / Create On-call 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
    -- 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.

    -- {
    --   "name": "SRE shift 1",
    --   "start_time": "2020-01-11T18:30:00.000Z",
    --   "end_time": "2020-01-11T18:30:00.000Z",
    --   "rotation_sets": [
    --     {
    --       "user_ids": [
    --         "5d81d9187000fb6b9def7e31"
    --       ],
    --       "squad_ids": [
    --       ]
    --     },
    --     {
    --       "user_ids": [
    --         "5d90cc3a4d031497494f07e6"
    --       ],
    --       "squad_ids": [
    --       ]
    --     }
    --   ],
    --   "config": {
    --     "timezone": {
    --       "name": "IST",
    --       "offset": -330
    --     },
    --     "is_forever": true,
    --     "until": "2020-01-09T14:38:06.658Z",
    --     "rotate": true,
    --     "repeat": true,
    --     "rotation_frequency": 1,
    --     "repetition": {
    --       "frequency": 1,
    --       "repeats_on": [
    --         0,
    --         6
    --       ],
    --       "type": "week"
    --     }
    --   }
    -- }

    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, 'name', 'SRE shift 1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'start_time', '2020-01-11T18:30:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'end_time', '2020-01-11T18:30:00.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rotation_sets[0].user_ids[0]', '5d81d9187000fb6b9def7e31'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'rotation_sets[0].squad_ids'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rotation_sets[1].user_ids[0]', '5d90cc3a4d031497494f07e6'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'rotation_sets[1].squad_ids'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'config.timezone.name', 'IST'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'config.timezone.offset', -330
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'config.is_forever', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'config.until', '2020-01-09T14:38:06.658Z'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'config.rotate', 1
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'config.repeat', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'config.rotation_frequency', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'config.repetition.frequency', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'config.repetition.repeats_on[0]', 0
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'config.repetition.repeats_on[1]', 6
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'config.repetition.type', 'week'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.squadcast.com/v3/schedules/:schedulesID/events', '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)

    -- {
    --   "data": [
    --     {
    --       "id": "5ecf89b4ebddeae2907cd424",
    --       "start_time": "2020-01-12T00:00:00+05:30",
    --       "end_time": "2020-01-12T00:00:00+05:30",
    --       "name": "SRE shift 1",
    --       "user_ids": [
    --         "5d81d9187000fb6b9def7e31"
    --       ],
    --       "series_id": "5ecf89b4ebddeae2907cd423",
    --       "squad_ids": [
    --       ],
    --       "is_override": false,
    --       "schedule_id": "5ecf7176f92f2e731262b852",
    --       "calendar": {
    --         "id": "5ecf7176f92f2e731262b852",
    --         "name": "Weekly schedule",
    --         "slug": "weekly-schedule"
    --       }
    --     },
    --     {
    --       "id": "5ecf89b4ebddeae2907cd425",
    --       "start_time": "2020-01-18T00:00:00+05:30",
    --       "end_time": "2020-01-18T00:00:00+05:30",
    --       "name": "SRE shift 1",
    --       "user_ids": [
    --         "5d90cc3a4d031497494f07e6"
    --       ],
    --       "series_id": "5ecf89b4ebddeae2907cd423",
    --       "squad_ids": [
    --       ],
    --       "is_override": false,
    --       "schedule_id": "5ecf7176f92f2e731262b852",
    --       "calendar": {
    --         "id": "5ecf7176f92f2e731262b852",
    --         "name": "Weekly schedule",
    --         "slug": "weekly-schedule"
    --       }
    --     },
    --     {
    --       "id": "5ecf89b4ebddeae2907cd426",
    --       "start_time": "2020-01-19T00:00:00+05:30",
    --       "end_time": "2020-01-19T00:00:00+05:30",
    --       "name": "SRE shift 1",
    --       "user_ids": [
    --         "5d81d9187000fb6b9def7e31"
    --       ],
    --       "series_id": "5ecf89b4ebddeae2907cd423",
    --       "squad_ids": [
    --       ],
    --       "is_override": false,
    --       "schedule_id": "5ecf7176f92f2e731262b852",
    --       "calendar": {
    --         "id": "5ecf7176f92f2e731262b852",
    --         "name": "Weekly schedule",
    --         "slug": "weekly-schedule"
    --       }
    --     }
    --   ]
    -- }

    -- 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 @start_time nvarchar(4000)

    DECLARE @end_time nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @series_id nvarchar(4000)

    DECLARE @is_override int

    DECLARE @schedule_id nvarchar(4000)

    DECLARE @Id nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @Slug nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @strVal nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'data'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'data[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @start_time OUT, 'data[i].start_time'
        EXEC sp_OAMethod @jResp, 'StringOf', @end_time OUT, 'data[i].end_time'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'data[i].name'
        EXEC sp_OAMethod @jResp, 'StringOf', @series_id OUT, 'data[i].series_id'
        EXEC sp_OAMethod @jResp, 'BoolOf', @is_override OUT, 'data[i].is_override'
        EXEC sp_OAMethod @jResp, 'StringOf', @schedule_id OUT, 'data[i].schedule_id'
        EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'data[i].calendar.id'
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'data[i].calendar.name'
        EXEC sp_OAMethod @jResp, 'StringOf', @Slug OUT, 'data[i].calendar.slug'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'data[i].user_ids'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @strVal OUT, 'data[i].user_ids[j]'
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'data[i].squad_ids'
        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>"
	-d '{
    "name": "SRE shift 1",
    "start_time": "2020-01-11T18:30:00.000Z",
    "end_time": "2020-01-11T18:30:00.000Z",
    "rotation_sets": [
        {
            "user_ids": ["5d81d9187000fb6b9def7e31"],
            "squad_ids": []
        },
        {
            "user_ids": ["5d90cc3a4d031497494f07e6"],
            "squad_ids": []
        }
    ],
    "config": {
        "timezone": {
        	"name": "IST", 
        	"offset": -330
        },
        "is_forever": true,
        "until": "2020-01-09T14:38:06.658Z",
        "rotate": true,
        "repeat": true,
        "rotation_frequency": 1,
        "repetition": {
            "frequency": 1,
            "repeats_on": [
                0,
                6
            ],
            "type": "week"
        }
    }
}'
https://api.squadcast.com/v3/schedules/:schedulesID/events

Postman Collection Item JSON

{
  "name": "Create On-call Event",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"SRE shift 1\",\n    \"start_time\": \"2020-01-11T18:30:00.000Z\",\n    \"end_time\": \"2020-01-11T18:30:00.000Z\",\n    \"rotation_sets\": [\n        {\n            \"user_ids\": [\"5d81d9187000fb6b9def7e31\"],\n            \"squad_ids\": []\n        },\n        {\n            \"user_ids\": [\"5d90cc3a4d031497494f07e6\"],\n            \"squad_ids\": []\n        }\n    ],\n    \"config\": {\n        \"timezone\": {\n        \t\"name\": \"IST\", \n        \t\"offset\": -330\n        },\n        \"is_forever\": true,\n        \"until\": \"2020-01-09T14:38:06.658Z\",\n        \"rotate\": true,\n        \"repeat\": true,\n        \"rotation_frequency\": 1,\n        \"repetition\": {\n            \"frequency\": 1,\n            \"repeats_on\": [\n                0,\n                6\n            ],\n            \"type\": \"week\"\n        }\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/schedules/:schedulesID/events",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "schedules",
        ":schedulesID",
        "events"
      ],
      "variable": [
        {
          "key": "schedulesID",
          "value": "5ecf7176f92f2e731262b852",
          "type": "string"
        }
      ]
    },
    "description": "This endpoint is used to create a new on-call rotation(event) for the schedule.\nRequires `access_token` as a `Bearer {{token}}` in the `Authorization` header with `schedule-write` scope.\n\n\nRepresentation of Weekly Repetition:\n```\n0 - Sunday\n1 - Monday\n2 - Tuesday\n3 - Wednesday\n4 - Thursday\n5 - Friday\n6 - Saturday\n```"
  },
  "response": [
    {
      "name": "400 Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"start_time\": \"2020-01-11T18:30:00.000Z\",\n    \"end_time\": \"2020-01-11T18:30:00.000Z\",\n    \"rotation_sets\": [\n        {\n            \"user_ids\": [\"5d81d9187000fb6b9def7e31\"],\n            \"squad_ids\": []\n        },\n        {\n            \"user_ids\": [\"5d90cc3a4d031497494f07e6\"],\n            \"squad_ids\": []\n        }\n    ],\n    \"config\": {\n        \"timezone\": {\n        \t\"name\": \"IST\", \n        \t\"offset\": -330\n        },\n        \"is_forever\": true,\n        \"until\": \"2020-01-09T14:38:06.658Z\",\n        \"rotate\": true,\n        \"repeat\": true,\n        \"rotation_frequency\": 1,\n        \"repetition\": {\n            \"frequency\": 1,\n            \"repeats_on\": [\n                0,\n                6\n            ],\n            \"type\": \"week\"\n        }\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/schedules/:schedulesID/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "schedules",
            ":schedulesID",
            "events"
          ],
          "variable": [
            {
              "key": "schedulesID",
              "value": "5ecf7176f92f2e731262b852"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Date",
          "value": "Thu, 28 May 2020 09:45:33 GMT"
        },
        {
          "key": "Content-Length",
          "value": "70"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"meta\": {\n        \"status\": 400,\n        \"error_message\": \"rotation frequency required\"\n    }\n}"
    },
    {
      "name": "500 Internal Server Error",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"start_time\": \"2020-01-11T18:30:00.000Z\",\n    \"end_time\": \"2020-01-11T18:30:00.000Z\",\n    \"rotation_sets\": [\n        {\n            \"user_ids\": [\"5d81d9187000fb6b9def7e31\"],\n            \"squad_ids\": []\n        },\n        {\n            \"user_ids\": [\"5d90cc3a4d031497494f07e6\"],\n            \"squad_ids\": []\n        }\n    ],\n    \"config\": {\n        \"timezone\": {\n        \t\"name\": \"IST\", \n        \t\"offset\": -330\n        },\n        \"is_forever\": true,\n        \"until\": \"2020-01-09T14:38:06.658Z\",\n        \"rotate\": true,\n        \"repeat\": true,\n        \"rotation_frequency\": 1,\n        \"repetition\": {\n            \"frequency\": 1,\n            \"repeats_on\": [\n                0,\n                6\n            ],\n            \"type\": \"week\"\n        }\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/schedules/:schedulesID/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "schedules",
            ":schedulesID",
            "events"
          ],
          "variable": [
            {
              "key": "schedulesID",
              "value": "5ecf7176f92f2e731262b852"
            }
          ]
        }
      },
      "status": "Internal Server Error",
      "code": 500,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Date",
          "value": "Thu, 28 May 2020 09:46:01 GMT"
        },
        {
          "key": "Content-Length",
          "value": "63"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"meta\": {\n        \"status\": 500,\n        \"error_message\": \"Something went wrong\"\n    }\n}"
    },
    {
      "name": "201 Created",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"start_time\": \"2020-01-11T18:30:00.000Z\",\n    \"end_time\": \"2020-01-11T18:30:00.000Z\",\n    \"rotation_sets\": [\n        {\n            \"user_ids\": [\"5d81d9187000fb6b9def7e31\"],\n            \"squad_ids\": []\n        },\n        {\n            \"user_ids\": [\"5d90cc3a4d031497494f07e6\"],\n            \"squad_ids\": []\n        }\n    ],\n    \"config\": {\n        \"timezone\": {\n        \t\"name\": \"IST\", \n        \t\"offset\": -330\n        },\n        \"is_forever\": true,\n        \"until\": \"2020-01-09T14:38:06.658Z\",\n        \"rotate\": true,\n        \"repeat\": true,\n        \"rotation_frequency\": 1,\n        \"repetition\": {\n            \"frequency\": 1,\n            \"repeats_on\": [\n                0,\n                6\n            ],\n            \"type\": \"week\"\n        }\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/schedules/:schedulesID/events",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "schedules",
            ":schedulesID",
            "events"
          ],
          "variable": [
            {
              "key": "schedulesID",
              "value": "5ecf7176f92f2e731262b852"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Date",
          "value": "Thu, 28 May 2020 09:51:50 GMT"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"data\": [\n        {\n            \"id\": \"5ecf89b4ebddeae2907cd424\",\n            \"start_time\": \"2020-01-12T00:00:00+05:30\",\n            \"end_time\": \"2020-01-12T00:00:00+05:30\",\n            \"name\": \"SRE shift 1\",\n            \"user_ids\": [\n                \"5d81d9187000fb6b9def7e31\"\n            ],\n            \"series_id\": \"5ecf89b4ebddeae2907cd423\",\n            \"squad_ids\": [],\n            \"is_override\": false,\n            \"schedule_id\": \"5ecf7176f92f2e731262b852\",\n            \"calendar\": {\n                \"id\": \"5ecf7176f92f2e731262b852\",\n                \"name\": \"Weekly schedule\",\n                \"slug\": \"weekly-schedule\"\n            }\n        },\n        {\n            \"id\": \"5ecf89b4ebddeae2907cd425\",\n            \"start_time\": \"2020-01-18T00:00:00+05:30\",\n            \"end_time\": \"2020-01-18T00:00:00+05:30\",\n            \"name\": \"SRE shift 1\",\n            \"user_ids\": [\n                \"5d90cc3a4d031497494f07e6\"\n            ],\n            \"series_id\": \"5ecf89b4ebddeae2907cd423\",\n            \"squad_ids\": [],\n            \"is_override\": false,\n            \"schedule_id\": \"5ecf7176f92f2e731262b852\",\n            \"calendar\": {\n                \"id\": \"5ecf7176f92f2e731262b852\",\n                \"name\": \"Weekly schedule\",\n                \"slug\": \"weekly-schedule\"\n            }\n        },\n        {\n            \"id\": \"5ecf89b4ebddeae2907cd426\",\n            \"start_time\": \"2020-01-19T00:00:00+05:30\",\n            \"end_time\": \"2020-01-19T00:00:00+05:30\",\n            \"name\": \"SRE shift 1\",\n            \"user_ids\": [\n                \"5d81d9187000fb6b9def7e31\"\n            ],\n            \"series_id\": \"5ecf89b4ebddeae2907cd423\",\n            \"squad_ids\": [],\n            \"is_override\": false,\n            \"schedule_id\": \"5ecf7176f92f2e731262b852\",\n            \"calendar\": {\n                \"id\": \"5ecf7176f92f2e731262b852\",\n                \"name\": \"Weekly schedule\",\n                \"slug\": \"weekly-schedule\"\n            }\n        }\n    ]\n}"
    }
  ]
}