Chilkat Online Tools

SQL Server / New FreshBooks / Add Journal Entry

Back to Collection Items

-- 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 assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "journal_entry": {
    --     "details": [
    --       {
    --         "sub_accountid": "635974",
    --         "debit": "200"
    --       },
    --       {
    --         "sub_accountid": "635976",
    --         "credit": "200"
    --       }
    --     ],
    --     "currency_code": "USD",
    --     "description": "Here is a basic description of the journal entry made.",
    --     "name": "JournalEntry",
    --     "user_entered_date": "2019-04-20"
    --   }
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.details[0].sub_accountid', '635974'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.details[0].debit', '200'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.details[1].sub_accountid', '635976'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.details[1].credit', '200'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.currency_code', 'USD'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.description', 'Here is a basic description of the journal entry made.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.name', 'JournalEntry'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'journal_entry.user_entered_date', '2019-04-20'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://my.freshbooks.com/service/api/accounting/account/{{accountId}}/journal_entries/journal_entries', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "response": {
    --     "result": {
    --       "journal_entry": {
    --         "currency_code": "USD",
    --         "description": "Here is a basic description of the journal entry made.",
    --         "details": [
    --           {
    --             "credit": null,
    --             "currency_code": "USD",
    --             "debit": "200",
    --             "description": "Here is a basic description of the journal entry made.",
    --             "detailid": 1313598,
    --             "id": 1313598,
    --             "name": "JournalEntry",
    --             "sub_accountid": 635974,
    --             "user_entered_date": "2019-04-20"
    --           },
    --           {
    --             "credit": "200",
    --             "currency_code": "USD",
    --             "debit": null,
    --             "description": "Here is a basic description of the journal entry made.",
    --             "detailid": 1313600,
    --             "id": 1313600,
    --             "name": "JournalEntry",
    --             "sub_accountid": 635976,
    --             "user_entered_date": "2019-04-20"
    --           }
    --         ],
    --         "entryid": 629310,
    --         "id": 629310,
    --         "name": "JournalEntry",
    --         "user_entered_date": "2019-04-20"
    --       }
    --     }
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @credit nvarchar(4000)

    DECLARE @currency_code nvarchar(4000)

    DECLARE @debit nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @detailid int

    DECLARE @id int

    DECLARE @name nvarchar(4000)

    DECLARE @sub_accountid int

    DECLARE @user_entered_date nvarchar(4000)

    DECLARE @Currency_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Currency_code OUT, 'response.result.journal_entry.currency_code'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'response.result.journal_entry.description'
    DECLARE @Entryid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Entryid OUT, 'response.result.journal_entry.entryid'
    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'response.result.journal_entry.id'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'response.result.journal_entry.name'
    DECLARE @User_entered_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @User_entered_date OUT, 'response.result.journal_entry.user_entered_date'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.result.journal_entry.details'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @credit OUT, 'response.result.journal_entry.details[i].credit'
        EXEC sp_OAMethod @jResp, 'StringOf', @currency_code OUT, 'response.result.journal_entry.details[i].currency_code'
        EXEC sp_OAMethod @jResp, 'StringOf', @debit OUT, 'response.result.journal_entry.details[i].debit'
        EXEC sp_OAMethod @jResp, 'StringOf', @description OUT, 'response.result.journal_entry.details[i].description'
        EXEC sp_OAMethod @jResp, 'IntOf', @detailid OUT, 'response.result.journal_entry.details[i].detailid'
        EXEC sp_OAMethod @jResp, 'IntOf', @id OUT, 'response.result.journal_entry.details[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'response.result.journal_entry.details[i].name'
        EXEC sp_OAMethod @jResp, 'IntOf', @sub_accountid OUT, 'response.result.journal_entry.details[i].sub_accountid'
        EXEC sp_OAMethod @jResp, 'StringOf', @user_entered_date OUT, 'response.result.journal_entry.details[i].user_entered_date'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "journal_entry": {
    "details": [
      {
        "sub_accountid": "635974",
        "debit": "200"
      },
      {
        "sub_accountid": "635976",
        "credit": "200"
      }
    ],
    "currency_code": "USD",
    "description": "Here is a basic description of the journal entry made.",
    "name": "JournalEntry",
    "user_entered_date": "2019-04-20"
  }
}'
https://my.freshbooks.com/service/api/accounting/account/{{accountId}}/journal_entries/journal_entries

