Chilkat Online Tools

SQL Server / New FreshBooks / Create Credit Note

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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.

    -- {
    --   "credit_note": {
    --     "credit_number": "0000064",
    --     "currency_code": "CAD",
    --     "create_date": "2020-07-31",
    --     "notes": "Free $150 voucher",
    --     "credit_type": "goodwill",
    --     "terms": "Will be applied to future invoice",
    --     "language": "en",
    --     "lines": [
    --       {
    --         "name": "Credit",
    --         "description": "Goodwill Credit",
    --         "taxName1": null,
    --         "taxAmount1": null,
    --         "taxName2": null,
    --         "taxAmount2": null,
    --         "compounded_tax": false,
    --         "qty": 1,
    --         "unit_cost": {
    --           "amount": "150",
    --           "code": "CAD"
    --         },
    --         "credit_note": null
    --       }
    --     ],
    --     "clientid": 123
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.credit_number', '0000064'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.currency_code', 'CAD'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.create_date', '2020-07-31'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.notes', 'Free $150 voucher'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.credit_type', 'goodwill'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.terms', 'Will be applied to future invoice'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.language', 'en'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.lines[0].name', 'Credit'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.lines[0].description', 'Goodwill Credit'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'credit_note.lines[0].taxName1'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'credit_note.lines[0].taxAmount1'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'credit_note.lines[0].taxName2'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'credit_note.lines[0].taxAmount2'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'credit_note.lines[0].compounded_tax', 0
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'credit_note.lines[0].qty', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.lines[0].unit_cost.amount', '150'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'credit_note.lines[0].unit_cost.code', 'CAD'
    EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'credit_note.lines[0].credit_note'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'credit_note.clientid', 123

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.freshbooks.com/accounting/account/{{accountid}}/credit_notes/credit_notes', '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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-d '{
    "credit_note": {
        "credit_number": "0000064",
        "currency_code": "CAD",
        "create_date": "2020-07-31",
        "notes": "Free $150 voucher",
        "credit_type": "goodwill",
        "terms": "Will be applied to future invoice",
        "language": "en",
        "lines": [
            {
                "name": "Credit",
                "description": "Goodwill Credit",
                "taxName1": null,
                "taxAmount1": null,
                "taxName2": null,
                "taxAmount2": null,
                "compounded_tax": false,
                "qty": 1,
                "unit_cost": {
                    "amount": "150",
                    "code": "CAD"
                },
                "credit_note": null
            }
        ],
        "clientid": {{clientid}}
    }
}'
https://api.freshbooks.com/accounting/account/{{accountid}}/credit_notes/credit_notes

Postman Collection Item JSON

{
  "name": "Create Credit Note",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"credit_note\": {\n        \"credit_number\": \"0000064\",\n        \"currency_code\": \"CAD\",\n        \"create_date\": \"2020-07-31\",\n        \"notes\": \"Free $150 voucher\",\n        \"credit_type\": \"goodwill\",\n        \"terms\": \"Will be applied to future invoice\",\n        \"language\": \"en\",\n        \"lines\": [\n            {\n                \"name\": \"Credit\",\n                \"description\": \"Goodwill Credit\",\n                \"taxName1\": null,\n                \"taxAmount1\": null,\n                \"taxName2\": null,\n                \"taxAmount2\": null,\n                \"compounded_tax\": false,\n                \"qty\": 1,\n                \"unit_cost\": {\n                    \"amount\": \"150\",\n                    \"code\": \"CAD\"\n                },\n                \"credit_note\": null\n            }\n        ],\n        \"clientid\": {{clientid}}\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountid}}/credit_notes/credit_notes",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountid}}",
        "credit_notes",
        "credit_notes"
      ]
    }
  },
  "response": [
  ]
}