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

SendGrid: Schedule a reminder and cancel it by batch ID

See more SendGrid Examples

Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.

Create a batch ID ยท Cancel a batch

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Resp
string ls_BatchToCancel
oleobject loo_Cancellation
oleobject loo_Batch
string ls_BatchId
oleobject loo_SendTime
oleobject loo_Message

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")

//  On cancellation, set this to the batch ID saved with the original appointment.
//  This branch does not create a new batch or send another reminder.
ls_BatchToCancel = ""
if ls_BatchToCancel <> "" then
    loo_Cancellation = create oleobject
    li_rc = loo_Cancellation.ConnectToNewObject("Chilkat.JsonObject")

    loo_Cancellation.UpdateString("batch_id",ls_BatchToCancel)
    loo_Cancellation.UpdateString("status","cancel")
    li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",loo_Cancellation,"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_Cancellation
        return
    end if

    if loo_Resp.StatusCode <> 201 then
        Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
        destroy loo_Http
        destroy loo_Resp
        destroy loo_Cancellation
        return
    end if

    Write-Debug "Batch cancellation accepted."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Cancellation
    return
end if

li_Success = loo_Http.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",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_Cancellation
    return
end if

if loo_Resp.StatusCode <> 201 then
    Write-Debug "HTTP " + string(loo_Resp.StatusCode) + ": " + loo_Resp.BodyStr
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Cancellation
    return
end if

loo_Batch = create oleobject
li_rc = loo_Batch.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Batch.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Unexpected response: expected JSON."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Cancellation
    destroy loo_Batch
    return
end if

ls_BatchId = loo_Batch.StringOf("batch_id")
if loo_Batch.HasMember("batch_id") <> 1 then
    Write-Debug "Missing batch_id in response."
    destroy loo_Http
    destroy loo_Resp
    destroy loo_Cancellation
    destroy loo_Batch
    return
end if

//  Allocate one batch per independently cancelable appointment.
Write-Debug "Save this batch ID with the appointment: " + ls_BatchId
loo_SendTime = create oleobject
li_rc = loo_SendTime.ConnectToNewObject("Chilkat.CkDateTime")

loo_SendTime.SetFromCurrentSystemTime()
loo_SendTime.AddSeconds(3600)
loo_Message = create oleobject
li_rc = loo_Message.ConnectToNewObject("Chilkat.JsonObject")

loo_Message.UpdateString("batch_id",ls_BatchId)
loo_Message.UpdateInt("send_at",loo_SendTime.GetAsUnixTime(0))
loo_Message.UpdateString("from.email","sender@example.com")
loo_Message.UpdateString("personalizations[0].to[0].email","recipient@example.com")
loo_Message.UpdateString("subject","Appointment reminder")
loo_Message.UpdateString("content[0].type","text/plain")
loo_Message.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.")

li_Success = loo_Http.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",loo_Message,"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_Cancellation
    destroy loo_Batch
    destroy loo_SendTime
    destroy loo_Message
    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_Cancellation
    destroy loo_Batch
    destroy loo_SendTime
    destroy loo_Message
    return
end if

Write-Debug "Reminder scheduled for " + loo_SendTime.GetAsTimestamp(0)


destroy loo_Http
destroy loo_Resp
destroy loo_Cancellation
destroy loo_Batch
destroy loo_SendTime
destroy loo_Message