Chilkat Online Tools

SQL Server / New FreshBooks / Update Invoice Profile

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.

    -- {
    --   "invoice_profile": {
    --     "frequency": "2w",
    --     "numberRecurring": 12
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'invoice_profile.frequency', '2w'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'invoice_profile.numberRecurring', 12

    -- 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 @sbRequestBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'PUT', 'https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles/{{invoiceProfileId}}', @sbRequestBody, 'utf-8', 'application/json', 0, 0
    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
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

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

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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 @sbRequestBody
        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": {
    --       "invoice_profile": {
    --         "accounting_systemid": "wkMd2g",
    --         "address": "",
    --         "amount": {
    --           "amount": "0.00",
    --           "code": "USD"
    --         },
    --         "auto_bill": false,
    --         "bill_gateway": null,
    --         "city": "",
    --         "code": "",
    --         "country": "Canada",
    --         "create_date": "2019-04-25",
    --         "currency_code": "USD",
    --         "customerid": 31006,
    --         "description": "",
    --         "disable": false,
    --         "discount_total": {
    --           "amount": "0.00",
    --           "code": "USD"
    --         },
    --         "discount_value": "0",
    --         "due_offset_days": 0,
    --         "ext_archive": null,
    --         "fname": "Nicolas",
    --         "frequency": "2w",
    --         "id": 456,
    --         "include_unbilled_time": false,
    --         "language": "en",
    --         "lname": "one",
    --         "notes": "",
    --         "numberRecurring": 12,
    --         "occurrences_to_date": 0,
    --         "organization": "first company",
    --         "ownerid": 1,
    --         "payment_details": "",
    --         "po_number": null,
    --         "profileid": 456,
    --         "province": "",
    --         "require_auto_bill": false,
    --         "retainer_id": null,
    --         "send_email": true,
    --         "send_gmail": false,
    --         "street": "",
    --         "street2": "",
    --         "terms": null,
    --         "total_accrued_revenue": null,
    --         "updated": "2019-04-24 14:18:27",
    --         "vat_name": null,
    --         "vat_number": "",
    --         "vis_state": 0
    --       }
    --     }
    --   }
    -- }

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

    DECLARE @Accounting_systemid nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Accounting_systemid OUT, 'response.result.invoice_profile.accounting_systemid'
    DECLARE @Address nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Address OUT, 'response.result.invoice_profile.address'
    DECLARE @Amount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Amount OUT, 'response.result.invoice_profile.amount.amount'
    DECLARE @Code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Code OUT, 'response.result.invoice_profile.amount.code'
    DECLARE @Auto_bill int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Auto_bill OUT, 'response.result.invoice_profile.auto_bill'
    DECLARE @Bill_gateway nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Bill_gateway OUT, 'response.result.invoice_profile.bill_gateway'
    DECLARE @City nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @City OUT, 'response.result.invoice_profile.city'
    DECLARE @Invoice_profileCode nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Invoice_profileCode OUT, 'response.result.invoice_profile.code'
    DECLARE @Country nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Country OUT, 'response.result.invoice_profile.country'
    DECLARE @Create_date nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Create_date OUT, 'response.result.invoice_profile.create_date'
    DECLARE @Currency_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Currency_code OUT, 'response.result.invoice_profile.currency_code'
    DECLARE @Customerid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Customerid OUT, 'response.result.invoice_profile.customerid'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'response.result.invoice_profile.description'
    DECLARE @Disable int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Disable OUT, 'response.result.invoice_profile.disable'
    DECLARE @Discount_totalAmount nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Discount_totalAmount OUT, 'response.result.invoice_profile.discount_total.amount'
    DECLARE @Discount_totalCode nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Discount_totalCode OUT, 'response.result.invoice_profile.discount_total.code'
    DECLARE @Discount_value nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Discount_value OUT, 'response.result.invoice_profile.discount_value'
    DECLARE @Due_offset_days int
    EXEC sp_OAMethod @jResp, 'IntOf', @Due_offset_days OUT, 'response.result.invoice_profile.due_offset_days'
    DECLARE @Ext_archive nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Ext_archive OUT, 'response.result.invoice_profile.ext_archive'
    DECLARE @Fname nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Fname OUT, 'response.result.invoice_profile.fname'
    DECLARE @Frequency nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Frequency OUT, 'response.result.invoice_profile.frequency'
    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'response.result.invoice_profile.id'
    DECLARE @Include_unbilled_time int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Include_unbilled_time OUT, 'response.result.invoice_profile.include_unbilled_time'
    DECLARE @Language nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Language OUT, 'response.result.invoice_profile.language'
    DECLARE @Lname nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Lname OUT, 'response.result.invoice_profile.lname'
    DECLARE @Notes nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Notes OUT, 'response.result.invoice_profile.notes'
    DECLARE @NumberRecurring int
    EXEC sp_OAMethod @jResp, 'IntOf', @NumberRecurring OUT, 'response.result.invoice_profile.numberRecurring'
    DECLARE @Occurrences_to_date int
    EXEC sp_OAMethod @jResp, 'IntOf', @Occurrences_to_date OUT, 'response.result.invoice_profile.occurrences_to_date'
    DECLARE @Organization nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Organization OUT, 'response.result.invoice_profile.organization'
    DECLARE @Ownerid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Ownerid OUT, 'response.result.invoice_profile.ownerid'
    DECLARE @Payment_details nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Payment_details OUT, 'response.result.invoice_profile.payment_details'
    DECLARE @Po_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Po_number OUT, 'response.result.invoice_profile.po_number'
    DECLARE @Profileid int
    EXEC sp_OAMethod @jResp, 'IntOf', @Profileid OUT, 'response.result.invoice_profile.profileid'
    DECLARE @Province nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Province OUT, 'response.result.invoice_profile.province'
    DECLARE @Require_auto_bill int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Require_auto_bill OUT, 'response.result.invoice_profile.require_auto_bill'
    DECLARE @Retainer_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Retainer_id OUT, 'response.result.invoice_profile.retainer_id'
    DECLARE @Send_email int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Send_email OUT, 'response.result.invoice_profile.send_email'
    DECLARE @Send_gmail int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Send_gmail OUT, 'response.result.invoice_profile.send_gmail'
    DECLARE @Street nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Street OUT, 'response.result.invoice_profile.street'
    DECLARE @Street2 nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Street2 OUT, 'response.result.invoice_profile.street2'
    DECLARE @Terms nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Terms OUT, 'response.result.invoice_profile.terms'
    DECLARE @Total_accrued_revenue nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Total_accrued_revenue OUT, 'response.result.invoice_profile.total_accrued_revenue'
    DECLARE @Updated nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Updated OUT, 'response.result.invoice_profile.updated'
    DECLARE @Vat_name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Vat_name OUT, 'response.result.invoice_profile.vat_name'
    DECLARE @Vat_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Vat_number OUT, 'response.result.invoice_profile.vat_number'
    DECLARE @Vis_state int
    EXEC sp_OAMethod @jResp, 'IntOf', @Vis_state OUT, 'response.result.invoice_profile.vis_state'

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


