Sample code for 30+ languages & platforms
Lua

Quickbooks Query an Invoice

See more QuickBooks Examples

Demonstrates how to query for invoices matching a SELECT statement via the Quickbooks REST API.

Chilkat Lua Downloads

Lua
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()

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 REST connection and setup of the AWS credentials
-- can be done once.  After connecting, any number of REST calls can be made.
-- The "auto reconnect" property passed to rest.Connect indicates that if
-- the connection is lost, a REST method call will automatically reconnect
-- if needed.
-- --------------------------------------------------------------------------

-- This is a GET request, so there should be no Content-Type
-- This line of code is just to make sure..
rest:remove_header("Content-Type")

rest:add_header("Accept", "application/json")
rest.allow_header_folding = false

-- Add a SELECT statement
rest:add_query_param("query", "select * from Invoice where id = '239'")

local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_no_body_sb("GET", "/v3/company/<realmID>/invoice", sbResponseBody)
if not success then
    print(rest.last_error_text)
    return
end

local respStatusCode = rest.response_status_code
if respStatusCode >= 400 then
    print("Response Status Code = " .. respStatusCode)
    print("Response Header:")
    print(rest.response_header)
    print("Response Body:")
    print(sbResponseBody:get_as_string())
    return
end

-- Success is indicated by a 200 response status.
print("response status code = " .. respStatusCode)

local json = chilkat.JsonObject.new()
json:load_sb(sbResponseBody)
json.emit_compact = false
print(json:emit())

-- Sample output:
-- Use the this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON

-- {
--   "QueryResponse": {
--     "startPosition": 1,
--     "totalCount": 1,
--     "maxResults": 1,
--     "Invoice": [
--       {
--         "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"
--         },
--         "ShipAddr": {
--           "City": "Bayshore",
--           "Line1": "4581 Finch St.",
--           "PostalCode": "94326",
--           "Lat": "INVALID",
--           "Long": "INVALID",
--           "CountrySubDivisionCode": "CA",
--           "Id": "109"
--         },
--         "LinkedTxn": [],
--         "DueDate": "2015-08-23",
--         "PrintStatus": "NeedToPrint",
--         "Deposit": 0,
--         "sparse": false,
--         "EmailStatus": "NotSet",
--         "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"
--         }
--       }
--     ]
--   },
--   "time": "2015-07-24T10:38:50.01-07:00"
-- }

local DocNumber = ""
local SyncToken = ""
local domain = ""
local Balance = ""
local BillAddrCity = ""
local BillAddrLine1 = ""
local BillAddrPostalCode = ""
local BillAddrLat = ""
local BillAddrLong = ""
local BillAddrCountrySubDivisionCode = ""
local BillAddrId = ""
local TxnDate = ""
local TotalAmt = ""
local CustomerRefName = ""
local CustomerRefValue = ""
local ShipAddrCity = ""
local ShipAddrLine1 = ""
local ShipAddrPostalCode = ""
local ShipAddrLat = ""
local ShipAddrLong = ""
local ShipAddrCountrySubDivisionCode = ""
local ShipAddrId = ""
local DueDate = ""
local PrintStatus = ""
local Deposit = 0
local sparse = false
local EmailStatus = ""
local ApplyTaxAfterDiscount = false
local Id = ""
local TxnTaxDetailTotalTax = 0
local MetaDataCreateTime = ""
local MetaDataLastUpdatedTime = ""
local j = 0
local count_j = 0
local LineNum = 0
local Amount = ""
local SalesItemLineDetailTaxCodeRefValue = ""
local SalesItemLineDetailItemRefName = ""
local SalesItemLineDetailItemRefValue = ""
local DetailType = ""
local DefinitionId = ""
local invType = ""
local Name = ""

