Sample code for 30+ languages & platforms
Xbase++

Quickbooks Update an Invoice

See more QuickBooks Examples

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

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJsonToken
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oSbAuth
LOCAL oJsonReq
LOCAL oSbRequestBody
LOCAL oSbResponseBody
LOCAL nRespStatusCode
LOCAL oJsonResponse
LOCAL nLineNum
LOCAL cAmount
LOCAL cSalesItemLineDetailTaxCodeRefValue
LOCAL cSalesItemLineDetailItemRefName
LOCAL cSalesItemLineDetailItemRefValue
LOCAL cId
LOCAL cDetailType
LOCAL cDefinitionId
LOCAL cInvType
LOCAL cName
LOCAL cInvoiceTxnDate
LOCAL cInvoiceDomain
LOCAL cInvoicePrintStatus
LOCAL cInvoiceTotalAmt
LOCAL cInvoiceDueDate
LOCAL nInvoiceApplyTaxAfterDiscount
LOCAL cInvoiceDocNumber
LOCAL nInvoiceSparse
LOCAL cInvoiceCustomerMemoValue
LOCAL nInvoiceDeposit
LOCAL cInvoiceBalance
LOCAL cInvoiceCustomerRefName
LOCAL cInvoiceCustomerRefValue
LOCAL nInvoiceTxnTaxDetailTotalTax
LOCAL cInvoiceSyncToken
LOCAL cInvoiceShipAddrCountrySubDivisionCode
LOCAL cInvoiceShipAddrCity
LOCAL cInvoiceShipAddrPostalCode
LOCAL cInvoiceShipAddrId
LOCAL cInvoiceShipAddrLine1
LOCAL cInvoiceEmailStatus
LOCAL cInvoiceBillAddrCountrySubDivisionCode
LOCAL cInvoiceBillAddrCity
LOCAL cInvoiceBillAddrPostalCode
LOCAL cInvoiceBillAddrId
LOCAL cInvoiceBillAddrLine1
LOCAL cInvoiceMetaDataCreateTime
LOCAL cInvoiceMetaDataLastUpdatedTime
LOCAL cInvoiceId
LOCAL cTime
LOCAL i
LOCAL nCount_i

nSuccess := 0

//  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.
oJsonToken := CreateObject("Chilkat.JsonObject")
nSuccess := oJsonToken:LoadFile("qa_data/tokens/qb-access-token.json")

oRest := CreateObject("Chilkat.Rest")

//  Connect to the REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("sandbox-quickbooks.api.intuit.com", nPort, nBTls, nBAutoReconnect)

oSbAuth := CreateObject("Chilkat.StringBuilder")
oSbAuth:Append("Bearer ")
oSbAuth:Append(oJsonToken:StringOf("access_token"))
oRest:Authorization := oSbAuth: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:

//  {
//    "DocNumber": "1070",
//    "SyncToken": "0",
//    "domain": "QBO",
//    "Balance": 150.0,
//    "BillAddr": {
//      "City": "Bayshore",
//      "Line1": "4581 Finch St.",
//      "PostalCode": "94326",
//      "Lat": "INVALID",
//      "Long": "INVALID",
//      "CountrySubDivisionCode": "CA",
//      "Id": "2"
//    },
//    "TxnDate": "2015-07-24",
//    "TotalAmt": 150.0,
//    "CustomerRef": {
//      "name": "Amy's Bird Sanctuary",
//      "value": "1"
//    },
//    "CustomerMemo": {
//      "value": "Added customer memo."
//    },
//    "ShipAddr": {
//      "City": "Bayshore",
//      "Line1": "4581 Finch St.",
//      "PostalCode": "94326",
//      "Lat": "INVALID",
//      "Long": "INVALID",
//      "CountrySubDivisionCode": "CA",
//      "Id": "109"
//    },
//    "LinkedTxn": [
//    ],
//    "DueDate": "2015-08-23",
//    "PrintStatus": "NeedToPrint",
//    "EmailStatus": "NotSet",
//    "sparse": false,
//    "Line": [
//      {
//        "LineNum": 1,
//        "Amount": 150.0,
//        "SalesItemLineDetail": {
//          "TaxCodeRef": {
//            "value": "NON"
//          },
//          "ItemRef": {
//            "name": "Services",
//            "value": "1"
//          }
//        },
//        "Id": "1",
//        "DetailType": "SalesItemLineDetail"
//      },
//      {
//        "DetailType": "SubTotalLineDetail",
//        "Amount": 150.0,
//        "SubTotalLineDetail": {}
//      }
//    ],
//    "ApplyTaxAfterDiscount": false,
//    "CustomField": [
//      {
//        "DefinitionId": "1",
//        "Type": "StringType",
//        "Name": "Crew #"
//      }
//    ],
//    "Id": "239",
//    "TxnTaxDetail": {
//      "TotalTax": 0
//    },
//    "MetaData": {
//      "CreateTime": "2015-07-24T10:35:08-07:00",
//      "LastUpdatedTime": "2015-07-24T10:35:08-07:00"
//    }
//  }
//  
//  Use the this online tool to generate the code from sample JSON: 
//  Generate Code to Create JSON

