web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Not able to post Cash Receipt Journal line using custom codeunit

(0) ShareShare
ReportReport
Posted on by 13
Hello,
 
I am trying to post the Cash receipt journal line using the custom codeunit, but somehow getting an error. Below is the code for the custom codeunit:
 
codeunit 50100 /Payment Posting Codeunit/
{
    procedure PostPaymentEntry(paymentJournalLineID: Text): Text
    var
        PaymentJournalLineRec: Record /Gen. Journal Line/;
 
    begin
        // Find the Payment Journal Line based on the provided ID
        if PaymentJournalLineRec.GetBySystemId(paymentJournalLineID) then begin
            // Check if the Payment Journal Line is not already posted
            // Perform any additional validation or customization here
 
            // Post the Payment Journal Line
            PaymentJournalLineRec.SendToPosting(12);
            exit('Payment Journal Line posted successfully.');
 
        end
        else begin
            Error('Payment Journal Line not found.');
        end;
    end;
}
 
And I trigger this codeunit using OData API: `POST {baseUrl}/ODataV4/PaymentPostingCodeunit_PostPaymentEntry?company=<company id>`
 
The following body is passed to this API:
{
    /paymentJournalLineID/: /<guid of journal line item/
}
 
Then I am getting the below error in the response:
{
    /error/: {
        /code/: /Application_DialogException/,
        /message/: /Sorry, the current permissions prevented the action. (TableData 45 G/L Register Read: Base Application)  CorrelationId:  <id>./
    }
}
 
I am not sure if I have written the correct codeunit to post the Journal line item or not. Can somebody please help me here?
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    94,862 Super User 2025 Season 2 on at
    Not able to post Cash Receipt Journal line using custom codeunit
    Hi, It is recommended that you refer to the standard API approach. Hope the following helps.
    page 30016 "APIV2 - Journals"
     
    Thanks.
    ZHU
  • Community member Profile Picture
    13 on at
    Not able to post Cash Receipt Journal line using custom codeunit
    Thanks @YUNZHU , but then how can we call the OData API for this extension? I mean what should be passed in the request body?
  • Suggested answer
    Shrey Chauhan Profile Picture
    284 on at
    Not able to post Cash Receipt Journal line using custom codeunit
    Hello,

    Here's the solution for ODATA API Call for Authorization and to Get or Post the request :

    Procedure GetAuthorizationToken();
    begin
            Content.GetHeaders(contentHeaders);
            RequestMessageforToken.GetHeaders(RequestHeaderforToken);
            RequestMessageforToken.SetRequestUri(Rec."Authorization URL");
            RequestMessageforToken.Method('POST');
            if ContentHeaders.Contains('Content-Type') then ContentHeaders.Remove('Content-Type');
            ContentHeaders.Add('Content-Type', 'application/json');
            RequestMessageforToken.Content(Content);
            IsSuccessful := HttpClient.Send(RequestMessageforToken, ResponseMessage);
            if not IsSuccessful then
                Error('Status Code: %1\ Reason : %2', ResponseMessage.HttpStatusCode, ResponseMessage.ReasonPhrase)
            else begin
                ResponseMessage.Content.ReadAs(ResponseText);
                JObject.ReadFrom(ResponseText);
    end; 
     
    Procedure PostURL();
    begin
            Content.GetHeaders(contentHeaders);
            RequestMessageforPost.GetHeaders(RequestHeaderforPost);
            RequestHeaderforPost.Add('Authorization', 'Bearer ' + Rec."Authorization Token");
            RequestMessageforPost.Method('POST');
            RequestMessageforPost.SetRequestUri(Rec."Post URL");
            if ContentHeaders.Contains('Content-Type') then ContentHeaders.Remove('Content-Type');
            ContentHeaders.Add('Content-Type', 'application/json');
            RequestHeaderforPost.Add('Accept', '*/*');
            RequestMessageforPost.Content(Content);
            IsSuccessful := HttpClient.Send(RequestMessageforPostContainer, ResponseMessage);
            if not IsSuccessful then
                Error('Status Code: %1\ Reason : %2', ResponseMessage.HttpStatusCode, ResponseMessage.ReasonPhrase)
            else begin
                ResponseMessage.Content.ReadAs(ResponseText);
                JObject.ReadFrom(ResponseText);
    end;

    Hope this solution helps.
    May other expert's give's you better solution's.

    Thank You,
    Shrey Chauhan
  • Verified answer
    Community member Profile Picture
    13 on at
    Not able to post Cash Receipt Journal line using custom codeunit
    Hello,
     
    Thanks for the responses, everyone. Based on these answers and some additional research, I was able to create a Codeunit to Post the Journal Line items using OData API.
     
    Here is the link to the source code, check it out - https://github.com/jigneshkhatri/MBC-Gen-Journal-Line-Posting-CodeUnit
     
    Thank you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,532

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 3,058 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,927 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans