Sales Header record cannot be saved because some information on the page is not up-to-date. PostCode

samantha73samantha73 Member Posts: 104
Hi All
I'm trying to find a way use the shipto address postcode to lookup a custom table to get the dimensions. The post code field doesnot trigger OnAfterValidate. So I use below event subscriber
{
    [EventSubscriber(ObjectType::Table, Database::"Sales Header", OnAfterLookupShipToPostCode, '', false, false)]

    local procedure OnafterLookup(var SalesHeader: Record "Sales Header")

However when running the code error comes on page as below

Sales Header record cannot be saved because some information on the page is not up-to-date. Close the page, reopen it, and try again

Is there a better way to capture postcode initial insert and subsequent update events so I can use the postcode for updating another field - say dimensions? Below is the code:
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;
}
Sign In or Register to comment.