oJsonReq := CreateObject("Chilkat.JsonObject")
oJsonReq:UpdateString("DocNumber", "1070")
oJsonReq:UpdateString("SyncToken", "0")
oJsonReq:UpdateString("domain", "QBO")
oJsonReq:UpdateNumber("Balance", "150.0")
oJsonReq:UpdateString("BillAddr.City", "Bayshore")
oJsonReq:UpdateString("BillAddr.Line1", "4581 Finch St.")
oJsonReq:UpdateString("BillAddr.PostalCode", "94326")
oJsonReq:UpdateString("BillAddr.Lat", "INVALID")
oJsonReq:UpdateString("BillAddr.Long", "INVALID")
oJsonReq:UpdateString("BillAddr.CountrySubDivisionCode", "CA")
oJsonReq:UpdateString("BillAddr.Id", "2")
oJsonReq:UpdateString("TxnDate", "2015-07-24")
oJsonReq:UpdateNumber("TotalAmt", "150.0")
oJsonReq:UpdateString("CustomerRef.name", "Amy's Bird Sanctuary")
oJsonReq:UpdateString("CustomerRef.value", "1")
oJsonReq:UpdateString("CustomerMemo.value", "Added customer memo.")
oJsonReq:UpdateString("ShipAddr.City", "Bayshore")
oJsonReq:UpdateString("ShipAddr.Line1", "4581 Finch St.")
oJsonReq:UpdateString("ShipAddr.PostalCode", "94326")
oJsonReq:UpdateString("ShipAddr.Lat", "INVALID")
oJsonReq:UpdateString("ShipAddr.Long", "INVALID")
oJsonReq:UpdateString("ShipAddr.CountrySubDivisionCode", "CA")
oJsonReq:UpdateString("ShipAddr.Id", "109")
oJsonReq:UpdateNewArray("LinkedTxn")
oJsonReq:UpdateString("DueDate", "2015-08-23")
oJsonReq:UpdateString("PrintStatus", "NeedToPrint")
oJsonReq:UpdateString("EmailStatus", "NotSet")
oJsonReq:UpdateBool("sparse", 0)
oJsonReq:UpdateInt("Line[0].LineNum", 1)
oJsonReq:UpdateNumber("Line[0].Amount", "150.0")
oJsonReq:UpdateString("Line[0].SalesItemLineDetail.TaxCodeRef.value", "NON")
oJsonReq:UpdateString("Line[0].SalesItemLineDetail.ItemRef.name", "Services")
oJsonReq:UpdateString("Line[0].SalesItemLineDetail.ItemRef.value", "1")
oJsonReq:UpdateString("Line[0].Id", "1")
oJsonReq:UpdateString("Line[0].DetailType", "SalesItemLineDetail")
oJsonReq:UpdateString("Line[1].DetailType", "SubTotalLineDetail")
oJsonReq:UpdateNumber("Line[1].Amount", "150.0")
oJsonReq:UpdateNewObject("Line[1].SubTotalLineDetail")
oJsonReq:UpdateBool("ApplyTaxAfterDiscount", 0)
oJsonReq:UpdateString("CustomField[0].DefinitionId", "1")
oJsonReq:UpdateString("CustomField[0].Type", "StringType")
oJsonReq:UpdateString("CustomField[0].Name", "Crew #")
oJsonReq:UpdateString("Id", "239")
oJsonReq:UpdateInt("TxnTaxDetail.TotalTax", 0)
oJsonReq:UpdateString("MetaData.CreateTime", "2015-07-24T10:35:08-07:00")
oJsonReq:UpdateString("MetaData.LastUpdatedTime", "2015-07-24T10:35:08-07:00")

