Skip to content

Commit

Permalink
Prevent duplicate record in the Master Data Management - Synchronizat…
Browse files Browse the repository at this point in the history
…ion Tables list (#26133)

Resolves #26105 

The loop adding related tables to the synchronization list is triggered
before the initial table requested by the user is added, therefore it
will be added to the list of related tables if any of the relations
points back to the table itself. An additional check is added in the
loop precondition to handle this case.




Fixes
[AB#534785](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/534785)
  • Loading branch information
adrogin authored May 21, 2024
1 parent 65f76c1 commit 1845570
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Apps/W1/MasterDataManagement/app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"name": "_Exclude_Master_Data_Management_Test_Library",
"publisher": "Microsoft"
}
],
],
"screenshots": [

],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,12 @@ page 7233 "Master Data Synch. Tables"
}
}

local procedure FindRelatedTables(var ExistingSynchTableNos: List of [Integer]; var RelatedTablesToAdd: List of [Integer]; var RelatedTablesToAddText: Text; TableId: Integer)
internal procedure FindRelatedTables(var ExistingSynchTableNos: List of [Integer]; var RelatedTablesToAdd: List of [Integer]; var RelatedTablesToAddText: Text; TableId: Integer)
begin
FindRelatedTables(ExistingSynchTableNos, RelatedTablesToAdd, RelatedTablesToAddText, TableId, TableId);
end;

local procedure FindRelatedTables(var ExistingSynchTableNos: List of [Integer]; var RelatedTablesToAdd: List of [Integer]; var RelatedTablesToAddText: Text; TableId: Integer; TopLevelTableId: Integer)
var
Field: Record Field;
TableMetadata: Record "Table Metadata";
Expand All @@ -531,7 +536,7 @@ page 7233 "Master Data Synch. Tables"
exit;

repeat
if not (ExistingSynchTableNos.Contains(Field.RelationTableNo) or RelatedTablesToAdd.Contains(Field.RelationTableNo)) then
if not ((Field.RelationTableNo = TopLevelTableId) or ExistingSynchTableNos.Contains(Field.RelationTableNo) or RelatedTablesToAdd.Contains(Field.RelationTableNo)) then
if TableMetadata.Get(Field.RelationTableNo) then
if (TableMetadata.TableType = TableMetadata.TableType::Normal) and TableMetadata.DataPerCompany then begin
RecRef.Open(Field.RelationTableNo);
Expand All @@ -541,7 +546,7 @@ page 7233 "Master Data Synch. Tables"
RelatedTablesToAddText := TableMetadata.Name
else
RelatedTablesToAddText += ', ' + TableMetadata.Name;
FindRelatedTables(ExistingSynchTableNos, RelatedTablesToAdd, RelatedTablesToAddText, Field.RelationTableNo);
FindRelatedTables(ExistingSynchTableNos, RelatedTablesToAdd, RelatedTablesToAddText, Field.RelationTableNo, TopLevelTableId);
end;
RecRef.Close();
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ codeunit 139757 "Library - Master Data Mgt."
MasterDataMgtSubscribers.HandleOnAfterJobQueueEntryRun(JobQueueEntry);
end;

procedure FindRelatedTables(var ExistingSynchTableNos: List of [Integer]; var RelatedTablesToAdd: List of [Integer]; var RelatedTablesToAddText: Text; TableId: Integer)
var
MasterDataSynchTables: Page "Master Data Synch. Tables";
begin
MasterDataSynchTables.FindRelatedTables(ExistingSynchTableNos, RelatedTablesToAdd, RelatedTablesToAddText, TableId);
end;

var
MasterDataMgtSubscribers: Codeunit "Master Data Mgt. Subscribers";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
table 139757 "MDM Test Table A"
{
DataClassification = SystemMetadata;

fields
{
field(1; "Primary Key"; Code[20])
{
Caption = 'Primary Key';
}
field(2; "Self-Reference Field"; Code[20])
{
Caption = 'Self-Reference Field';
TableRelation = "MDM Test Table A"."Primary Key";
}
field(3; "TableB Reference"; Code[20])
{
Caption = 'TableB Reference';
TableRelation = "MDM Test Table B"."Primary Key";
}
}

keys
{
key(PK; "Primary Key")
{
Clustered = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
table 139758 "MDM Test Table B"
{
DataClassification = SystemMetadata;

fields
{
field(1; "Primary Key"; Code[20])
{
Caption = 'Primary Key';
}
field(3; "TableA Reference"; Code[20])
{
Caption = 'TableA Reference';
TableRelation = "MDM Test Table A"."Primary Key";
}
}

keys
{
key(PK; "Primary Key")
{
Clustered = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ codeunit 139770 "Master Data Mgt. Setup Tests"
LibrarySetupStorage: Codeunit "Library - Setup Storage";
LibraryRandom: Codeunit "Library - Random";
LibraryVariableStorage: Codeunit "Library - Variable Storage";
LibraryMasterDataMgt: Codeunit "Library - Master Data Mgt.";
InitializeHandled: Boolean;

[Test]
Expand Down Expand Up @@ -323,6 +324,24 @@ codeunit 139770 "Master Data Mgt. Setup Tests"
Assert.AreEqual(0, MasterDataMgtCoupling.Count(), '');
end;

[Test]
procedure NoDuplicatesAddedToSetupOnSelectingTableWithCyclicReference()
var
SynchTables: List of [Integer];
RelatedTablesToAdd: List of [Integer];
TablesToAddText: Text;
IncorrectTablesListErr: Label 'Synchronization tables list is incorrect.';
begin
// [SCENARIO] When selecting a table that has a self-reference or other reference that create a cycle, duplicate records are not added to the setup list

Initialize();

LibraryMasterDataMgt.FindRelatedTables(SynchTables, RelatedTablesToAdd, TablesToAddText, Database::"MDM Test Table A");

Assert.AreEqual(1, RelatedTablesToAdd.Count, IncorrectTablesListErr);
Assert.IsTrue(RelatedTablesToAdd.Contains(Database::"MDM Test Table B"), IncorrectTablesListErr);
end;

local procedure Initialize()
var
MasterDataManagementSetup: Record "Master Data Management Setup";
Expand Down

0 comments on commit 1845570

Please sign in to comment.