Sample code for 30+ languages & platforms
Java

Quickbooks Delete an Invoice

See more QuickBooks Examples

Demonstrates how to delete an invoice using 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();

        // Connect to the REST server.
        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 code to setup the initial REST connection
        // can be done once.  After connecting, any number of REST calls can be made.
        // If the connection is lost, the next REST method call will automatically
        // reconnect if needed.
        // --------------------------------------------------------------------------

        // Create the following JSON:

        // {
        //   "SyncToken": "3",
        //   "Id": "33"
        // }
        //
        // Use the this online tool to generate the code from sample JSON:
        // Generate Code to Create JSON

        JsonObject jsonReq = new JsonObject();
        jsonReq.updateString("SyncToken", "3");
        jsonReq.updateString("Id", "33");

        StringBuilder sbRequestBody = new StringBuilder();
        jsonReq.emitSb(sbRequestBody);

        rest.addHeader("Content-Type", "application/json");
        rest.addHeader("Accept", "application/json");
        rest.setAllowHeaderFolding(false);

        StringBuilder sbResponseBody = new StringBuilder();
        try {
            rest.fullRequestSb("POST", "/v3/company/<realmID>/invoice?operation=delete", sbRequestBody, sbResponseBody);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int respStatusCode = rest.getResponseStatusCode();

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

        JsonObject jsonResponse = new JsonObject();
        jsonResponse.loadSb(sbResponseBody);
        jsonResponse.setEmitCompact(false);
        System.out.println(jsonResponse.emit());

        if (rest.getResponseStatusCode() != 200) {
            System.out.println("Failed.");
            return;
        }

        // Sample output...
        // (See the parsing code below..)
        //
        // Use the this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        // {
        //   "Invoice": {
        //     "status": "Deleted",
        //     "domain": "QBO",
        //     "Id": "33"
        //   },
        //   "time": "2013-03-15T00:18:15.322-07:00"
        // }
        //

        String InvoiceStatus = jsonResponse.stringOf("Invoice.status");
        String InvoiceDomain = jsonResponse.stringOf("Invoice.domain");
        String InvoiceId = jsonResponse.stringOf("Invoice.Id");
        String time = jsonResponse.stringOf("time");
    }
}