Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

SendGrid: Create a newsletter draft, then schedule the reviewed draft

See more SendGrid Examples

Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.

Create a Single Send · Schedule a Single Send

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
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    --  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    EXEC sp_OASetProperty @http, 'AuthToken', 'SENDGRID_API_KEY'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    --  Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
    DECLARE @reviewedDraftId nvarchar(4000)
    SELECT @reviewedDraftId = ''
    IF @reviewedDraftId = ''
      BEGIN
        DECLARE @draft int
        EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @draft OUT

        EXEC sp_OAMethod @draft, 'UpdateString', @success OUT, 'name', 'November newsletter'
        EXEC sp_OAMethod @draft, 'UpdateString', @success OUT, 'send_to.list_ids[0]', 'YOUR_LIST_UUID'
        EXEC sp_OAMethod @draft, 'UpdateInt', @success OUT, 'email_config.sender_id', 12345
        EXEC sp_OAMethod @draft, 'UpdateInt', @success OUT, 'email_config.suppression_group_id', 67890
        EXEC sp_OAMethod @draft, 'UpdateString', @success OUT, 'email_config.design_id', 'YOUR_NEWSLETTER_DESIGN_UUID'
        EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.sendgrid.com/v3/marketing/singlesends', @draft, 'application/json', @resp
        IF @success <> 1
          BEGIN
            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            IF @iTmp0 = 0
              BEGIN
                EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
              END
            ELSE
              BEGIN

                EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

                EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
                PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
              END
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @draft
            RETURN
          END
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 <> 201
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @draft
            RETURN
          END

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

        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @created, 'Load', @success OUT, @sTmp0
        IF @success <> 1
          BEGIN

            PRINT 'Unexpected response: expected JSON.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @draft
            EXEC @hr = sp_OADestroy @created
            RETURN
          END
        DECLARE @draftId nvarchar(4000)
        EXEC sp_OAMethod @created, 'StringOf', @draftId OUT, 'id'
        EXEC sp_OAMethod @created, 'HasMember', @iTmp0 OUT, 'id'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'Missing id in response.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @draft
            EXEC @hr = sp_OADestroy @created
            RETURN
          END


        PRINT 'Draft created: ' + @draftId + '. Review it in Marketing Campaigns before scheduling.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @draft
        EXEC @hr = sp_OADestroy @created
        RETURN
      END
    --  The example selects tomorrow at the current UTC time. Use your approved send time.
    DECLARE @releaseTime int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @releaseTime OUT

    EXEC sp_OAMethod @releaseTime, 'SetFromCurrentSystemTime', @success OUT
    EXEC sp_OAMethod @releaseTime, 'AddDays', @success OUT, 1
    DECLARE @schedule int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @schedule OUT

    EXEC sp_OAMethod @releaseTime, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @schedule, 'UpdateString', @success OUT, 'send_at', @sTmp0
    EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'draft', @reviewedDraftId

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'PUT', 'https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule', @schedule, 'application/json', @resp
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
          END
        ELSE
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

            EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
            PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
          END
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @draft
        EXEC @hr = sp_OADestroy @created
        EXEC @hr = sp_OADestroy @releaseTime
        EXEC @hr = sp_OADestroy @schedule
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 201
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT

        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        PRINT 'HTTP ' + @iTmp0 + ': ' + @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @draft
        EXEC @hr = sp_OADestroy @created
        EXEC @hr = sp_OADestroy @releaseTime
        EXEC @hr = sp_OADestroy @schedule
        RETURN
      END

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

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @scheduled, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Unexpected response: expected JSON.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @draft
        EXEC @hr = sp_OADestroy @created
        EXEC @hr = sp_OADestroy @releaseTime
        EXEC @hr = sp_OADestroy @schedule
        EXEC @hr = sp_OADestroy @scheduled
        RETURN
      END
    EXEC sp_OAMethod @scheduled, 'StringOf', @sTmp0 OUT, 'status'

    EXEC sp_OAMethod @scheduled, 'StringOf', @sTmp1 OUT, 'send_at'
    PRINT @sTmp0 + ': ' + @sTmp1

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @draft
    EXEC @hr = sp_OADestroy @created
    EXEC @hr = sp_OADestroy @releaseTime
    EXEC @hr = sp_OADestroy @schedule
    EXEC @hr = sp_OADestroy @scheduled


END
GO