diff --git a/src/Apps/W1/Intrastat/app/src/IntrastatReport/IntrastatReportLine.Table.al b/src/Apps/W1/Intrastat/app/src/IntrastatReport/IntrastatReportLine.Table.al index 99f1b5b4264..bcf3507ec5b 100644 --- a/src/Apps/W1/Intrastat/app/src/IntrastatReport/IntrastatReportLine.Table.al +++ b/src/Apps/W1/Intrastat/app/src/IntrastatReport/IntrastatReportLine.Table.al @@ -94,6 +94,16 @@ table 4812 "Intrastat Report Line" Caption = 'Country/Region Code'; TableRelation = "Country/Region"; ToolTip = 'Specifies the country/region of the address.'; + trigger OnValidate() + var + IntrastatReportMgt: Codeunit IntrastatReportManagement; + begin + if "Country/Region Code" = '' then begin + Validate("Intrastat Country/Region Code", ''); + exit; + end; + + Validate("Intrastat Country/Region Code", IntrastatReportMgt.GetIntrastatCodeFromCountryRegion("Country/Region Code")); } field(8; "Transaction Type"; Code[10]) { diff --git a/src/Apps/W1/Intrastat/test/src/IntrastatReportTest.Codeunit.al b/src/Apps/W1/Intrastat/test/src/IntrastatReportTest.Codeunit.al index 6b482773294..47d286839af 100644 --- a/src/Apps/W1/Intrastat/test/src/IntrastatReportTest.Codeunit.al +++ b/src/Apps/W1/Intrastat/test/src/IntrastatReportTest.Codeunit.al @@ -4829,6 +4829,31 @@ codeunit 139550 "Intrastat Report Test" Assert.AreEqual(TariffNo."No.", ItemTempl."Tariff No.", ''); end; + [Test] + procedure ManualIntrastatReportLineUsesIntrastatCode() + var + CountryRegion: array[2] of Record "Country/Region"; + IntrastatReportLine: Record "Intrastat Report Line"; + begin + // [SCENARIO 646599] Intrastat Country/Region Code is populated from the Country/Region's Intrastat Code when a line is added manually + Initialize(); + + // [GIVEN] Create Country/Region with a different Intrastat Code. + LibraryIntrastat.CreateCountryRegion(CountryRegion[1], true); + LibraryIntrastat.CreateCountryRegion(CountryRegion[2], true); + CountryRegion[1].Validate(CountryRegion[1]."Intrastat Code", CountryRegion[2].Code); + CountryRegion[1].Modify(true); + + // [GIVEN] An Intrastat Report with a manually added line + LibraryIntrastat.CreateIntrastatReportLine(IntrastatReportLine); + + // [WHEN] The Country/Region Code is validated on the line + IntrastatReportLine.Validate("Country/Region Code", CountryRegion[1].Code); + + // [THEN] The Intrastat Country/Region Code inherits the Intrastat Code from the Country/Region + IntrastatReportLine.TestField("Intrastat Country/Region Code", CountryRegion[2].Code); + end; + local procedure Initialize() var LibraryERMCountryData: Codeunit "Library - ERM Country Data";