Sample code for 30+ languages & platforms
Lianja

Quickbooks Delete an Invoice

See more QuickBooks Examples

Demonstrates how to delete an invoice using the Quickbooks REST API.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// First get our previously obtained OAuth2 access token.
loJsonToken = createobject("CkJsonObject")
llSuccess = loJsonToken.LoadFile("qa_data/tokens/qb-access-token.json")

loRest = createobject("CkRest")

// Connect to the REST server.
llBTls = .T.
lnPort = 443
llBAutoReconnect = .T.
llSuccess = loRest.Connect("sandbox-quickbooks.api.intuit.com",lnPort,llBTls,llBAutoReconnect)

loSbAuth = createobject("CkStringBuilder")
loSbAuth.Append("Bearer ")
loSbAuth.Append(loJsonToken.StringOf("access_token"))
loRest.Authorization = loSbAuth.GetAsString()

// --------------------------------------------------------------------------
// Note: The above code to setup the initial REST connection
// can be done once.  After connecting, any number of REST calls can be made.
// If the connection is lost, the next REST method call will automatically
// reconnect if needed.
// --------------------------------------------------------------------------

// Create the following JSON:

// {
//   "SyncToken": "3",
//   "Id": "33"
// }
// 
// Use the this online tool to generate the code from sample JSON: 
// Generate Code to Create JSON

loJsonReq = createobject("CkJsonObject")
loJsonReq.UpdateString("SyncToken","3")
loJsonReq.UpdateString("Id","33")

loSbRequestBody = createobject("CkStringBuilder")
loJsonReq.EmitSb(loSbRequestBody)

loRest.AddHeader("Content-Type","application/json")
loRest.AddHeader("Accept","application/json")
loRest.AllowHeaderFolding = .F.

loSbResponseBody = createobject("CkStringBuilder")
llSuccess = loRest.FullRequestSb("POST","/v3/company/<realmID>/invoice?operation=delete",loSbRequestBody,loSbResponseBody)
if (llSuccess <> .T.) then
    ? loRest.LastErrorText
    release loJsonToken
    release loRest
    release loSbAuth
    release loJsonReq
    release loSbRequestBody
    release loSbResponseBody
    return
endif

lnRespStatusCode = loRest.ResponseStatusCode

// Success is indicated by a 200 response status code.
? "response status code = " + str(lnRespStatusCode)

loJsonResponse = createobject("CkJsonObject")
loJsonResponse.LoadSb(loSbResponseBody)
loJsonResponse.EmitCompact = .F.
? loJsonResponse.Emit()

if (loRest.ResponseStatusCode <> 200) then
    ? "Failed."
    release loJsonToken
    release loRest
    release loSbAuth
    release loJsonReq
    release loSbRequestBody
    release loSbResponseBody
    release loJsonResponse
    return
endif

// Sample output...
// (See the parsing code below..)
// 
// Use the this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "Invoice": {
//     "status": "Deleted",
//     "domain": "QBO",
//     "Id": "33"
//   },
//   "time": "2013-03-15T00:18:15.322-07:00"
// }
// 

lcInvoiceStatus = loJsonResponse.StringOf("Invoice.status")
lcInvoiceDomain = loJsonResponse.StringOf("Invoice.domain")
lcInvoiceId = loJsonResponse.StringOf("Invoice.Id")
lcTime = loJsonResponse.StringOf("time")


release loJsonToken
release loRest
release loSbAuth
release loJsonReq
release loSbRequestBody
release loSbResponseBody
release loJsonResponse