Sample code for 30+ languages & platforms
Java

Quickbooks Query an Invoice

See more QuickBooks Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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.
        JsonObject jsonToken = new JsonObject();
        jsonToken.loadFile("qa_data/tokens/qb-access-token.json");

        Rest rest = new Rest();

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

        StringBuilder sbAuth = new StringBuilder();
        sbAuth.append("Bearer ");
        sbAuth.append(jsonToken.stringOf("access_token"));
        rest.setAuthorization(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.setAllowHeaderFolding(false);

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

        StringBuilder sbResponseBody = new StringBuilder();
        try {
            rest.fullRequestNoBodySb("GET", "/v3/company/<realmID>/invoice", sbResponseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int respStatusCode = rest.getResponseStatusCode();
        if (respStatusCode >= 400) {
            System.out.println("Response Status Code = " + respStatusCode);
            System.out.println("Response Header:");
            System.out.println(rest.getResponseHeader());
            System.out.println("Response Body:");
            System.out.println(sbResponseBody.getAsString());
            return;
        }

        // Success is indicated by a 200 response status.
        System.out.println("response status code = " + respStatusCode);

        JsonObject json = new JsonObject();
        json.loadSb(sbResponseBody);
        json.setEmitCompact(false);
        System.out.println(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"
        // }

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

        int QueryResponseStartPosition = json.intOf("QueryResponse.startPosition");
        int QueryResponseTotalCount = json.intOf("QueryResponse.totalCount");
        int QueryResponseMaxResults = json.intOf("QueryResponse.maxResults");
        String time = json.stringOf("time");
        int i = 0;
        int count_i = json.sizeOfArray("QueryResponse.Invoice");
        while (i < count_i) {
            json.setI(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.setJ(j);
                j = j + 1;
            }

            j = 0;
            count_j = json.sizeOfArray("QueryResponse.Invoice[i].Line");
            while (j < count_j) {
                json.setJ(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.setJ(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;
        }
    }
}