Postman Collection Item JSON

{
  "name": "Add Journal Entry",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"journal_entry\": {\n    \"details\": [\n      {\n        \"sub_accountid\": \"635974\",\n        \"debit\": \"200\"\n      },\n      {\n        \"sub_accountid\": \"635976\",\n        \"credit\": \"200\"\n      }\n    ],\n    \"currency_code\": \"USD\",\n    \"description\": \"Here is a basic description of the journal entry made.\",\n    \"name\": \"JournalEntry\",\n    \"user_entered_date\": \"2019-04-20\"\n  }\n}"
    },
    "url": {
      "raw": "https://my.freshbooks.com/service/api/accounting/account/{{accountId}}/journal_entries/journal_entries",
      "protocol": "https",
      "host": [
        "my",
        "freshbooks",
        "com"
      ],
      "path": [
        "service",
        "api",
        "accounting",
        "account",
        "{{accountId}}",
        "journal_entries",
        "journal_entries"
      ]
    }
  },
  "response": [
    {
      "name": "Add Journal Entry",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"journal_entry\": {\n    \"details\": [\n      {\n        \"sub_accountid\": \"635974\",\n        \"debit\": \"200\"\n      },\n      {\n        \"sub_accountid\": \"635976\",\n        \"credit\": \"200\"\n      }\n    ],\n    \"currency_code\": \"USD\",\n    \"description\": \"Here is a basic description of the journal entry made.\",\n    \"name\": \"JournalEntry\",\n    \"user_entered_date\": \"2019-04-20\"\n  }\n}"
        },
        "url": {
          "raw": "https://my.freshbooks.com/service/api/accounting/account/{{accountId}}/journal_entries/journal_entries",
          "protocol": "https",
          "host": [
            "my",
            "freshbooks",
            "com"
          ],
          "path": [
            "service",
            "api",
            "accounting",
            "account",
            "{{accountId}}",
            "journal_entries",
            "journal_entries"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gOXkNBDAcPPVUMTBBRXUcIWQpME18AXwcKQkUYFAZLXEUWAgcVHQdKVB8HAlFWUFEFUQtSAQ4HAwBRAU0CVgBORlRVBFEAWVFRXFQDBFdWB10TGlUDChAHbQ=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:46:24 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:46:24 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17363-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"result\": {\n            \"journal_entry\": {\n                \"currency_code\": \"USD\",\n                \"description\": \"Here is a basic description of the journal entry made.\",\n                \"details\": [\n                    {\n                        \"credit\": null,\n                        \"currency_code\": \"USD\",\n                        \"debit\": \"200\",\n                        \"description\": \"Here is a basic description of the journal entry made.\",\n                        \"detailid\": 1313598,\n                        \"id\": 1313598,\n                        \"name\": \"JournalEntry\",\n                        \"sub_accountid\": 635974,\n                        \"user_entered_date\": \"2019-04-20\"\n                    },\n                    {\n                        \"credit\": \"200\",\n                        \"currency_code\": \"USD\",\n                        \"debit\": null,\n                        \"description\": \"Here is a basic description of the journal entry made.\",\n                        \"detailid\": 1313600,\n                        \"id\": 1313600,\n                        \"name\": \"JournalEntry\",\n                        \"sub_accountid\": 635976,\n                        \"user_entered_date\": \"2019-04-20\"\n                    }\n                ],\n                \"entryid\": 629310,\n                \"id\": 629310,\n                \"name\": \"JournalEntry\",\n                \"user_entered_date\": \"2019-04-20\"\n            }\n        }\n    }\n}"
    }
  ]
}