oSbRequestBody := CreateObject("Chilkat.StringBuilder")
oJsonReq:EmitSb(oSbRequestBody)

oRest:AddHeader("Content-Type", "application/json")
oRest:AddHeader("Accept", "application/json")
oRest:AllowHeaderFolding := 0

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestSb("POST", "/v3/company/<realmID>/invoice", oSbRequestBody, oSbResponseBody)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oJsonToken:destroy()
    oRest:destroy()
    oSbAuth:destroy()
    oJsonReq:destroy()
    oSbRequestBody:destroy()
    oSbResponseBody:destroy()
    RETURN
ENDIF

nRespStatusCode := oRest:ResponseStatusCode

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

oJsonResponse := CreateObject("Chilkat.JsonObject")
oJsonResponse:LoadSb(oSbResponseBody)
oJsonResponse:EmitCompact := 0
? oJsonResponse:Emit()

IF (oRest:ResponseStatusCode != 200)
    ? "Failed."
    oJsonToken:destroy()
    oRest:destroy()
    oSbAuth:destroy()
    oJsonReq:destroy()
    oSbRequestBody:destroy()
    oSbResponseBody:destroy()
    oJsonResponse:destroy()
    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": {
//      "TxnDate": "2015-07-24",
//      "domain": "QBO",
//      "PrintStatus": "NeedToPrint",
//      "TotalAmt": 150.0,
//      "Line": [
//        {
//          "LineNum": 1,
//          "Amount": 150.0,
//          "SalesItemLineDetail": {
//            "TaxCodeRef": {
//              "value": "NON"
//            },
//            "ItemRef": {
//              "name": "Services",
//              "value": "1"
//            }
//          },
//          "Id": "1",
//          "DetailType": "SalesItemLineDetail"
//        },
//        {
//          "DetailType": "SubTotalLineDetail",
//          "Amount": 150.0,
//          "SubTotalLineDetail": {}
//        }
//      ],
//      "DueDate": "2015-08-23",
//      "ApplyTaxAfterDiscount": false,
//      "DocNumber": "1070",
//      "sparse": false,
//      "CustomerMemo": {
//        "value": "Added customer memo."
//      },
//      "Deposit": 0,
//      "Balance": 150.0,
//      "CustomerRef": {
//        "name": "Amy's Bird Sanctuary",
//        "value": "1"
//      },
//      "TxnTaxDetail": {
//        "TotalTax": 0
//      },
//      "SyncToken": "1",
//      "LinkedTxn": [
//      ],
//      "ShipAddr": {
//        "CountrySubDivisionCode": "CA",
//        "City": "Bayshore",
//        "PostalCode": "94326",
//        "Id": "118",
//        "Line1": "4581 Finch St."
//      },
//      "EmailStatus": "NotSet",
//      "BillAddr": {
//        "CountrySubDivisionCode": "CA",
//        "City": "Bayshore",
//        "PostalCode": "94326",
//        "Id": "117",
//        "Line1": "4581 Finch St."
//      },
//      "MetaData": {
//        "CreateTime": "2015-07-24T10:35:08-07:00",
//        "LastUpdatedTime": "2015-07-24T10:53:39-07:00"
//      },
//      "CustomField": [
//        {
//          "DefinitionId": "1",
//          "Type": "StringType",
//          "Name": "Crew #"
//        }
//      ],
//      "Id": "239"
//    },
//    "time": "2015-07-24T10:53:39.287-07:00"
//  }
//  

