Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

Not able to post Cash Receipt Journal line using custom codeunit

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?
Categories:
  • Verified answer
    Community member Profile Picture
    Community member 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.
  • Suggested answer
    Shrey Chauhan Profile Picture
    Shrey Chauhan 288 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
  • Community member Profile Picture
    Community member 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
    YUN ZHU Profile Picture
    YUN ZHU 71,749 Super User 2024 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

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,316 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans