SQL Server
SQL Server
Xero Update Account Details
See more Xero Examples
Update some details of an account in a Xero company (Accounting API)Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @jsonToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT
EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/xero-access-token.json'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jsonToken, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
RETURN
END
EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
-- Replace the value here with an actual tenant ID obtained from this example:
-- Get Xero Tenant IDs
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Xero-tenant-id', '83299b9e-5747-4a14-a18a-a6c94f824eb7'
EXEC sp_OASetProperty @http, 'Accept', 'application/json'
-- The following JSON is sent in the request body:
-- {
-- "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
-- "Name": "Sales account",
-- "Type": "REVENUE",
-- "TaxType": "OUTPUT",
-- "Description": "Income from any normal business trading activity",
-- "EnablePaymentsToAccount": "false",
-- "ShowInExpenseClaims": "false"
-- }
-- Use this online tool to generate the code from sample JSON:
-- Generate Code to Create JSON
DECLARE @jsonRequestBody int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonRequestBody OUT
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'AccountID', '54ddab14-4a8d-45cf-86be-076c99a0cea0'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'Name', 'Sales account'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'Type', 'REVENUE'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'TaxType', 'OUTPUT'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'Description', 'Income from any normal business trading activity'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'EnablePaymentsToAccount', 'false'
EXEC sp_OAMethod @jsonRequestBody, 'UpdateString', @success OUT, 'ShowInExpenseClaims', 'false'
DECLARE @url nvarchar(4000)
SELECT @url = 'https://api.xero.com/api.xro/2.0/Accounts/54ddab14-4a8d-45cf-86be-076c99a0cea0'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @url, @jsonRequestBody, 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @jsonRequestBody
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Response Status Code: ' + @iTmp0
DECLARE @jsonResponse int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResponse OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @sTmp0
EXEC sp_OASetProperty @jsonResponse, 'EmitCompact', 0
EXEC sp_OAMethod @jsonResponse, 'Emit', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 >= 300
BEGIN
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @jsonRequestBody
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @jsonResponse
RETURN
END
-- Sample output...
-- (See the parsing code below..)
--
-- Use the this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
-- {
-- "Id": "430e92a8-de02-41d5-a00e-3ef899188aea",
-- "Status": "OK",
-- "ProviderName": "Chilkat2222",
-- "DateTimeUTC": "\/Date(1587162517005)\/",
-- "Accounts": [
-- {
-- "AccountID": "54ddab14-4a8d-45cf-86be-076c99a0cea0",
-- "Code": "201",
-- "Name": "Sales account",
-- "Status": "ACTIVE",
-- "Type": "REVENUE",
-- "TaxType": "OUTPUT",
-- "Description": "Income from any normal business trading activity",
-- "Class": "REVENUE",
-- "EnablePaymentsToAccount": false,
-- "ShowInExpenseClaims": false,
-- "ReportingCode": "REV",
-- "ReportingCodeName": "Revenue",
-- "UpdatedDateUTC": "\/Date(1587162517090+0000)\/",
-- "AddToWatchlist": false
-- }
-- ]
-- }
--
DECLARE @AccountID nvarchar(4000)
DECLARE @Code nvarchar(4000)
DECLARE @Name nvarchar(4000)
DECLARE @Type nvarchar(4000)
DECLARE @TaxType nvarchar(4000)
DECLARE @Description nvarchar(4000)
DECLARE @Class nvarchar(4000)
DECLARE @EnablePaymentsToAccount int
DECLARE @ShowInExpenseClaims int
DECLARE @ReportingCode nvarchar(4000)
DECLARE @ReportingCodeName nvarchar(4000)
DECLARE @UpdatedDateUTC nvarchar(4000)
DECLARE @AddToWatchlist int
DECLARE @Id nvarchar(4000)
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Id OUT, 'Id'
DECLARE @Status nvarchar(4000)
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Status OUT, 'Status'
DECLARE @ProviderName nvarchar(4000)
EXEC sp_OAMethod @jsonResponse, 'StringOf', @ProviderName OUT, 'ProviderName'
DECLARE @DateTimeUTC nvarchar(4000)
EXEC sp_OAMethod @jsonResponse, 'StringOf', @DateTimeUTC OUT, 'DateTimeUTC'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'Accounts'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jsonResponse, 'I', @i
EXEC sp_OAMethod @jsonResponse, 'StringOf', @AccountID OUT, 'Accounts[i].AccountID'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Code OUT, 'Accounts[i].Code'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Name OUT, 'Accounts[i].Name'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Status OUT, 'Accounts[i].Status'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Type OUT, 'Accounts[i].Type'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @TaxType OUT, 'Accounts[i].TaxType'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Description OUT, 'Accounts[i].Description'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @Class OUT, 'Accounts[i].Class'
EXEC sp_OAMethod @jsonResponse, 'BoolOf', @EnablePaymentsToAccount OUT, 'Accounts[i].EnablePaymentsToAccount'
EXEC sp_OAMethod @jsonResponse, 'BoolOf', @ShowInExpenseClaims OUT, 'Accounts[i].ShowInExpenseClaims'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @ReportingCode OUT, 'Accounts[i].ReportingCode'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @ReportingCodeName OUT, 'Accounts[i].ReportingCodeName'
EXEC sp_OAMethod @jsonResponse, 'StringOf', @UpdatedDateUTC OUT, 'Accounts[i].UpdatedDateUTC'
EXEC sp_OAMethod @jsonResponse, 'BoolOf', @AddToWatchlist OUT, 'Accounts[i].AddToWatchlist'
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @jsonRequestBody
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @jsonResponse
END
GO