Sample code for 30+ languages & platforms
SQL Server

IntakeQ: Send a Questionnaire

See more IntakeQ Examples

Send an intake package using the API.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    DECLARE @success int
    SELECT @success = 0

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

    -- Implements the following CURL command:

    -- curl -H "Content-Type: application/json" \
    --      -H 'X-Auth-Key: xxxxxxxxxxxxxxxxxxxxxxxxx' \
    --      -X POST \
    --      -d '{     		
    -- 	"QuestionnaireId": "00000000-0000-0000-0000-000000000000",     		
    -- 	"ClientId": 123, 
    -- 	"ClientName": "Dexter Morgan",
    -- 	"ClientEmail": "dexter@email.com",
    -- 	"ClientPhone": "2222222222", 
    -- 	"PractitionerId": "00000000-0000-0000-0000-000000000000",
    -- }' https://intakeq.com/api/v1/intakes/send

    -- Use the following online tool to generate HTTP code from a CURL command
    -- Convert a cURL Command to HTTP Source Code

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

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

    -- {
    --   "QuestionnaireId": "00000000-0000-0000-0000-000000000000",
    --   "ClientId": 123,
    --   "ClientName": "Dexter Morgan",
    --   "ClientEmail": "dexter@email.com",
    --   "ClientPhone": "2222222222",
    --   "PractitionerId": "00000000-0000-0000-0000-000000000000"
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'QuestionnaireId', '00000000-0000-0000-0000-000000000000'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'ClientId', 123
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ClientName', 'Dexter Morgan'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ClientEmail', 'dexter@email.com'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'ClientPhone', '2222222222'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'PractitionerId', '00000000-0000-0000-0000-000000000000'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Auth-Key', 'xxxxxxxxxxxxxxxxxxxxxxxxx'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://intakeq.com/api/v1/intakes/send', @json, 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
    DECLARE @jResp int
    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 @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        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)

    -- {
    --   "Id": "00000000-0000-0000-0000-000000000000",
    --   "ClientName": "test",
    --   "ClientEmail": "test@email.com",
    --   "ClientId": 9999,
    --   "Status": "Completed",
    --   "DateCreated": 1458526480368,
    --   "DateSubmitted": 1458526532654,
    --   "QuestionnaireName": "test",
    --   "Practitioner": "test@email.com",
    --   "PractitionerName": "FirstName LastName",
    --   "Questions": [
    --     {
    --       "Id": "xxxx-1",
    --       "Text": "Full name",
    --       "Answer": "Dexter Morgan",
    --       "QuestionType": "OpenQuestion",
    --       "Rows": [
    --       ],
    --       "ColumnNames": [
    --       ],
    --       "OfficeUse": false,
    --       "OfficeNote": "aaa"
    --     },
    --     {
    --       "Id": "xxxx-2",
    --       "Text": "Full name",
    --       "Answer": "Dexter Morgan",
    --       "QuestionType": "OpenQuestion",
    --       "Rows": [
    --       ],
    --       "ColumnNames": [
    --       ],
    --       "OfficeUse": false,
    --       "OfficeNote": "aaa"
    --     }
    --   ],
    --   "AppointmentId": "xxxxxxxx",
    --   "ConsentForms": [
    --     {
    --       "Id": "00000000-0000-0000-0000-000000000000",
    --       "Name": "HIPAA Release Form",
    --       "DocumentType": "Html",
    --       "Signed": true,
    --       "DateSubmitted": 1458526532654
    --     }
    --   ]
    -- }

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

    DECLARE @Text nvarchar(4000)

    DECLARE @Answer nvarchar(4000)

    DECLARE @QuestionType nvarchar(4000)

    DECLARE @OfficeUse int

    DECLARE @OfficeNote nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @Name nvarchar(4000)

    DECLARE @DocumentType nvarchar(4000)

    DECLARE @Signed int

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'Id'
    DECLARE @ClientName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ClientName OUT, 'ClientName'
    DECLARE @ClientEmail nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ClientEmail OUT, 'ClientEmail'
    DECLARE @ClientId int
    EXEC sp_OAMethod @jResp, 'IntOf', @ClientId OUT, 'ClientId'
    DECLARE @Status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Status OUT, 'Status'
    DECLARE @DateCreated int
    EXEC sp_OAMethod @jResp, 'IntOf', @DateCreated OUT, 'DateCreated'
    DECLARE @DateSubmitted int
    EXEC sp_OAMethod @jResp, 'IntOf', @DateSubmitted OUT, 'DateSubmitted'
    DECLARE @QuestionnaireName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @QuestionnaireName OUT, 'QuestionnaireName'
    DECLARE @Practitioner nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Practitioner OUT, 'Practitioner'
    DECLARE @PractitionerName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PractitionerName OUT, 'PractitionerName'
    DECLARE @AppointmentId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AppointmentId OUT, 'AppointmentId'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'Questions'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'Questions[i].Id'
        EXEC sp_OAMethod @jResp, 'StringOf', @Text OUT, 'Questions[i].Text'
        EXEC sp_OAMethod @jResp, 'StringOf', @Answer OUT, 'Questions[i].Answer'
        EXEC sp_OAMethod @jResp, 'StringOf', @QuestionType OUT, 'Questions[i].QuestionType'
        EXEC sp_OAMethod @jResp, 'BoolOf', @OfficeUse OUT, 'Questions[i].OfficeUse'
        EXEC sp_OAMethod @jResp, 'StringOf', @OfficeNote OUT, 'Questions[i].OfficeNote'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'Questions[i].Rows'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'Questions[i].ColumnNames'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'ConsentForms'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'ConsentForms[i].Id'
        EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'ConsentForms[i].Name'
        EXEC sp_OAMethod @jResp, 'StringOf', @DocumentType OUT, 'ConsentForms[i].DocumentType'
        EXEC sp_OAMethod @jResp, 'BoolOf', @Signed OUT, 'ConsentForms[i].Signed'
        EXEC sp_OAMethod @jResp, 'IntOf', @DateSubmitted OUT, 'ConsentForms[i].DateSubmitted'
        SELECT @i = @i + 1
      END

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


END
GO