Options

Business Central - purchase line - update shortcut/global dimensions

Good day,

I am looking for a hint on how to update shortcut dimension codes on a purchase line, after an update to line dimension values.

Here is the code I use to update the purchase line dimensions with values from external system:
procedure AssignLineDimension(DocumentType: Integer; DocumentNumber: Code[20]; LineNo: Integer; DimensionCode: Code[20]; DimensionValue: Code[20]);
    var
        TempDimSetEntry: Record "Dimension Set Entry" temporary;
        PurchaseLine: Record "Purchase Line";
        DimMgt: Codeunit DimensionManagement;
        NewDimSetID: Integer;
        OldDimSetID: Integer;
    begin
        PurchaseLine.Get(DocumentType, DocumentNumber, LineNo);

        //obtain current line dimension set id and its dimensions
        OldDimSetID := PurchaseLine."Dimension Set ID";
        DimMgt.GetDimensionSet(TempDimSetEntry, OldDimSetID);

        //assign new/update existing dimension with data from external system
        TempDimSetEntry.Reset();
        TempDimSetEntry.SetRange("Dimension Code", DimensionCode);
        if TempDimSetEntry.FindFirst() then begin
            TempDimSetEntry.Validate("Dimension Value Code", DimensionValue);
            TempDimSetEntry.Modify();
        end

        else begin
            TempDimSetEntry.Init();
            TempDimSetEntry.Validate("Dimension Code", DimensionCode);
            TempDimSetEntry.Validate("Dimension Value Code", DimensionValue);
            TempDimSetEntry.Insert();
        end;

        //obtain DimSetID after line dimension update
        NewDimSetID := DimMgt.GetDimensionSetID(TempDimSetEntry);

        //update line dimension set id 
        if OldDimSetID <> NewDimSetID then begin
            PurchaseLine."Dimension Set ID" := NewDimSetID;
            PurchaseLine.Modify();
        end;

        //update line's global dimensions
        DimMgt.UpdateGlobalDimFromDimSetID(PurchaseLine."Dimension Set ID", PurchaseLine."Shortcut Dimension 1 Code", PurchaseLine."Shortcut Dimension 2 Code");        
    end;

It works fine - line dimension values are updated by values from external system, but the shortcut dimension values remain blank.

Is it a call to DimMgt.ValidateShortcutDimValues that I am missing?
If so, what is the FieldNumber parameter? Is it the number of the shorcut/global dimension in the gl setup?

Thank you.

Best Answer

  • Options
    DmitriySozinovDmitriySozinov Member Posts: 23
    Answer ✓
    Adding this code made the trick:
            ...
            //update line's global dimensions
            GLSetup.Get();
            if DimensionCode = GLSetup."Global Dimension 1 Code" then begin
                PurchaseLine.Validate("Shortcut Dimension 1 Code", DimensionValue);
                PurchaseLine.Modify();
            end;
    
            if DimensionCode = GLSetup."Global Dimension 2 Code" then begin
                PurchaseLine.Validate("Shortcut Dimension 2 Code", DimensionValue);
                PurchaseLine.Modify();
            end;
    
           //obtain DimSetID after line dimension update
           ...
    

    This way, the onValidate trigger of the table field handles the call to DimMgt.ValidateShortcutDimValues

Answers

  • Options
    PhoguePhogue Member Posts: 76
    edited 2020-07-20
    "FieldNumber" is a reference to the number the dimension code has in GL Setup.
    Dimension 1 = field 1 and so on

    It's used to point the dimension value to the correct dimension code
    If so, what is the FieldNumber parameter? Is it the number of the shorcut/global dimension in the gl setup?
    That is correct
  • Options
    DmitriySozinovDmitriySozinov Member Posts: 23
    Answer ✓
    Adding this code made the trick:
            ...
            //update line's global dimensions
            GLSetup.Get();
            if DimensionCode = GLSetup."Global Dimension 1 Code" then begin
                PurchaseLine.Validate("Shortcut Dimension 1 Code", DimensionValue);
                PurchaseLine.Modify();
            end;
    
            if DimensionCode = GLSetup."Global Dimension 2 Code" then begin
                PurchaseLine.Validate("Shortcut Dimension 2 Code", DimensionValue);
                PurchaseLine.Modify();
            end;
    
           //obtain DimSetID after line dimension update
           ...
    

    This way, the onValidate trigger of the table field handles the call to DimMgt.ValidateShortcutDimValues
Sign In or Register to comment.