{ [EventSubscriber(ObjectType::Table, Database::"Sales Header", OnAfterLookupShipToPostCode, '', false, false)] local procedure OnafterLookup(var SalesHeader: Record "Sales Header")
begin Rec := SalesHeader; Message('OnAfterUpdateEvent');//debug if Rec.IsTemporary then Exit; //todo check SO status open GenLedgerSetup.Get(); Dim3Code := GenLedgerSetup."Shortcut Dimension 3 Code"; //Postcode lookup from custom table IsConversionSuccessful := Evaluate(ShipToPostCodeInt, Rec."Ship-to Post Code"); if not IsConversionSuccessful then Error('Post code value "%1" cannot be converted to an integer. Check Post code.', Rec."Ship-to Post Code"); //Find Territory based on post code PostCodeTerritory.Reset(); PostCodeTerritory.SetFilter(StartPostCode, '<=%1', ShipToPostCodeInt); PostCodeTerritory.SetFilter(EndPostCode, '>=%1', ShipToPostCodeInt); if PostCodeTerritory.FindFirst() then begin Message('Territory code found for Post code %1 and territory %2', ShipToPostCodeInt, PostCodeTerritory.TerritoryID); Dim3Value := PostCodeTerritory.TerritoryID end else Error('Territory code not found for Post code %1', Rec."Ship-to Post Code"); //Dim3 territory DimSetEntryTemp.init(); DimSetEntryTemp.validate("Dimension Code", Dim3Code); DimSetEntryTemp.validate("Dimension Value Code", Dim3Value); DimSetEntryTemp.Insert(); UpdateDimSetOnSalesHeader(Rec, DimSetEntryTemp); Rec.Modify() end; procedure UpdateDimSetOnSalesHeader(var Header: Record "Sales Header"; var ToAddDims: Record "Dimension Set Entry" temporary) var DimMgt: Codeunit DimensionManagement; NewDimSet: Record "Dimension Set Entry" temporary; begin DimMgt.GetDimensionSet(NewDimSet, header."Dimension Set ID"); if ToAddDims.FindSet() then repeat if NewDimSet.Get(header."Dimension Set ID", ToAddDims."Dimension Code") then begin NewDimSet.validate("Dimension Value Code", ToAddDims."Dimension Value Code"); NewDimSet.Modify(); end else begin NewDimSet := ToAddDims; NewDimSet."Dimension Set ID" := Header."Dimension Set ID"; NewDimSet.Insert(); end; until ToAddDims.Next() = 0; Header."Dimension Set ID" := DimMgt.GetDimensionSetID(NewDimSet); DimMgt.UpdateGlobalDimFromDimSetID(Header."Dimension Set ID", Header."Shortcut Dimension 1 Code", Header."Shortcut Dimension 2 Code"); end; }
Answers