Skip to content

Commit

Permalink
GP - Safe Batch Posting (#26608)
Browse files Browse the repository at this point in the history
This PR updates the GP migration batch posting to safely run and log any
errors instead of fail the migration.
Fixes
[AB#537482](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/537482)
Fixes #26487

---------

Co-authored-by: jaymckinney <[email protected]>
  • Loading branch information
jaymckinney-enavate and jaymckinney authored Jun 6, 2024
1 parent 711c59c commit 5d9a4d3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,14 @@ codeunit 4019 "GP Item Migrator"
TempTrackingSpecification: Record "Tracking Specification" temporary;
DataMigrationErrorLogging: Codeunit "Data Migration Error Logging";
CreateReserveEntry: Codeunit "Create Reserv. Entry";
ItemJnlLineReserve: Codeunit "Item Jnl. Line-Reserve";
ExpirationDate: Date;
begin
if GPItem.ItemTrackingCode = '' then
exit;

DataMigrationErrorLogging.SetLastRecordUnderProcessing(Format(GPItemTransactions.RecordId));

ItemJnlLineReserve.InitFromItemJnlLine(TempTrackingSpecification, ItemJnlLine);
TempTrackingSpecification.InitFromItemJnlLine(ItemJnlLine);

if GPItemTransactions.ExpirationDate = DMY2Date(1, 1, 1900) then
ExpirationDate := 0D
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ codeunit 4037 "Helper Functions"
CloudMigrationTok: Label 'CloudMigration', Locked = true;
GeneralTemplateNameTxt: Label 'GENERAL', Locked = true;
NotAllJournalLinesPostedMsg: Label 'Not all journal lines were posted. Number of unposted lines - %1.', Comment = '%1 Number of unposted lines';
MigrationLogAreaBatchPostingTxt: Label 'Batch Posting', Locked = true;

procedure GetTextFromJToken(JToken: JsonToken; Path: Text): Text
var
Expand Down Expand Up @@ -1241,7 +1242,7 @@ codeunit 4037 "Helper Functions"
ItemJournalLine.SetRange("Journal Batch Name", ItemJournalBatch.Name);
ItemJournalLine.SetFilter("Item No.", '<>%1', '');
if not ItemJournalLine.IsEmpty() then
PostItemBatch(ItemJournalBatch);
SafePostItemBatch(ItemJournalBatch);
until ItemJournalBatch.Next() = 0;

// Account batches
Expand All @@ -1260,7 +1261,7 @@ codeunit 4037 "Helper Functions"
GenJournalLine.SetRange("Journal Template Name", GeneralTemplateNameTxt);
GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if not GenJournalLine.IsEmpty() then
PostGLBatch(CopyStr(JournalBatchName, 1, 10));
SafePostGLBatch(CopyStr(JournalBatchName, 1, 10));
end;
until GenJournalBatch.Next() = 0;

Expand All @@ -1270,7 +1271,7 @@ codeunit 4037 "Helper Functions"
repeat
StatisticalAccJournalLine.SetRange("Journal Batch Name", StatisticalAccJournalBatch.Name);
if not StatisticalAccJournalLine.IsEmpty() then
PostStatisticalAccBatch(StatisticalAccJournalBatch.Name);
SafePostStatisticalAccBatch(StatisticalAccJournalBatch.Name);
until StatisticalAccJournalBatch.Next() = 0;
end;

Expand All @@ -1283,7 +1284,7 @@ codeunit 4037 "Helper Functions"
GenJournalLine.SetRange("Journal Template Name", GeneralTemplateNameTxt);
GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if not GenJournalLine.IsEmpty() then
PostGLBatch(CopyStr(JournalBatchName, 1, 10));
SafePostGLBatch(CopyStr(JournalBatchName, 1, 10));
end;

// Vendor batches
Expand All @@ -1295,7 +1296,7 @@ codeunit 4037 "Helper Functions"
GenJournalLine.SetRange("Journal Template Name", GeneralTemplateNameTxt);
GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if not GenJournalLine.IsEmpty() then
PostGLBatch(CopyStr(JournalBatchName, 1, 10));
SafePostGLBatch(CopyStr(JournalBatchName, 1, 10));
end;

// Bank batches
Expand All @@ -1307,7 +1308,7 @@ codeunit 4037 "Helper Functions"
GenJournalLine.SetRange("Journal Template Name", GeneralTemplateNameTxt);
GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if not GenJournalLine.IsEmpty() then
PostGLBatch(CopyStr(JournalBatchName, 1, 10));
SafePostGLBatch(CopyStr(JournalBatchName, 1, 10));
end;

// Remove posted batches
Expand All @@ -1316,6 +1317,7 @@ codeunit 4037 "Helper Functions"
Session.LogMessage('00007GK', StrSubstNo(FinishedTelemetryTxt, DurationAsInt), Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, 'Category', GetTelemetryCategory());
end;

[Obsolete('This procedure will be soon removed.', '26.0')]
procedure PostGLBatch(JournalBatchName: Code[10])
var
GenJournalLine: Record "Gen. Journal Line";
Expand All @@ -1342,6 +1344,21 @@ codeunit 4037 "Helper Functions"
codeunit.Run(codeunit::"Gen. Jnl.-Post Batch", GenJournalLine);
end;

local procedure SafePostGLBatch(JournalBatchName: Code[10])
var
GenJournalLine: Record "Gen. Journal Line";
begin
GenJournalLine.SetRange("Journal Template Name", GeneralTemplateNameTxt);
GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if GenJournalLine.FindFirst() then begin
// Commit is required to safely handle errors that may occur during posting.
Commit();
if not Codeunit.Run(Codeunit::"Gen. Jnl.-Post Batch", GenJournalLine) then
LogWarningAndClearLastError(JournalBatchName);
end;
end;

[Obsolete('This procedure will be soon removed.', '26.0')]
procedure PostStatisticalAccBatch(JournalBatchName: Code[10])
var
StatisticalAccJournalLine: Record "Statistical Acc. Journal Line";
Expand All @@ -1351,14 +1368,41 @@ codeunit 4037 "Helper Functions"
Codeunit.Run(Codeunit::"Stat. Acc. Post. Batch", StatisticalAccJournalLine);
end;

local procedure PostItemBatch(ItemJournalBatch: Record "Item Journal Batch")
local procedure SafePostStatisticalAccBatch(JournalBatchName: Code[10])
var
StatisticalAccJournalLine: Record "Statistical Acc. Journal Line";
begin
StatisticalAccJournalLine.SetRange("Journal Batch Name", JournalBatchName);
if StatisticalAccJournalLine.FindFirst() then begin
// Commit is required to safely handle errors that may occur during posting.
Commit();
if not Codeunit.Run(Codeunit::"Stat. Acc. Post. Batch", StatisticalAccJournalLine) then
LogWarningAndClearLastError(JournalBatchName);
end;
end;

local procedure SafePostItemBatch(ItemJournalBatch: Record "Item Journal Batch")
var
ItemJournalLine: Record "Item Journal Line";
begin
ItemJournalLine.SetRange("Journal Template Name", ItemJournalBatch."Journal Template Name");
ItemJournalLine.SetRange("Journal Batch Name", ItemJournalBatch.Name);
if ItemJournalLine.FindFirst() then
Codeunit.Run(Codeunit::"Item Jnl.-Post Batch", ItemJournalLine);
if ItemJournalLine.FindFirst() then begin
// Commit is required to safely handle errors that may occur during posting.
Commit();
if not Codeunit.Run(Codeunit::"Item Jnl.-Post Batch", ItemJournalLine) then
LogWarningAndClearLastError(ItemJournalBatch.Name);
end;
end;

internal procedure LogWarningAndClearLastError(ContextValue: Text[50])
var
GPMigrationWarnings: Record "GP Migration Warnings";
WarningText: Text[500];
begin
WarningText := CopyStr(GetLastErrorText(false), 1, MaxStrLen(WarningText));
GPMigrationWarnings.InsertWarning(MigrationLogAreaBatchPostingTxt, ContextValue, WarningText);
ClearLastError();
end;

local procedure GLBatchHasLines(TemplateName: Code[10]; BatchName: Code[10]; AccountType: Enum "Gen. Journal Account Type"): Boolean
Expand Down

0 comments on commit 5d9a4d3

Please sign in to comment.