PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
string ls_JobId
oleobject loo_Contacts
oleobject loo_Queued
oleobject loo_Job
string ls_State
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
loo_Http.AuthToken = "SENDGRID_API_KEY"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
ls_JobId = ""
if ls_JobId = "" then
loo_Contacts = create oleobject
li_rc = loo_Contacts.ConnectToNewObject("Chilkat.JsonObject")
loo_Contacts.UpdateString("list_ids[0]","YOUR_LIST_UUID")
loo_Contacts.UpdateString("contacts[0].email","avery@example.com")
loo_Contacts.UpdateString("contacts[0].first_name","Avery")
loo_Contacts.UpdateString("contacts[1].email","robin@example.com")
loo_Contacts.UpdateString("contacts[1].first_name","Robin")
li_Success = loo_Http.HttpJson("PUT","https://api.sendgrid.com/v3/marketing/contacts",loo_Contacts,"application/json",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
return
end if
if loo_Resp.StatusCode <> 202 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
return
end if
loo_Queued = create oleobject
li_rc = loo_Queued.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Queued.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Unexpected response: expected JSON."
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
return
end if
if loo_Queued.HasMember("job_id") <> 1 then
Write-Debug "No job ID returned."
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
return
end if
ls_JobId = loo_Queued.StringOf("job_id")
Write-Debug "Save this import job ID: " + ls_JobId
end if
loo_Http.SetUrlVar("job",ls_JobId)
li_Success = loo_Http.HttpNoBody("GET","https://api.sendgrid.com/v3/marketing/contacts/imports/{$job}",loo_Resp)
if li_Success <> 1 then
if loo_Resp.StatusCode = 0 then
Write-Debug loo_Http.LastErrorText
else
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
end if
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
return
end if
if loo_Resp.StatusCode <> 200 then
Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
return
end if
loo_Job = create oleobject
li_rc = loo_Job.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Job.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Unexpected response: expected JSON."
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
destroy loo_Job
return
end if
ls_State = loo_Job.StringOf("status")
if ls_State = "pending" then
Write-Debug "Still pending. Check again later with jobId = " + ls_JobId
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
destroy loo_Job
return
end if
Write-Debug "Job status: " + ls_State
Write-Debug "Created: " + string(loo_Job.IntOf("results.created_count")) + ", updated: " + string(loo_Job.IntOf("results.updated_count")) + ", errors: " + string(loo_Job.IntOf("results.errored_count"))
if (ls_State = "errored") OR (ls_State = "failed") then
Write-Debug "Review the job before retrying. If present, results.errors_url identifies the private error report."
end if
destroy loo_Http
destroy loo_Resp
destroy loo_Contacts
destroy loo_Queued
destroy loo_Job