local QueryResponseStartPosition = json:int_of("QueryResponse.startPosition")
local QueryResponseTotalCount = json:int_of("QueryResponse.totalCount")
local QueryResponseMaxResults = json:int_of("QueryResponse.maxResults")
local time = json:string_of("time")
local i = 0
local count_i = json:size_of_array("QueryResponse.Invoice")
while i < count_i do
    json.i = i
    DocNumber = json:string_of("QueryResponse.Invoice[i].DocNumber")
    SyncToken = json:string_of("QueryResponse.Invoice[i].SyncToken")
    domain = json:string_of("QueryResponse.Invoice[i].domain")
    Balance = json:string_of("QueryResponse.Invoice[i].Balance")
    BillAddrCity = json:string_of("QueryResponse.Invoice[i].BillAddr.City")
    BillAddrLine1 = json:string_of("QueryResponse.Invoice[i].BillAddr.Line1")
    BillAddrPostalCode = json:string_of("QueryResponse.Invoice[i].BillAddr.PostalCode")
    BillAddrLat = json:string_of("QueryResponse.Invoice[i].BillAddr.Lat")
    BillAddrLong = json:string_of("QueryResponse.Invoice[i].BillAddr.Long")
    BillAddrCountrySubDivisionCode = json:string_of("QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode")
    BillAddrId = json:string_of("QueryResponse.Invoice[i].BillAddr.Id")
    TxnDate = json:string_of("QueryResponse.Invoice[i].TxnDate")
    TotalAmt = json:string_of("QueryResponse.Invoice[i].TotalAmt")
    CustomerRefName = json:string_of("QueryResponse.Invoice[i].CustomerRef.name")
    CustomerRefValue = json:string_of("QueryResponse.Invoice[i].CustomerRef.value")
    ShipAddrCity = json:string_of("QueryResponse.Invoice[i].ShipAddr.City")
    ShipAddrLine1 = json:string_of("QueryResponse.Invoice[i].ShipAddr.Line1")
    ShipAddrPostalCode = json:string_of("QueryResponse.Invoice[i].ShipAddr.PostalCode")
    ShipAddrLat = json:string_of("QueryResponse.Invoice[i].ShipAddr.Lat")
    ShipAddrLong = json:string_of("QueryResponse.Invoice[i].ShipAddr.Long")
    ShipAddrCountrySubDivisionCode = json:string_of("QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode")
    ShipAddrId = json:string_of("QueryResponse.Invoice[i].ShipAddr.Id")
    DueDate = json:string_of("QueryResponse.Invoice[i].DueDate")
    PrintStatus = json:string_of("QueryResponse.Invoice[i].PrintStatus")
    Deposit = json:int_of("QueryResponse.Invoice[i].Deposit")
    sparse = json:bool_of("QueryResponse.Invoice[i].sparse")
    EmailStatus = json:string_of("QueryResponse.Invoice[i].EmailStatus")
    ApplyTaxAfterDiscount = json:bool_of("QueryResponse.Invoice[i].ApplyTaxAfterDiscount")
    Id = json:string_of("QueryResponse.Invoice[i].Id")
    TxnTaxDetailTotalTax = json:int_of("QueryResponse.Invoice[i].TxnTaxDetail.TotalTax")
    MetaDataCreateTime = json:string_of("QueryResponse.Invoice[i].MetaData.CreateTime")
    MetaDataLastUpdatedTime = json:string_of("QueryResponse.Invoice[i].MetaData.LastUpdatedTime")
    j = 0
    count_j = json:size_of_array("QueryResponse.Invoice[i].LinkedTxn")
    while j < count_j do
        json.j = j
        j = j + 1
    end

    j = 0
    count_j = json:size_of_array("QueryResponse.Invoice[i].Line")
    while j < count_j do
        json.j = j
        LineNum = json:int_of("QueryResponse.Invoice[i].Line[j].LineNum")
        Amount = json:string_of("QueryResponse.Invoice[i].Line[j].Amount")
        SalesItemLineDetailTaxCodeRefValue = json:string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value")
        SalesItemLineDetailItemRefName = json:string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name")
        SalesItemLineDetailItemRefValue = json:string_of("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value")
        Id = json:string_of("QueryResponse.Invoice[i].Line[j].Id")
        DetailType = json:string_of("QueryResponse.Invoice[i].Line[j].DetailType")
        j = j + 1
    end

    j = 0
    count_j = json:size_of_array("QueryResponse.Invoice[i].CustomField")
    while j < count_j do
        json.j = j
        DefinitionId = json:string_of("QueryResponse.Invoice[i].CustomField[j].DefinitionId")
        invType = json:string_of("QueryResponse.Invoice[i].CustomField[j].Type")
        Name = json:string_of("QueryResponse.Invoice[i].CustomField[j].Name")
        j = j + 1
    end

    i = i + 1
end