Sample code for 30+ languages & platforms
Dart

Quickbooks Query an Invoice

See more QuickBooks Examples

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

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final jsonToken = CkJsonObject();
  jsonToken.loadFile('qa_data/tokens/qb-access-token.json');

  final rest = CkRest();

  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  rest.connect('sandbox-quickbooks.api.intuit.com', port, bTls, bAutoReconnect);

  final sbAuth = CkStringBuilder();
  sbAuth.append('Bearer ');
  sbAuth.append(jsonToken.stringOf('access_token'));
  rest.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.allowHeaderFolding = false;

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

  final sbResponseBody = CkStringBuilder();
  try {
    rest.fullRequestNoBodySb('GET', '/v3/company/<realmID>/invoice', sbResponseBody);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final respStatusCode = rest.responseStatusCode;
  if (respStatusCode >= 400) {
    print('Response Status Code = $respStatusCode');
    print('Response Header:');
    print(rest.responseHeader);
    print('Response Body:');
    print(sbResponseBody.getAsString());
    return;
  }

  // Success is indicated by a 200 response status.
  print('response status code = $respStatusCode');

  final json = CkJsonObject();
  json.loadSb(sbResponseBody);
  json.emitCompact = 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"
  // }

  var docNumber = '';
  var syncToken = '';
  var domain = '';
  var balance = '';
  var billAddrCity = '';
  var billAddrLine1 = '';
  var billAddrPostalCode = '';
  var billAddrLat = '';
  var billAddrLong = '';
  var billAddrCountrySubDivisionCode = '';
  var billAddrId = '';
  var txnDate = '';
  var totalAmt = '';
  var customerRefName = '';
  var customerRefValue = '';
  var shipAddrCity = '';
  var shipAddrLine1 = '';
  var shipAddrPostalCode = '';
  var shipAddrLat = '';
  var shipAddrLong = '';
  var shipAddrCountrySubDivisionCode = '';
  var shipAddrId = '';
  var dueDate = '';
  var printStatus = '';
  var deposit = 0;
  var emailStatus = '';
  var id = '';
  var txnTaxDetailTotalTax = 0;
  var metaDataCreateTime = '';
  var metaDataLastUpdatedTime = '';
  var j = 0;
  var countJ = 0;
  var lineNum = 0;
  var amount = '';
  var salesItemLineDetailTaxCodeRefValue = '';
  var salesItemLineDetailItemRefName = '';
  var salesItemLineDetailItemRefValue = '';
  var detailType = '';
  var definitionId = '';
  var invType = '';
  var name = '';

  final queryResponseStartPosition = json.intOf('QueryResponse.startPosition');
  final queryResponseTotalCount = json.intOf('QueryResponse.totalCount');
  final queryResponseMaxResults = json.intOf('QueryResponse.maxResults');
  final time = json.stringOf('time');
  var i = 0;
  final countI = json.sizeOfArray('QueryResponse.Invoice');
  while (i < countI) {
    json.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');
    json.boolOf('QueryResponse.Invoice[i].sparse');
    emailStatus = json.stringOf('QueryResponse.Invoice[i].EmailStatus');
    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;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].LinkedTxn');
    while (j < countJ) {
      json.j = j;
      j++;
    }

    j = 0;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].Line');
    while (j < countJ) {
      json.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 = 0;
    countJ = json.sizeOfArray('QueryResponse.Invoice[i].CustomField');
    while (j < countJ) {
      json.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++;
    }

    i++;
  }
}