Sample code for 30+ languages & platforms
C++

Quickbooks Query an Invoice

See more QuickBooks Examples

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

Chilkat C++ Downloads

C++
#include <CkJsonObject.h>
#include <CkRest.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  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.
    CkJsonObject jsonToken;
    success = jsonToken.LoadFile("qa_data/tokens/qb-access-token.json");

    CkRest rest;

    bool bTls = true;
    int port = 443;
    bool bAutoReconnect = true;
    success = rest.Connect("sandbox-quickbooks.api.intuit.com",port,bTls,bAutoReconnect);

    CkStringBuilder sbAuth;
    sbAuth.Append("Bearer ");
    sbAuth.Append(jsonToken.stringOf("access_token"));
    rest.put_Authorization(sbAuth.getAsString());

    //  --------------------------------------------------------------------------
    //  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.RemoveHeader("Content-Type");

    rest.AddHeader("Accept","application/json");
    rest.put_AllowHeaderFolding(false);

    //  Add a SELECT statement 
    rest.AddQueryParam("query","select * from Invoice where id = '239'");

    CkStringBuilder sbResponseBody;
    success = rest.FullRequestNoBodySb("GET","/v3/company/<realmID>/invoice",sbResponseBody);
    if (success != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    if (respStatusCode >= 400) {
        std::cout << "Response Status Code = " << respStatusCode << "\r\n";
        std::cout << "Response Header:" << "\r\n";
        std::cout << rest.responseHeader() << "\r\n";
        std::cout << "Response Body:" << "\r\n";
        std::cout << sbResponseBody.getAsString() << "\r\n";
        return;
    }

    //  Success is indicated by a 200 response status.
    std::cout << "response status code = " << respStatusCode << "\r\n";

    CkJsonObject json;
    json.LoadSb(sbResponseBody);
    json.put_EmitCompact(false);
    std::cout << json.emit() << "\r\n";

    //  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"
    //  }

    //  Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    //  See this example explaining how this memory should be used: const char * functions.

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

    int QueryResponseStartPosition = json.IntOf("QueryResponse.startPosition");
    int QueryResponseTotalCount = json.IntOf("QueryResponse.totalCount");
    int QueryResponseMaxResults = json.IntOf("QueryResponse.maxResults");
    const char *time = json.stringOf("time");
    int i = 0;
    int count_i = json.SizeOfArray("QueryResponse.Invoice");
    while (i < count_i) {
        json.put_I(i);
        DocNumber = json.stringOf("QueryResponse.Invoice[i].DocNumber");
        SyncToken = json.stringOf("QueryResponse.Invoice[i].SyncToken");
        domain = json.stringOf("QueryResponse.Invoice[i].domain");
        Balance = json.stringOf("QueryResponse.Invoice[i].Balance");
        BillAddrCity = json.stringOf("QueryResponse.Invoice[i].BillAddr.City");
        BillAddrLine1 = json.stringOf("QueryResponse.Invoice[i].BillAddr.Line1");
        BillAddrPostalCode = json.stringOf("QueryResponse.Invoice[i].BillAddr.PostalCode");
        BillAddrLat = json.stringOf("QueryResponse.Invoice[i].BillAddr.Lat");
        BillAddrLong = json.stringOf("QueryResponse.Invoice[i].BillAddr.Long");
        BillAddrCountrySubDivisionCode = json.stringOf("QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode");
        BillAddrId = json.stringOf("QueryResponse.Invoice[i].BillAddr.Id");
        TxnDate = json.stringOf("QueryResponse.Invoice[i].TxnDate");
        TotalAmt = json.stringOf("QueryResponse.Invoice[i].TotalAmt");
        CustomerRefName = json.stringOf("QueryResponse.Invoice[i].CustomerRef.name");
        CustomerRefValue = json.stringOf("QueryResponse.Invoice[i].CustomerRef.value");
        ShipAddrCity = json.stringOf("QueryResponse.Invoice[i].ShipAddr.City");
        ShipAddrLine1 = json.stringOf("QueryResponse.Invoice[i].ShipAddr.Line1");
        ShipAddrPostalCode = json.stringOf("QueryResponse.Invoice[i].ShipAddr.PostalCode");
        ShipAddrLat = json.stringOf("QueryResponse.Invoice[i].ShipAddr.Lat");
        ShipAddrLong = json.stringOf("QueryResponse.Invoice[i].ShipAddr.Long");
        ShipAddrCountrySubDivisionCode = json.stringOf("QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode");
        ShipAddrId = json.stringOf("QueryResponse.Invoice[i].ShipAddr.Id");
        DueDate = json.stringOf("QueryResponse.Invoice[i].DueDate");
        PrintStatus = json.stringOf("QueryResponse.Invoice[i].PrintStatus");
        Deposit = json.IntOf("QueryResponse.Invoice[i].Deposit");
        sparse = json.BoolOf("QueryResponse.Invoice[i].sparse");
        EmailStatus = json.stringOf("QueryResponse.Invoice[i].EmailStatus");
        ApplyTaxAfterDiscount = json.BoolOf("QueryResponse.Invoice[i].ApplyTaxAfterDiscount");
        Id = json.stringOf("QueryResponse.Invoice[i].Id");
        TxnTaxDetailTotalTax = json.IntOf("QueryResponse.Invoice[i].TxnTaxDetail.TotalTax");
        MetaDataCreateTime = json.stringOf("QueryResponse.Invoice[i].MetaData.CreateTime");
        MetaDataLastUpdatedTime = json.stringOf("QueryResponse.Invoice[i].MetaData.LastUpdatedTime");
        j = 0;
        count_j = json.SizeOfArray("QueryResponse.Invoice[i].LinkedTxn");
        while (j < count_j) {
            json.put_J(j);
            j = j + 1;
        }

        j = 0;
        count_j = json.SizeOfArray("QueryResponse.Invoice[i].Line");
        while (j < count_j) {
            json.put_J(j);
            LineNum = json.IntOf("QueryResponse.Invoice[i].Line[j].LineNum");
            Amount = json.stringOf("QueryResponse.Invoice[i].Line[j].Amount");
            SalesItemLineDetailTaxCodeRefValue = json.stringOf("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value");
            SalesItemLineDetailItemRefName = json.stringOf("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name");
            SalesItemLineDetailItemRefValue = json.stringOf("QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value");
            Id = json.stringOf("QueryResponse.Invoice[i].Line[j].Id");
            DetailType = json.stringOf("QueryResponse.Invoice[i].Line[j].DetailType");
            j = j + 1;
        }

        j = 0;
        count_j = json.SizeOfArray("QueryResponse.Invoice[i].CustomField");
        while (j < count_j) {
            json.put_J(j);
            DefinitionId = json.stringOf("QueryResponse.Invoice[i].CustomField[j].DefinitionId");
            invType = json.stringOf("QueryResponse.Invoice[i].CustomField[j].Type");
            Name = json.stringOf("QueryResponse.Invoice[i].CustomField[j].Name");
            j = j + 1;
        }

        i = i + 1;
    }
    }