Sample code for 30+ languages & platforms
Objective-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 Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <CkoRest.h>
#import <CkoStringBuilder.h>
#import <NSString.h>

BOOL success = NO;

// 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.
CkoJsonObject *jsonToken = [[CkoJsonObject alloc] init];
success = [jsonToken LoadFile: @"qa_data/tokens/qb-access-token.json"];

CkoRest *rest = [[CkoRest alloc] init];

BOOL bTls = YES;
int port = 443;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"sandbox-quickbooks.api.intuit.com" port: [NSNumber numberWithInt: port] tls: bTls autoReconnect: bAutoReconnect];

CkoStringBuilder *sbAuth = [[CkoStringBuilder alloc] init];
[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" value: @"application/json"];
rest.AllowHeaderFolding = NO;

// Add a SELECT statement 
[rest AddQueryParam: @"query" value: @"select * from Invoice where id = '239'"];

CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/v3/company/<realmID>/invoice" sb: sbResponseBody];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

int respStatusCode = [rest.ResponseStatusCode intValue];
if (respStatusCode >= 400) {
    NSLog(@"%@%d",@"Response Status Code = ",respStatusCode);
    NSLog(@"%@",@"Response Header:");
    NSLog(@"%@",rest.ResponseHeader);
    NSLog(@"%@",@"Response Body:");
    NSLog(@"%@",[sbResponseBody GetAsString]);
    return;
}

// Success is indicated by a 200 response status.
NSLog(@"%@%d",@"response status code = ",respStatusCode);

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json LoadSb: sbResponseBody];
json.EmitCompact = NO;
NSLog(@"%@",[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"
// }

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

int QueryResponseStartPosition = [[json IntOf: @"QueryResponse.startPosition"] intValue];
int QueryResponseTotalCount = [[json IntOf: @"QueryResponse.totalCount"] intValue];
int QueryResponseMaxResults = [[json IntOf: @"QueryResponse.maxResults"] intValue];
NSString *time = [json StringOf: @"time"];
int i = 0;
int count_i = [[json SizeOfArray: @"QueryResponse.Invoice"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: 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"] intValue];
    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"] intValue];
    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"] intValue];
    while (j < count_j) {
        json.J = [NSNumber numberWithInt: j];
        j = j + 1;
    }

    j = 0;
    count_j = [[json SizeOfArray: @"QueryResponse.Invoice[i].Line"] intValue];
    while (j < count_j) {
        json.J = [NSNumber numberWithInt: j];
        LineNum = [[json IntOf: @"QueryResponse.Invoice[i].Line[j].LineNum"] intValue];
        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"] intValue];
    while (j < count_j) {
        json.J = [NSNumber numberWithInt: 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;
}