Lua
Lua
Quickbooks Update an Invoice
See more QuickBooks Examples
Demonstrates how to update an invoice using the Quickbooks REST API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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.
local jsonToken = chilkat.JsonObject.new()
local success = jsonToken:load_file("qa_data/tokens/qb-access-token.json")
local rest = chilkat.Rest.new()
-- Connect to the REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
success = rest:connect("sandbox-quickbooks.api.intuit.com", port, bTls, bAutoReconnect)
local sbAuth = chilkat.StringBuilder.new()
sbAuth:append("Bearer ")
sbAuth:append(jsonToken:string_of("access_token"))
rest.authorization = sbAuth:get_as_string()
-- --------------------------------------------------------------------------
-- 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
local jsonReq = chilkat.JsonObject.new()
jsonReq:update_string("DocNumber", "1070")
jsonReq:update_string("SyncToken", "0")
jsonReq:update_string("domain", "QBO")
jsonReq:update_number("Balance", "150.0")
jsonReq:update_string("BillAddr.City", "Bayshore")
jsonReq:update_string("BillAddr.Line1", "4581 Finch St.")
jsonReq:update_string("BillAddr.PostalCode", "94326")
jsonReq:update_string("BillAddr.Lat", "INVALID")
jsonReq:update_string("BillAddr.Long", "INVALID")
jsonReq:update_string("BillAddr.CountrySubDivisionCode", "CA")
jsonReq:update_string("BillAddr.Id", "2")
jsonReq:update_string("TxnDate", "2015-07-24")
jsonReq:update_number("TotalAmt", "150.0")
jsonReq:update_string("CustomerRef.name", "Amy's Bird Sanctuary")
jsonReq:update_string("CustomerRef.value", "1")
jsonReq:update_string("CustomerMemo.value", "Added customer memo.")
jsonReq:update_string("ShipAddr.City", "Bayshore")
jsonReq:update_string("ShipAddr.Line1", "4581 Finch St.")
jsonReq:update_string("ShipAddr.PostalCode", "94326")
jsonReq:update_string("ShipAddr.Lat", "INVALID")
jsonReq:update_string("ShipAddr.Long", "INVALID")
jsonReq:update_string("ShipAddr.CountrySubDivisionCode", "CA")
jsonReq:update_string("ShipAddr.Id", "109")
jsonReq:update_new_array("LinkedTxn")
jsonReq:update_string("DueDate", "2015-08-23")
jsonReq:update_string("PrintStatus", "NeedToPrint")
jsonReq:update_string("EmailStatus", "NotSet")
jsonReq:update_bool("sparse", false)
jsonReq:update_int("Line[0].LineNum", 1)
jsonReq:update_number("Line[0].Amount", "150.0")
jsonReq:update_string("Line[0].SalesItemLineDetail.TaxCodeRef.value", "NON")
jsonReq:update_string("Line[0].SalesItemLineDetail.ItemRef.name", "Services")
jsonReq:update_string("Line[0].SalesItemLineDetail.ItemRef.value", "1")
jsonReq:update_string("Line[0].Id", "1")
jsonReq:update_string("Line[0].DetailType", "SalesItemLineDetail")
jsonReq:update_string("Line[1].DetailType", "SubTotalLineDetail")
jsonReq:update_number("Line[1].Amount", "150.0")
jsonReq:update_new_object("Line[1].SubTotalLineDetail")
jsonReq:update_bool("ApplyTaxAfterDiscount", false)
jsonReq:update_string("CustomField[0].DefinitionId", "1")
jsonReq:update_string("CustomField[0].Type", "StringType")
jsonReq:update_string("CustomField[0].Name", "Crew #")
jsonReq:update_string("Id", "239")
jsonReq:update_int("TxnTaxDetail.TotalTax", 0)
jsonReq:update_string("MetaData.CreateTime", "2015-07-24T10:35:08-07:00")
jsonReq:update_string("MetaData.LastUpdatedTime", "2015-07-24T10:35:08-07:00")
local sbRequestBody = chilkat.StringBuilder.new()
jsonReq:emit_sb(sbRequestBody)
rest:add_header("Content-Type", "application/json")
rest:add_header("Accept", "application/json")
rest.allow_header_folding = false
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/v3/company/<realmID>/invoice", sbRequestBody, sbResponseBody)
if not success then
print(rest.last_error_text)
return
end
local respStatusCode = rest.response_status_code
-- Success is indicated by a 200 response status code.
print("response status code = " .. respStatusCode)
local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load_sb(sbResponseBody)
jsonResponse.emit_compact = false
print(jsonResponse:emit())
if rest.response_status_code ~= 200 then
print("Failed.")
return
end
-- 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"
-- }
--
local LineNum = 0
local Amount = ""
local SalesItemLineDetailTaxCodeRefValue = ""
local SalesItemLineDetailItemRefName = ""
local SalesItemLineDetailItemRefValue = ""
local Id = ""
local DetailType = ""
local DefinitionId = ""
local invType = ""
local Name = ""
local InvoiceTxnDate = jsonResponse:string_of("Invoice.TxnDate")
local InvoiceDomain = jsonResponse:string_of("Invoice.domain")
local InvoicePrintStatus = jsonResponse:string_of("Invoice.PrintStatus")
local InvoiceTotalAmt = jsonResponse:string_of("Invoice.TotalAmt")
local InvoiceDueDate = jsonResponse:string_of("Invoice.DueDate")
local InvoiceApplyTaxAfterDiscount = jsonResponse:bool_of("Invoice.ApplyTaxAfterDiscount")
local InvoiceDocNumber = jsonResponse:string_of("Invoice.DocNumber")
local InvoiceSparse = jsonResponse:bool_of("Invoice.sparse")
local InvoiceCustomerMemoValue = jsonResponse:string_of("Invoice.CustomerMemo.value")
local InvoiceDeposit = jsonResponse:int_of("Invoice.Deposit")
local InvoiceBalance = jsonResponse:string_of("Invoice.Balance")
local InvoiceCustomerRefName = jsonResponse:string_of("Invoice.CustomerRef.name")
local InvoiceCustomerRefValue = jsonResponse:string_of("Invoice.CustomerRef.value")
local InvoiceTxnTaxDetailTotalTax = jsonResponse:int_of("Invoice.TxnTaxDetail.TotalTax")
local InvoiceSyncToken = jsonResponse:string_of("Invoice.SyncToken")
local InvoiceShipAddrCountrySubDivisionCode = jsonResponse:string_of("Invoice.ShipAddr.CountrySubDivisionCode")
local InvoiceShipAddrCity = jsonResponse:string_of("Invoice.ShipAddr.City")
local InvoiceShipAddrPostalCode = jsonResponse:string_of("Invoice.ShipAddr.PostalCode")
local InvoiceShipAddrId = jsonResponse:string_of("Invoice.ShipAddr.Id")
local InvoiceShipAddrLine1 = jsonResponse:string_of("Invoice.ShipAddr.Line1")
local InvoiceEmailStatus = jsonResponse:string_of("Invoice.EmailStatus")
local InvoiceBillAddrCountrySubDivisionCode = jsonResponse:string_of("Invoice.BillAddr.CountrySubDivisionCode")
local InvoiceBillAddrCity = jsonResponse:string_of("Invoice.BillAddr.City")
local InvoiceBillAddrPostalCode = jsonResponse:string_of("Invoice.BillAddr.PostalCode")
local InvoiceBillAddrId = jsonResponse:string_of("Invoice.BillAddr.Id")
local InvoiceBillAddrLine1 = jsonResponse:string_of("Invoice.BillAddr.Line1")
local InvoiceMetaDataCreateTime = jsonResponse:string_of("Invoice.MetaData.CreateTime")
local InvoiceMetaDataLastUpdatedTime = jsonResponse:string_of("Invoice.MetaData.LastUpdatedTime")
local InvoiceId = jsonResponse:string_of("Invoice.Id")
local time = jsonResponse:string_of("time")
local i = 0
local count_i = jsonResponse:size_of_array("Invoice.Line")
while i < count_i do
jsonResponse.i = i
LineNum = jsonResponse:int_of("Invoice.Line[i].LineNum")
Amount = jsonResponse:string_of("Invoice.Line[i].Amount")
SalesItemLineDetailTaxCodeRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value")
SalesItemLineDetailItemRefName = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.name")
SalesItemLineDetailItemRefValue = jsonResponse:string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.value")
Id = jsonResponse:string_of("Invoice.Line[i].Id")
DetailType = jsonResponse:string_of("Invoice.Line[i].DetailType")
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.LinkedTxn")
while i < count_i do
jsonResponse.i = i
i = i + 1
end
i = 0
count_i = jsonResponse:size_of_array("Invoice.CustomField")
while i < count_i do
jsonResponse.i = i
DefinitionId = jsonResponse:string_of("Invoice.CustomField[i].DefinitionId")
invType = jsonResponse:string_of("Invoice.CustomField[i].Type")
Name = jsonResponse:string_of("Invoice.CustomField[i].Name")
i = i + 1
end