Sample code for 30+ languages & platforms
Rust

Quickbooks Update an Invoice

See more QuickBooks Examples

Demonstrates how to update an invoice using the Quickbooks REST API.

Chilkat Rust Downloads

Rust

// 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.
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/qb-access-token.json").is_ok();

let rest = chilkat::Rest::new();

// Connect to the REST server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect).is_ok();

let sb_auth = chilkat::StringBuilder::new();
let _ = sb_auth.append("Bearer ");
let _ = sb_auth.append(&json_token.string_of("access_token").unwrap_or_default());
rest.set_authorization(&sb_auth.get_as_string().unwrap_or_default());

// --------------------------------------------------------------------------
// 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:

// {
//   "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"
//   },
//   "CustomerMemo": {
//     "value": "Added customer memo."
//   },
//   "ShipAddr": {
//     "City": "Bayshore",
//     "Line1": "4581 Finch St.",
//     "PostalCode": "94326",
//     "Lat": "INVALID",
//     "Long": "INVALID",
//     "CountrySubDivisionCode": "CA",
//     "Id": "109"
//   },
//   "LinkedTxn": [
//   ],
//   "DueDate": "2015-08-23",
//   "PrintStatus": "NeedToPrint",
//   "EmailStatus": "NotSet",
//   "sparse": false,
//   "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"
//   }
// }
// 
// Use the this online tool to generate the code from sample JSON: 
// Generate Code to Create JSON

let json_req = chilkat::JsonObject::new();
let _ = json_req.update_string("DocNumber", "1070");
let _ = json_req.update_string("SyncToken", "0");
let _ = json_req.update_string("domain", "QBO");
let _ = json_req.update_number("Balance", "150.0");
let _ = json_req.update_string("BillAddr.City", "Bayshore");
let _ = json_req.update_string("BillAddr.Line1", "4581 Finch St.");
let _ = json_req.update_string("BillAddr.PostalCode", "94326");
let _ = json_req.update_string("BillAddr.Lat", "INVALID");
let _ = json_req.update_string("BillAddr.Long", "INVALID");
let _ = json_req.update_string("BillAddr.CountrySubDivisionCode", "CA");
let _ = json_req.update_string("BillAddr.Id", "2");
let _ = json_req.update_string("TxnDate", "2015-07-24");
let _ = json_req.update_number("TotalAmt", "150.0");
let _ = json_req.update_string("CustomerRef.name", "Amy's Bird Sanctuary");
let _ = json_req.update_string("CustomerRef.value", "1");
let _ = json_req.update_string("CustomerMemo.value", "Added customer memo.");
let _ = json_req.update_string("ShipAddr.City", "Bayshore");
let _ = json_req.update_string("ShipAddr.Line1", "4581 Finch St.");
let _ = json_req.update_string("ShipAddr.PostalCode", "94326");
let _ = json_req.update_string("ShipAddr.Lat", "INVALID");
let _ = json_req.update_string("ShipAddr.Long", "INVALID");
let _ = json_req.update_string("ShipAddr.CountrySubDivisionCode", "CA");
let _ = json_req.update_string("ShipAddr.Id", "109");
let _ = json_req.update_new_array("LinkedTxn");
let _ = json_req.update_string("DueDate", "2015-08-23");
let _ = json_req.update_string("PrintStatus", "NeedToPrint");
let _ = json_req.update_string("EmailStatus", "NotSet");
let _ = json_req.update_bool("sparse", false);
let _ = json_req.update_int("Line[0].LineNum", 1);
let _ = json_req.update_number("Line[0].Amount", "150.0");
let _ = json_req.update_string("Line[0].SalesItemLineDetail.TaxCodeRef.value", "NON");
let _ = json_req.update_string("Line[0].SalesItemLineDetail.ItemRef.name", "Services");
let _ = json_req.update_string("Line[0].SalesItemLineDetail.ItemRef.value", "1");
let _ = json_req.update_string("Line[0].Id", "1");
let _ = json_req.update_string("Line[0].DetailType", "SalesItemLineDetail");
let _ = json_req.update_string("Line[1].DetailType", "SubTotalLineDetail");
let _ = json_req.update_number("Line[1].Amount", "150.0");
let _ = json_req.update_new_object("Line[1].SubTotalLineDetail");
let _ = json_req.update_bool("ApplyTaxAfterDiscount", false);
let _ = json_req.update_string("CustomField[0].DefinitionId", "1");
let _ = json_req.update_string("CustomField[0].Type", "StringType");
let _ = json_req.update_string("CustomField[0].Name", "Crew #");
let _ = json_req.update_string("Id", "239");
let _ = json_req.update_int("TxnTaxDetail.TotalTax", 0);
let _ = json_req.update_string("MetaData.CreateTime", "2015-07-24T10:35:08-07:00");
let _ = json_req.update_string("MetaData.LastUpdatedTime", "2015-07-24T10:35:08-07:00");

