Sample code for 30+ languages & platforms
Delphi DLL

Quickbooks Query an Invoice

See more QuickBooks Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Rest, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
jsonToken: HCkJsonObject;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
sbAuth: HCkStringBuilder;
sbResponseBody: HCkStringBuilder;
respStatusCode: Integer;
json: HCkJsonObject;
DocNumber: PWideChar;
SyncToken: PWideChar;
domain: PWideChar;
Balance: PWideChar;
BillAddrCity: PWideChar;
BillAddrLine1: PWideChar;
BillAddrPostalCode: PWideChar;
BillAddrLat: PWideChar;
BillAddrLong: PWideChar;
BillAddrCountrySubDivisionCode: PWideChar;
BillAddrId: PWideChar;
TxnDate: PWideChar;
TotalAmt: PWideChar;
CustomerRefName: PWideChar;
CustomerRefValue: PWideChar;
ShipAddrCity: PWideChar;
ShipAddrLine1: PWideChar;
ShipAddrPostalCode: PWideChar;
ShipAddrLat: PWideChar;
ShipAddrLong: PWideChar;
ShipAddrCountrySubDivisionCode: PWideChar;
ShipAddrId: PWideChar;
DueDate: PWideChar;
PrintStatus: PWideChar;
Deposit: Integer;
sparse: Boolean;
EmailStatus: PWideChar;
ApplyTaxAfterDiscount: Boolean;
Id: PWideChar;
TxnTaxDetailTotalTax: Integer;
MetaDataCreateTime: PWideChar;
MetaDataLastUpdatedTime: PWideChar;
j: Integer;
count_j: Integer;
LineNum: Integer;
Amount: PWideChar;
SalesItemLineDetailTaxCodeRefValue: PWideChar;
SalesItemLineDetailItemRefName: PWideChar;
SalesItemLineDetailItemRefValue: PWideChar;
DetailType: PWideChar;
DefinitionId: PWideChar;
invType: PWideChar;
Name: PWideChar;
QueryResponseStartPosition: Integer;
QueryResponseTotalCount: Integer;
QueryResponseMaxResults: Integer;
time: PWideChar;
i: Integer;
count_i: Integer;

begin
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.
jsonToken := CkJsonObject_Create();
success := CkJsonObject_LoadFile(jsonToken,'qa_data/tokens/qb-access-token.json');

rest := CkRest_Create();

bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'sandbox-quickbooks.api.intuit.com',port,bTls,bAutoReconnect);

sbAuth := CkStringBuilder_Create();
CkStringBuilder_Append(sbAuth,'Bearer ');
CkStringBuilder_Append(sbAuth,CkJsonObject__stringOf(jsonToken,'access_token'));
CkRest_putAuthorization(rest,CkStringBuilder__getAsString(sbAuth));

// --------------------------------------------------------------------------
// 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..
CkRest_RemoveHeader(rest,'Content-Type');

CkRest_AddHeader(rest,'Accept','application/json');
CkRest_putAllowHeaderFolding(rest,False);

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

sbResponseBody := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/v3/company/<realmID>/invoice',sbResponseBody);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

respStatusCode := CkRest_getResponseStatusCode(rest);
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkRest__responseHeader(rest));
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponseBody));
    Exit;
  end;

// Success is indicated by a 200 response status.
Memo1.Lines.Add('response status code = ' + IntToStr(respStatusCode));

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbResponseBody);
CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(CkJsonObject__emit(json));

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

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

    j := 0;
    count_j := CkJsonObject_SizeOfArray(json,'QueryResponse.Invoice[i].Line');
    while j < count_j do
      begin
        CkJsonObject_putJ(json,j);
        LineNum := CkJsonObject_IntOf(json,'QueryResponse.Invoice[i].Line[j].LineNum');
        Amount := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].Amount');
        SalesItemLineDetailTaxCodeRefValue := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value');
        SalesItemLineDetailItemRefName := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name');
        SalesItemLineDetailItemRefValue := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value');
        Id := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].Id');
        DetailType := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].Line[j].DetailType');
        j := j + 1;
      end;

    j := 0;
    count_j := CkJsonObject_SizeOfArray(json,'QueryResponse.Invoice[i].CustomField');
    while j < count_j do
      begin
        CkJsonObject_putJ(json,j);
        DefinitionId := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].CustomField[j].DefinitionId');
        invType := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].CustomField[j].Type');
        Name := CkJsonObject__stringOf(json,'QueryResponse.Invoice[i].CustomField[j].Name');
        j := j + 1;
      end;

    i := i + 1;
  end;

CkJsonObject_Dispose(jsonToken);
CkRest_Dispose(rest);
CkStringBuilder_Dispose(sbAuth);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(json);

end;