delphiDll / New FreshBooks / Add Journal Entry
Back to Collection Items
var
http: HCkHttp;
success: Boolean;
json: HCkJsonObject;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
credit: PWideChar;
currency_code: PWideChar;
debit: PWideChar;
description: PWideChar;
detailid: Integer;
id: Integer;
name: PWideChar;
sub_accountid: Integer;
user_entered_date: PWideChar;
Currency_code: PWideChar;
Description: PWideChar;
Entryid: Integer;
Id: Integer;
Name: PWideChar;
User_entered_date: PWideChar;
i: Integer;
count_i: Integer;
begin
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// 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"
// }
// }
json := CkJsonObject_Create();
CkJsonObject_UpdateString(json,'journal_entry.details[0].sub_accountid','635974');
CkJsonObject_UpdateString(json,'journal_entry.details[0].debit','200');
CkJsonObject_UpdateString(json,'journal_entry.details[1].sub_accountid','635976');
CkJsonObject_UpdateString(json,'journal_entry.details[1].credit','200');
CkJsonObject_UpdateString(json,'journal_entry.currency_code','USD');
CkJsonObject_UpdateString(json,'journal_entry.description','Here is a basic description of the journal entry made.');
CkJsonObject_UpdateString(json,'journal_entry.name','JournalEntry');
CkJsonObject_UpdateString(json,'journal_entry.user_entered_date','2019-04-20');
// Adds the "Authorization: Bearer <access_token>" header.
CkHttp_putAuthToken(http,'<access_token>');
CkHttp_SetRequestHeader(http,'Content-Type','application/json');
resp := CkHttp_PostJson3(http,'https://my.freshbooks.com/service/api/accounting/account/{{accountId}}/journal_entries/journal_entries','application/json',json);
if (CkHttp_getLastMethodSuccess(http) = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));
respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(CkHttpResponse__header(resp));
Memo1.Lines.Add('Failed.');
CkHttpResponse_Dispose(resp);
Exit;
end;
CkHttpResponse_Dispose(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
Currency_code := CkJsonObject__stringOf(jResp,'response.result.journal_entry.currency_code');
Description := CkJsonObject__stringOf(jResp,'response.result.journal_entry.description');
Entryid := CkJsonObject_IntOf(jResp,'response.result.journal_entry.entryid');
Id := CkJsonObject_IntOf(jResp,'response.result.journal_entry.id');
Name := CkJsonObject__stringOf(jResp,'response.result.journal_entry.name');
User_entered_date := CkJsonObject__stringOf(jResp,'response.result.journal_entry.user_entered_date');
i := 0;
count_i := CkJsonObject_SizeOfArray(jResp,'response.result.journal_entry.details');
while i < count_i do
begin
CkJsonObject_putI(jResp,i);
credit := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].credit');
currency_code := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].currency_code');
debit := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].debit');
description := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].description');
detailid := CkJsonObject_IntOf(jResp,'response.result.journal_entry.details[i].detailid');
id := CkJsonObject_IntOf(jResp,'response.result.journal_entry.details[i].id');
name := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].name');
sub_accountid := CkJsonObject_IntOf(jResp,'response.result.journal_entry.details[i].sub_accountid');
user_entered_date := CkJsonObject__stringOf(jResp,'response.result.journal_entry.details[i].user_entered_date');
i := i + 1;
end;
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
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}"
}
]
}