Chilkat Online Tools

SQL Server / New FreshBooks / Update a Time 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
    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.

    -- {
    --   "time_entry": {
    --     "is_logged": true,
    --     "duration": 600,
    --     "note": "Updated Note",
    --     "started_at": "2016-08-16T18:03:28.000Z",
    --     "timer": {
    --       "id": "2936"
    --     },
    --     "client_id": 123,
    --     "project_id": 123
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'time_entry.is_logged', 1
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'time_entry.duration', 600
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'time_entry.note', 'Updated Note'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'time_entry.started_at', '2016-08-16T18:03:28.000Z'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'time_entry.timer.id', '2936'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'time_entry.client_id', 123
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'time_entry.project_id', 123

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

    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/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}', @sbRequestBody, 'utf-8', '', 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)

    -- {
    --   "time_entry": {
    --     "id": 47902064,
    --     "identity_id": 1882548,
    --     "timer": null,
    --     "is_logged": true,
    --     "started_at": "2016-08-16T18:03:28Z",
    --     "created_at": "2019-04-24T18:44:50Z",
    --     "duration": 600,
    --     "client_id": 31006,
    --     "project_id": 2989082,
    --     "pending_client": null,
    --     "pending_project": null,
    --     "pending_task": null,
    --     "task_id": null,
    --     "service_id": null,
    --     "note": "Updated Note",
    --     "active": true,
    --     "billable": false,
    --     "billed": false,
    --     "internal": false,
    --     "retainer_id": null
    --   }
    -- }

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

    DECLARE @Id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'time_entry.id'
    DECLARE @Identity_id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Identity_id OUT, 'time_entry.identity_id'
    DECLARE @Timer nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Timer OUT, 'time_entry.timer'
    DECLARE @Is_logged int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Is_logged OUT, 'time_entry.is_logged'
    DECLARE @Started_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Started_at OUT, 'time_entry.started_at'
    DECLARE @Created_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Created_at OUT, 'time_entry.created_at'
    DECLARE @Duration int
    EXEC sp_OAMethod @jResp, 'IntOf', @Duration OUT, 'time_entry.duration'
    DECLARE @Client_id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Client_id OUT, 'time_entry.client_id'
    DECLARE @Project_id int
    EXEC sp_OAMethod @jResp, 'IntOf', @Project_id OUT, 'time_entry.project_id'
    DECLARE @Pending_client nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Pending_client OUT, 'time_entry.pending_client'
    DECLARE @Pending_project nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Pending_project OUT, 'time_entry.pending_project'
    DECLARE @Pending_task nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Pending_task OUT, 'time_entry.pending_task'
    DECLARE @Task_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Task_id OUT, 'time_entry.task_id'
    DECLARE @Service_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Service_id OUT, 'time_entry.service_id'
    DECLARE @Note nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Note OUT, 'time_entry.note'
    DECLARE @Active int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Active OUT, 'time_entry.active'
    DECLARE @Billable int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Billable OUT, 'time_entry.billable'
    DECLARE @Billed int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Billed OUT, 'time_entry.billed'
    DECLARE @Internal int
    EXEC sp_OAMethod @jResp, 'BoolOf', @Internal OUT, 'time_entry.internal'
    DECLARE @Retainer_id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Retainer_id OUT, 'time_entry.retainer_id'

    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: "
	-d '{
  "time_entry": {
      "is_logged": true,
      "duration": 600,
      "note": "Updated Note",
      "started_at": "2016-08-16T18:03:28.000Z",
      "timer": {
          "id": "2936"
      },
      "client_id": {{clientId}},
      "project_id": {{projectId}}
  }
}'
https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}

Postman Collection Item JSON

{
  "name": "Update a Time Entry",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "",
        "name": "Content-Type",
        "value": "",
        "type": "text",
        "disabled": true
      },
      {
        "key": "Content-Type",
        "value": "",
        "disabled": true
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"time_entry\": {\n      \"is_logged\": true,\n      \"duration\": 600,\n      \"note\": \"Updated Note\",\n      \"started_at\": \"2016-08-16T18:03:28.000Z\",\n      \"timer\": {\n          \"id\": \"2936\"\n      },\n      \"client_id\": {{clientId}},\n      \"project_id\": {{projectId}}\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "timetracking",
        "business",
        "{{businessId}}",
        "time_entries",
        "{{timeEntryId}}"
      ]
    }
  },
  "response": [
    {
      "name": "Update a Time Entry",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"time_entry\": {\n      \"is_logged\": true,\n      \"duration\": 600,\n      \"note\": \"Updated Note\",\n      \"started_at\": \"2016-08-16T18:03:28.000Z\",\n      \"timer\": {\n          \"id\": \"2936\"\n      },\n      \"client_id\": {{clientId}},\n      \"project_id\": {{projectId}}\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/timetracking/business/{{businessId}}/time_entries/{{timeEntryId}}",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "timetracking",
            "business",
            "{{businessId}}",
            "time_entries",
            "{{timeEntryId}}"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1904.25.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNFlkPXT1dVkBASktLBEIORhAHUw0VDw5dbFUKFRBOExtWSAEYAlRVVlcIUghUCgACBAFUCE0DVAdORgQBUVFaCQoEVQVVDwVWBFYTGlUDChAHbQ=="
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 18:45:14 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "450"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 18:45:14 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": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"time_entry\": {\n        \"id\": 47902064,\n        \"identity_id\": 1882548,\n        \"timer\": null,\n        \"is_logged\": true,\n        \"started_at\": \"2016-08-16T18:03:28Z\",\n        \"created_at\": \"2019-04-24T18:44:50Z\",\n        \"duration\": 600,\n        \"client_id\": 31006,\n        \"project_id\": 2989082,\n        \"pending_client\": null,\n        \"pending_project\": null,\n        \"pending_task\": null,\n        \"task_id\": null,\n        \"service_id\": null,\n        \"note\": \"Updated Note\",\n        \"active\": true,\n        \"billable\": false,\n        \"billed\": false,\n        \"internal\": false,\n        \"retainer_id\": null\n    }\n}"
    }
  ]
}