Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3/4 Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Node.js) QuickBooks - Read an Employee (with Error Response)Demonstrates how to fetch the information for a specific Quickbooks employee, but tries to fetch an inactive employee. Shows the error response and how to parse it.
var os = require('os'); if (os.platform() == 'win32') { if (os.arch() == 'ia32') { var chilkat = require('@chilkat/ck-node11-win-ia32'); } else { var chilkat = require('@chilkat/ck-node11-win64'); } } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node11-arm'); } else if (os.arch() == 'x86') { var chilkat = require('@chilkat/ck-node11-linux32'); } else { var chilkat = require('@chilkat/ck-node11-linux64'); } } else if (os.platform() == 'darwin') { var chilkat = require('@chilkat/ck-node11-macosx'); } function chilkatExample() { // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // This example also assumes an OAuth1 access token was previously fetched. // and saved in a JSON file. See the Chilkat online examples at example-code.com // for Quickbooks OAuth1 examples (in the Quickbooks and OAuth1 categories) // // First get our previously obtained access token. var jsonToken = new chilkat.JsonObject(); var success = jsonToken.LoadFile("qa_data/tokens/quickbooks.json"); var oauth1 = new chilkat.OAuth1(); oauth1.ConsumerKey = "QUICKBOOKS_CONSUMER_KEY"; oauth1.ConsumerSecret = "QUICKBOOKS_CONSUMER_SECRET"; oauth1.Token = jsonToken.StringOf("oauth_token"); oauth1.TokenSecret = jsonToken.StringOf("oauth_token_secret"); var rest = new chilkat.Rest(); // Connect using TLS. // A single REST object, once connected, can be used for many Quickbooks REST API calls. // The auto-reconnect indicates that if the already-established HTTPS connection is closed, // then it will be automatically re-established as needed. var bAutoReconnect = true; success = rest.Connect("sandbox-quickbooks.api.intuit.com",443,true,bAutoReconnect); if (success !== true) { console.log(rest.LastErrorText); return; } rest.SetAuthOAuth1(oauth1,false); // The company ID is the realmId var sbPath = new chilkat.StringBuilder(); sbPath.Append("/v3/company/<companyID>/employee/<employeeID>"); var numReplacements = sbPath.Replace("<companyID>",jsonToken.StringOf("realmId")); // Assume we already know that we want to read the employee whose Id = 98. // We know this employee was made inactive, and we should get an error response. numReplacements = sbPath.Replace("<employeeID>","98"); rest.AddHeader("Accept","application/json"); rest.AllowHeaderFolding = false; var responseBody = rest.FullRequestNoBody("GET",sbPath.GetAsString()); if (rest.LastMethodSuccess !== true) { console.log(rest.LastErrorText); return; } // Load the JSON response into a JSON object for parsing. // A sample JSON response is shown below. var json = new chilkat.JsonObject(); json.Load(responseBody); json.EmitCompact = false; console.log(json.Emit()); // A 400 response is what we'd expect if trying to query an inactive employee... if (rest.ResponseStatusCode !== 400) { console.log("Request Header: "); console.log(rest.LastRequestHeader); console.log("----"); console.log("Response StatusCode = " + rest.ResponseStatusCode); console.log("Response StatusLine: " + rest.ResponseStatusText); console.log("Response Header:"); console.log(rest.ResponseHeader); return; } // If we got here, the response status code was 400.. // Iterate over the errors.. var numErrors = json.SizeOfArray("Fault.Error"); var i = 0; while (i < numErrors) { json.I = i; console.log("Message: " + json.StringOf("Fault.Error[i].Message")); console.log("Detail: " + json.StringOf("Fault.Error[i].Detail")); console.log("code: " + json.StringOf("Fault.Error[i].code")); console.log("----"); i = i+1; } // ------------------------------------------------------ // The JSON error response looks like this: // { // "Fault": { // "Error": [ // { // "Message": "Object Not Found", // "Detail": "Object Not Found : Something you're trying to use has been made inactive. Check the fields with accounts, customers, items, vendors or employees.", // "code": "610", // "element": "" // } // ], // "type": "ValidationFault" // }, // "time": "2016-10-25T14:56:37.335-07:00" // } // } chilkatExample(); |
© 2000-2016 Chilkat Software, Inc. All Rights Reserved.