let sb_request_body = chilkat::StringBuilder::new();
let _ = json_req.emit_sb(&sb_request_body);

let _ = rest.add_header("Content-Type", "application/json");
let _ = rest.add_header("Accept", "application/json");
rest.set_allow_header_folding(false);

let sb_response_body = chilkat::StringBuilder::new();
if rest.full_request_sb("POST", "/v3/company/<realmID>/invoice", &sb_request_body, &sb_response_body).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let resp_status_code = rest.response_status_code();

// Success is indicated by a 200 response status code.
println!("response status code = {}", resp_status_code);

let json_response = chilkat::JsonObject::new();
let _ = json_response.load_sb(&sb_response_body);
json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

if rest.response_status_code() != 200 {
    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": {
//     "TxnDate": "2015-07-24",
//     "domain": "QBO",
//     "PrintStatus": "NeedToPrint",
//     "TotalAmt": 150.0,
//     "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": {}
//       }
//     ],
//     "DueDate": "2015-08-23",
//     "ApplyTaxAfterDiscount": false,
//     "DocNumber": "1070",
//     "sparse": false,
//     "CustomerMemo": {
//       "value": "Added customer memo."
//     },
//     "Deposit": 0,
//     "Balance": 150.0,
//     "CustomerRef": {
//       "name": "Amy's Bird Sanctuary",
//       "value": "1"
//     },
//     "TxnTaxDetail": {
//       "TotalTax": 0
//     },
//     "SyncToken": "1",
//     "LinkedTxn": [
//     ],
//     "ShipAddr": {
//       "CountrySubDivisionCode": "CA",
//       "City": "Bayshore",
//       "PostalCode": "94326",
//       "Id": "118",
//       "Line1": "4581 Finch St."
//     },
//     "EmailStatus": "NotSet",
//     "BillAddr": {
//       "CountrySubDivisionCode": "CA",
//       "City": "Bayshore",
//       "PostalCode": "94326",
//       "Id": "117",
//       "Line1": "4581 Finch St."
//     },
//     "MetaData": {
//       "CreateTime": "2015-07-24T10:35:08-07:00",
//       "LastUpdatedTime": "2015-07-24T10:53:39-07:00"
//     },
//     "CustomField": [
//       {
//         "DefinitionId": "1",
//         "Type": "StringType",
//         "Name": "Crew #"
//       }
//     ],
//     "Id": "239"
//   },
//   "time": "2015-07-24T10:53:39.287-07:00"
// }
// 

let invoice_txn_date = json_response.string_of("Invoice.TxnDate").unwrap_or_default();
let invoice_domain = json_response.string_of("Invoice.domain").unwrap_or_default();
let invoice_print_status = json_response.string_of("Invoice.PrintStatus").unwrap_or_default();
let invoice_total_amt = json_response.string_of("Invoice.TotalAmt").unwrap_or_default();
let invoice_due_date = json_response.string_of("Invoice.DueDate").unwrap_or_default();
let invoice_apply_tax_after_discount = json_response.bool_of("Invoice.ApplyTaxAfterDiscount");
let invoice_doc_number = json_response.string_of("Invoice.DocNumber").unwrap_or_default();
let invoice_sparse = json_response.bool_of("Invoice.sparse");
let invoice_customer_memo_value = json_response.string_of("Invoice.CustomerMemo.value").unwrap_or_default();
let invoice_deposit = json_response.int_of("Invoice.Deposit");
let invoice_balance = json_response.string_of("Invoice.Balance").unwrap_or_default();
let invoice_customer_ref_name = json_response.string_of("Invoice.CustomerRef.name").unwrap_or_default();
let invoice_customer_ref_value = json_response.string_of("Invoice.CustomerRef.value").unwrap_or_default();
let invoice_txn_tax_detail_total_tax = json_response.int_of("Invoice.TxnTaxDetail.TotalTax");
let invoice_sync_token = json_response.string_of("Invoice.SyncToken").unwrap_or_default();
let invoice_ship_addr_country_sub_division_code = json_response.string_of("Invoice.ShipAddr.CountrySubDivisionCode").unwrap_or_default();
let invoice_ship_addr_city = json_response.string_of("Invoice.ShipAddr.City").unwrap_or_default();
let invoice_ship_addr_postal_code = json_response.string_of("Invoice.ShipAddr.PostalCode").unwrap_or_default();
let invoice_ship_addr_id = json_response.string_of("Invoice.ShipAddr.Id").unwrap_or_default();
let invoice_ship_addr_line1 = json_response.string_of("Invoice.ShipAddr.Line1").unwrap_or_default();
let invoice_email_status = json_response.string_of("Invoice.EmailStatus").unwrap_or_default();
let invoice_bill_addr_country_sub_division_code = json_response.string_of("Invoice.BillAddr.CountrySubDivisionCode").unwrap_or_default();
let invoice_bill_addr_city = json_response.string_of("Invoice.BillAddr.City").unwrap_or_default();
let invoice_bill_addr_postal_code = json_response.string_of("Invoice.BillAddr.PostalCode").unwrap_or_default();
let invoice_bill_addr_id = json_response.string_of("Invoice.BillAddr.Id").unwrap_or_default();
let invoice_bill_addr_line1 = json_response.string_of("Invoice.BillAddr.Line1").unwrap_or_default();
let invoice_meta_data_create_time = json_response.string_of("Invoice.MetaData.CreateTime").unwrap_or_default();
let invoice_meta_data_last_updated_time = json_response.string_of("Invoice.MetaData.LastUpdatedTime").unwrap_or_default();
let invoice_id = json_response.string_of("Invoice.Id").unwrap_or_default();
let time = json_response.string_of("time").unwrap_or_default();
let mut i = 0;
let mut count_i = json_response.size_of_array("Invoice.Line");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.int_of("Invoice.Line[i].LineNum");
    let _ = json_response.string_of("Invoice.Line[i].Amount").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.TaxCodeRef.value").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.name").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].SalesItemLineDetail.ItemRef.value").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].Id").unwrap_or_default();
    let _ = json_response.string_of("Invoice.Line[i].DetailType").unwrap_or_default();
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("Invoice.LinkedTxn");
while i < count_i {
    json_response.set_i(i);
    i = i + 1;
}

i = 0;
count_i = json_response.size_of_array("Invoice.CustomField");
while i < count_i {
    json_response.set_i(i);
    let _ = json_response.string_of("Invoice.CustomField[i].DefinitionId").unwrap_or_default();
    let _ = json_response.string_of("Invoice.CustomField[i].Type").unwrap_or_default();
    let _ = json_response.string_of("Invoice.CustomField[i].Name").unwrap_or_default();
    i = i + 1;
}