SQL Server Requires Chilkat v11.0.0+
SQL Server
SendGrid: Add or update list contacts and check the import job
See more SendGrid Examples
Use a Marketing Campaigns API key with contact access and the ID of an existing list. A 202 response queues the upsert; it does not confirm that the contacts were saved. Save the job ID and use it to recheck a pending import without submitting the contacts again. The error report may contain contact data; handle it privately.
Add or update contacts ยท Import job status
Chilkat SQL Server Downloads
-- 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 @iTmp1 int
DECLARE @iTmp2 int
DECLARE @sTmp0 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
DECLARE @jobId nvarchar(4000)
SELECT @jobId = ''
IF @jobId = ''
BEGIN
DECLARE @contacts int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @contacts OUT
EXEC sp_OAMethod @contacts, 'UpdateString', @success OUT, 'list_ids[0]', 'YOUR_LIST_UUID'
EXEC sp_OAMethod @contacts, 'UpdateString', @success OUT, 'contacts[0].email', 'avery@example.com'
EXEC sp_OAMethod @contacts, 'UpdateString', @success OUT, 'contacts[0].first_name', 'Avery'
EXEC sp_OAMethod @contacts, 'UpdateString', @success OUT, 'contacts[1].email', 'robin@example.com'
EXEC sp_OAMethod @contacts, 'UpdateString', @success OUT, 'contacts[1].first_name', 'Robin'
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'PUT', 'https://api.sendgrid.com/v3/marketing/contacts', @contacts, '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 @contacts
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 202
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 @contacts
RETURN
END
DECLARE @queued int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @queued OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @queued, '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 @contacts
EXEC @hr = sp_OADestroy @queued
RETURN
END
EXEC sp_OAMethod @queued, 'HasMember', @iTmp0 OUT, 'job_id'
IF @iTmp0 <> 1
BEGIN
PRINT 'No job ID returned.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @contacts
EXEC @hr = sp_OADestroy @queued
RETURN
END
EXEC sp_OAMethod @queued, 'StringOf', @jobId OUT, 'job_id'
PRINT 'Save this import job ID: ' + @jobId
END
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'job', @jobId
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}', @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 @contacts
EXEC @hr = sp_OADestroy @queued
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
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 @contacts
EXEC @hr = sp_OADestroy @queued
RETURN
END
DECLARE @job int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @job OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @job, '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 @contacts
EXEC @hr = sp_OADestroy @queued
EXEC @hr = sp_OADestroy @job
RETURN
END
DECLARE @state nvarchar(4000)
EXEC sp_OAMethod @job, 'StringOf', @state OUT, 'status'
IF @state = 'pending'
BEGIN
PRINT 'Still pending. Check again later with jobId = ' + @jobId
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @contacts
EXEC @hr = sp_OADestroy @queued
EXEC @hr = sp_OADestroy @job
RETURN
END
PRINT 'Job status: ' + @state
EXEC sp_OAMethod @job, 'IntOf', @iTmp0 OUT, 'results.created_count'
EXEC sp_OAMethod @job, 'IntOf', @iTmp1 OUT, 'results.updated_count'
EXEC sp_OAMethod @job, 'IntOf', @iTmp2 OUT, 'results.errored_count'
PRINT 'Created: ' + @iTmp0 + ', updated: ' + @iTmp1 + ', errors: ' + @iTmp2
IF (@state = 'errored') or (@state = 'failed')
BEGIN
PRINT 'Review the job before retrying. If present, results.errors_url identifies the private error report.'
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @contacts
EXEC @hr = sp_OADestroy @queued
EXEC @hr = sp_OADestroy @job
END
GO