cInvoiceTxnDate := oJsonResponse:StringOf("Invoice.TxnDate")
cInvoiceDomain := oJsonResponse:StringOf("Invoice.domain")
cInvoicePrintStatus := oJsonResponse:StringOf("Invoice.PrintStatus")
cInvoiceTotalAmt := oJsonResponse:StringOf("Invoice.TotalAmt")
cInvoiceDueDate := oJsonResponse:StringOf("Invoice.DueDate")
nInvoiceApplyTaxAfterDiscount := oJsonResponse:BoolOf("Invoice.ApplyTaxAfterDiscount")
cInvoiceDocNumber := oJsonResponse:StringOf("Invoice.DocNumber")
nInvoiceSparse := oJsonResponse:BoolOf("Invoice.sparse")
cInvoiceCustomerMemoValue := oJsonResponse:StringOf("Invoice.CustomerMemo.value")
nInvoiceDeposit := oJsonResponse:IntOf("Invoice.Deposit")
cInvoiceBalance := oJsonResponse:StringOf("Invoice.Balance")
cInvoiceCustomerRefName := oJsonResponse:StringOf("Invoice.CustomerRef.name")
cInvoiceCustomerRefValue := oJsonResponse:StringOf("Invoice.CustomerRef.value")
nInvoiceTxnTaxDetailTotalTax := oJsonResponse:IntOf("Invoice.TxnTaxDetail.TotalTax")
cInvoiceSyncToken := oJsonResponse:StringOf("Invoice.SyncToken")
cInvoiceShipAddrCountrySubDivisionCode := oJsonResponse:StringOf("Invoice.ShipAddr.CountrySubDivisionCode")
cInvoiceShipAddrCity := oJsonResponse:StringOf("Invoice.ShipAddr.City")
cInvoiceShipAddrPostalCode := oJsonResponse:StringOf("Invoice.ShipAddr.PostalCode")
cInvoiceShipAddrId := oJsonResponse:StringOf("Invoice.ShipAddr.Id")
cInvoiceShipAddrLine1 := oJsonResponse:StringOf("Invoice.ShipAddr.Line1")
cInvoiceEmailStatus := oJsonResponse:StringOf("Invoice.EmailStatus")
cInvoiceBillAddrCountrySubDivisionCode := oJsonResponse:StringOf("Invoice.BillAddr.CountrySubDivisionCode")
cInvoiceBillAddrCity := oJsonResponse:StringOf("Invoice.BillAddr.City")
cInvoiceBillAddrPostalCode := oJsonResponse:StringOf("Invoice.BillAddr.PostalCode")
cInvoiceBillAddrId := oJsonResponse:StringOf("Invoice.BillAddr.Id")
cInvoiceBillAddrLine1 := oJsonResponse:StringOf("Invoice.BillAddr.Line1")
cInvoiceMetaDataCreateTime := oJsonResponse:StringOf("Invoice.MetaData.CreateTime")
cInvoiceMetaDataLastUpdatedTime := oJsonResponse:StringOf("Invoice.MetaData.LastUpdatedTime")
cInvoiceId := oJsonResponse:StringOf("Invoice.Id")
cTime := oJsonResponse:StringOf("time")
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.Line")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    nLineNum := oJsonResponse:IntOf("Invoice.Line[i].LineNum")
    cAmount := oJsonResponse:StringOf("Invoice.Line[i].Amount")
    cSalesItemLineDetailTaxCodeRefValue := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
    cSalesItemLineDetailItemRefName := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
    cSalesItemLineDetailItemRefValue := oJsonResponse:StringOf("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
    cId := oJsonResponse:StringOf("Invoice.Line[i].Id")
    cDetailType := oJsonResponse:StringOf("Invoice.Line[i].DetailType")
    i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.LinkedTxn")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    i := i + 1
ENDDO
i := 0
nCount_i := oJsonResponse:SizeOfArray("Invoice.CustomField")
DO WHILE i < nCount_i
    oJsonResponse:I := i
    cDefinitionId := oJsonResponse:StringOf("Invoice.CustomField[i].DefinitionId")
    cInvType := oJsonResponse:StringOf("Invoice.CustomField[i].Type")
    cName := oJsonResponse:StringOf("Invoice.CustomField[i].Name")
    i := i + 1
ENDDO

oJsonToken:destroy()
oRest:destroy()
oSbAuth:destroy()
oJsonReq:destroy()
oSbRequestBody:destroy()
oSbResponseBody:destroy()
oJsonResponse:destroy()