END
GO

Curl Command

curl -X PUT
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "invoice_profile": {
    "frequency": "2w",
    "numberRecurring": 12
  }
}'
https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles/{{invoiceProfileId}}

Postman Collection Item JSON

{
  "name": "Update Invoice Profile",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"invoice_profile\": {\n    \"frequency\": \"2w\",\n    \"numberRecurring\": 12\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles/{{invoiceProfileId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "accounting",
        "account",
        "{{accountId}}",
        "invoice_profiles",
        "invoice_profiles",
        "{{invoiceProfileId}}"
      ]
    },
    "description": "Update invoice profile to two weeks and increase the number of remaining invoices to 10.\n\n"
  },
  "response": [
    {
      "name": "Update Invoice Profile",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"invoice_profile\": {\n    \"frequency\": \"2w\",\n    \"numberRecurring\": 12\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/accounting/account/{{accountId}}/invoice_profiles/invoice_profiles/{{invoiceProfileId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "accounting",
            "account",
            "{{accountId}}",
            "invoice_profiles",
            "invoice_profiles",
            "{{invoiceProfileId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSAwEOVlcTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4DFVdfRgFPCkNFR0gNX0BcCwUGPUAQVwRRVFFBCQxXF14IUAcURFgHDw9dERxUTVIZAA5SUAAABlBUWlUJVQtUCwsYBQpJGwcJAwFRUVMAUFdWWVUIB1BAG1dWChdUaw=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:18:27 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "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:18:27 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17369-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            \"invoice_profile\": {\n                \"accounting_systemid\": \"wkMd2g\",\n                \"address\": \"\",\n                \"amount\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"auto_bill\": false,\n                \"bill_gateway\": null,\n                \"city\": \"\",\n                \"code\": \"\",\n                \"country\": \"Canada\",\n                \"create_date\": \"2019-04-25\",\n                \"currency_code\": \"USD\",\n                \"customerid\": 31006,\n                \"description\": \"\",\n                \"disable\": false,\n                \"discount_total\": {\n                    \"amount\": \"0.00\",\n                    \"code\": \"USD\"\n                },\n                \"discount_value\": \"0\",\n                \"due_offset_days\": 0,\n                \"ext_archive\": null,\n                \"fname\": \"Nicolas\",\n                \"frequency\": \"2w\",\n                \"id\": 456,\n                \"include_unbilled_time\": false,\n                \"language\": \"en\",\n                \"lname\": \"one\",\n                \"notes\": \"\",\n                \"numberRecurring\": 12,\n                \"occurrences_to_date\": 0,\n                \"organization\": \"first company\",\n                \"ownerid\": 1,\n                \"payment_details\": \"\",\n                \"po_number\": null,\n                \"profileid\": 456,\n                \"province\": \"\",\n                \"require_auto_bill\": false,\n                \"retainer_id\": null,\n                \"send_email\": true,\n                \"send_gmail\": false,\n                \"street\": \"\",\n                \"street2\": \"\",\n                \"terms\": null,\n                \"total_accrued_revenue\": null,\n                \"updated\": \"2019-04-24 14:18:27\",\n                \"vat_name\": null,\n                \"vat_number\": \"\",\n                \"vis_state\": 0\n            }\n        }\n    }\n}"
    }
  ]
}