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;
{ "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." } }
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.
Message()
Any advice or guidance would be greatly appreciated! Thanks in advance.
if not Codeunit.Run(Codeunit::"HR Leave Jnl.-Post", HRJournalLine) then begin
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
Rishabh Kanaskar 4,160
Nimsara Jayathilaka. 2,943
Sumit Singh 2,823