Sample code for 30+ languages & platforms
SQL Server

MYOB: Get List of Company Files

See more MYOB Examples

Gets a list of company files.

Chilkat SQL Server Downloads

SQL Server
-- 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)
    -- 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

    EXEC sp_OASetProperty @http, 'AuthToken', 'ACCESS_TOKEN'

    EXEC sp_OASetProperty @http, 'Accept', 'application/json'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-myobapi-key', 'MYOB_API_KEY'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'x-myobapi-version', 'v2'

    DECLARE @strResp nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @strResp OUT, 'https://ar1.api.myob.com/accountright'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END


    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    PRINT 'Response Status Code: ' + @iTmp0

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonResponse OUT

    DECLARE @success int
    EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @strResp
    EXEC sp_OASetProperty @jsonResponse, 'EmitCompact', 0
    EXEC sp_OAMethod @jsonResponse, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        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": "d2014f64-ffdf-487b-8d12-67a20976aca6",
    --   "Name": "Internal Sandbox API",
    --   "LibraryPath": "Internal Sandbox API",
    --   "ProductVersion": "2013.0",
    --   "ProductLevel": {
    --     "Code": 20,
    --     "Name": "Standard"
    --   },
    --   "CheckedOutDate": "2013-06-11T01:47:47.0065514",
    --   "CheckedOutBy": "developers@myob.com",
    --   "Uri": "{cf_uri}",
    --   "Country": "AU"
    -- }
    -- 

    DECLARE @Id nvarchar(4000)

    DECLARE @Name nvarchar(4000)

    DECLARE @LibraryPath nvarchar(4000)

    DECLARE @ProductVersion nvarchar(4000)

    DECLARE @ProductLevelCode int

    DECLARE @ProductLevelName nvarchar(4000)

    DECLARE @CheckedOutDate nvarchar(4000)

    DECLARE @CheckedOutBy nvarchar(4000)

    DECLARE @Uri nvarchar(4000)

    DECLARE @Country nvarchar(4000)

    EXEC sp_OAMethod @jsonResponse, 'StringOf', @Id OUT, 'Id'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @Name OUT, 'Name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @LibraryPath OUT, 'LibraryPath'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @ProductVersion OUT, 'ProductVersion'
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @ProductLevelCode OUT, 'ProductLevel.Code'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @ProductLevelName OUT, 'ProductLevel.Name'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @CheckedOutDate OUT, 'CheckedOutDate'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @CheckedOutBy OUT, 'CheckedOutBy'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @Uri OUT, 'Uri'
    EXEC sp_OAMethod @jsonResponse, 'StringOf', @Country OUT, 'Country'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO