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

Business Central Web Service: "Internal_ServerError" When Posting Journal Line

(1) ShareShare
ReportReport
Posted on by 79
I'm working on integrating Microsoft Dynamics 365 Business Central with an external system via a web service. The integration involves creating HR Leave Journal Lines and then posting them using a custom AL procedure.
 
procedure CreateAndPostLeaveLine(StaffNo: Code[20]; Days: Integer; LeaveType: Text) Res: Text
    var
        HRJournalLine: Record "HR Journal Line";
        HRLeavePeriod: Record "HR Leave Periods";
        LeaveTypeCode: Code[20];
        Success: Boolean;
        ErrorMessage: Text;
    begin
        // Validate input parameters
        if StaffNo = '' then
            Error('Staff number cannot be empty.');

        if Days <= 0 then
            Error('Days must be greater than zero.');
        HRLeavePeriod.Reset();
        HRLeavePeriod.SetRange(HRLeavePeriod."Closed", false);
        if not HRLeavePeriod.Find('-') then
            Error('No active leave period found.');

        // Determine leave type based on input
        case LeaveType of
            'ANNUAL':
                LeaveTypeCode := 'ANNUAL';
            'COMPASSIONATE':
                LeaveTypeCode := 'COMPASSIONATE';
            'DISCRETIONARY':
                LeaveTypeCode := 'DISCRETIONARY';
            'MATERNITY':
                LeaveTypeCode := 'MATERNITY';
            'PATERNITY':
                LeaveTypeCode := 'PATERNITY';
            'SICK':
                LeaveTypeCode := 'SICK';
            'STUDY':
                LeaveTypeCode := 'STUDY';
            else
                Error('Invalid leave type: %1', LeaveType);
        end;

        // Insert the HR Journal Line with proper error handling
        Clear(ErrorMessage);
        Success := true;
        HRJournalLine.Init();
        HRJournalLine."Journal Template Name" := 'LEAVE';
        HRJournalLine."Journal Batch Name" := 'DEFAULT';
        HRJournalLine."Leave Period" := HRLeavePeriod."Period Code";
        HRJournalLine.Validate("Staff No.", StaffNo);
        HRJournalLine."Staff No." := StaffNo;
        HRJournalLine."Posting Date" := System.Today();
        HRJournalLine."Document No." := '1';
        HRJournalLine."Leave Type" := LeaveTypeCode;
        HRJournalLine."Leave Entry Type" := HRJournalLine."Leave Entry Type"::Positive;
        HRJournalLine."No. of Days" := Days;
        HRJournalLine.Description := Format(LeaveType) + ' Leave for ' + StaffNo;

        // Try inserting the record
        if not HRJournalLine.Insert() then begin
            Success := false;
            ErrorMessage := 'Failed to insert leave journal line.';
        end;


        // If insertion succeeded, proceed with posting
        if Success then begin
            if not Codeunit.Run(Codeunit::"HR Leave Jnl.-Post", HRJournalLine) then begin
                Success := false;
                ErrorMessage := 'Failed to post leave journal line.';
            end;
        end;

        // If any step failed, rollback and show error
        if not Success then
            Error(ErrorMessage)
        else
            exit('Leave journal line posted successfully.');
    end;
 
Issue Description
  • Creating journal lines via the API works as expected.
  • Posting the leave journal line via the API results in the following error
{
    "error": {
        "code": "Internal_ServerError",
        "message": "An error occurred and the transaction is stopped. Contact your administrator or partner for further assistance.  CorrelationId:  79f8ea8a-c59b-4203-b8f7-b1b60d42cb8a."
    }
}
  • However, leave lines created via the API can be posted successfully in the Business Central UI.

What I Tried

To diagnose the issue, I removed the posting step from my procedure:

// If insertion succeeded, proceed with posting
if Success then begin
    if not Codeunit.Run(Codeunit::"HR Leave Jnl.-Post", HRJournalLine) then begin
        Success := false;
        ErrorMessage := 'Failed to post leave journal line.';
    end;
end;

When I did this, journal lines were created without issues, confirming that the creation logic works fine. However, once I added back the posting logic, the error returned.

Assumptions & Observations

  • Since manual posting from the UI works, the issue might be related to web service execution context rather than the posting logic itself.
  • The user making the API request may lack sufficient permissions.

Questions

  1. Has anyone encountered similar Internal_ServerError issues when posting journal lines via a Business Central web service?
  2. Could this be a permissions issue with the web service user? What permissions should be checked?
  3. Are there special considerations when running posting logic from a web service (e.g., handling UI-related commands like Message())?
  4. Any debugging tips for tracking down the cause of the error?

Any advice or guidance would be greatly appreciated! Thanks in advance.

 
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,316 Super User 2025 Season 2 on at
    Business Central Web Service: "Internal_ServerError" When Posting Journal Line
     
    I suggest you use TryFunction for error handling to find the source of this problem.
     
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Verified answer
    Alexander Drogin Profile Picture
    262 on at
    Business Central Web Service: "Internal_ServerError" When Posting Journal Line
    I suppose the problem is in this line:
     
    if not Codeunit.Run(Codeunit::"HR Leave Jnl.-Post", HRJournalLine) then begin
     
    Return value of the Codeunit.Run function cannot be used in the calling code if there is an open write transaction, and in your sample code, HRJournalLine.Insert opens this transaction. You need to commit journal lines before posting.
     

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 4,160

#2
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 2,943

#3
Sumit Singh Profile Picture

Sumit Singh 2,823

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans