Insert 4 dimensions how to improve code

samantha73samantha73 Member Posts: 106
Hi Guys
I have the below code to enter shortcut dim 1 to 4:
actions
    {
        addfirst(processing)
        {
            action(UpdateSalesHeaderDimensions)
            {
                Caption = 'Test Update Sales Header Dimensions';
                ApplicationArea = all;
                trigger OnAction()
                var
                    DimVal: Record "Dimension Value";
                    TempDimSetEntry: Record "Dimension Set Entry" temporary;
                    DimMgt: Codeunit DimensionManagement;
                begin
                    // Dimension 1 (Location)
                    DimVal.GET('LOCATION DIM', 'MEL');
                    TempDimSetEntry.INIT();
                    TempDimSetEntry.VALIDATE("Dimension Code", DimVal."Dimension Code");
                    TempDimSetEntry.VALIDATE("Dimension Value Code", DimVal.CODE);
                    TempDimSetEntry."Dimension Value ID" := DimVal."Dimension Value ID";
                    TempDimSetEntry.INSERT();

                    // Dimension 2 (Department)
                    DimVal.GET('DEPARTMENT', 'SERVICE');
                    TempDimSetEntry.INIT();
                    TempDimSetEntry.VALIDATE("Dimension Code", DimVal."Dimension Code");
                    TempDimSetEntry.VALIDATE("Dimension Value Code", DimVal.CODE);
                    TempDimSetEntry."Dimension Value ID" := DimVal."Dimension Value ID";
                    TempDimSetEntry.INSERT();

                    // Dimension 3 (Area)
                    DimVal.GET('AREA', '30');
                    TempDimSetEntry.INIT();
                    TempDimSetEntry.VALIDATE("Dimension Code", DimVal."Dimension Code");
                    TempDimSetEntry.VALIDATE("Dimension Value Code", DimVal.CODE);
                    TempDimSetEntry."Dimension Value ID" := DimVal."Dimension Value ID";
                    TempDimSetEntry.INSERT();

                    // Dimension 4 (salesperson)
                    DimVal.GET('SALESPERSON', 'JO');
                    TempDimSetEntry.INIT();
                    TempDimSetEntry.VALIDATE("Dimension Code", DimVal."Dimension Code");
                    TempDimSetEntry.VALIDATE("Dimension Value Code", DimVal.CODE);
                    TempDimSetEntry."Dimension Value ID" := DimVal."Dimension Value ID";
                    TempDimSetEntry.INSERT();

                    Rec."Dimension Set ID" := DimMgt.GetDimensionSetID(TempDimSetEntry);
                    Rec.Modify(true);
                end;
            }
        }
    }

}

Is there a better way to handle this with shorter code? Also instead of passing dimension code like 'AREA' can we say shortcutdimension3code so if someone change the name it wont break?

Best Answer

  • vaprogvaprog Member Posts: 1,140
    Answer ✓
    samantha73 wrote: »
    Is there a better way to handle this with shorter code?

    You should likely use SalesHeader.CreateDim, or even make provisions that SalesHeader.CreateDimFromDefaultDim does the right thing anyway.

    If you must use GetDimensionSetID for any compelling reason you SHOULD
    • use DimMgt.GetDimensionSet to initialize your TempDimSetEntry. If you don't, you will loose any dimensions not covered in your own code.
    • Use setup tables for both, the dimension codes, and the dimensions values to set. NEVER use codes from table fields hardcoded in your code.
    • If you created a setup, you will have a loop anyaway and this list item is obsolete. Else create a function to set up a TempDimSetEntry. Take a look at "G/L Budget Entry".OnInsert.
    • Validate "Dimension Set ID"
    samantha73 wrote: »
    Also instead of passing dimension code like 'AREA' can we say shortcutdimension3code so if someone change the name it wont break?

    You can configure which shortcut dimensions correspond to which dimensions. So switching from dimension code to shortcut dimension number probably makes things worse, not better, because renaming a dimension is handled transparently thorougout the system, but re-assigning shortcut dimensions is not. Moreover, your requirement to update certaing dimension values probably is tied to the dimension, and neither to the dimension code, nor to the assignment of that dimension to a global dimension or shortcut dimension. Use a Setup!

Answers

  • vaprogvaprog Member Posts: 1,140
    Answer ✓
    samantha73 wrote: »
    Is there a better way to handle this with shorter code?

    You should likely use SalesHeader.CreateDim, or even make provisions that SalesHeader.CreateDimFromDefaultDim does the right thing anyway.

    If you must use GetDimensionSetID for any compelling reason you SHOULD
    • use DimMgt.GetDimensionSet to initialize your TempDimSetEntry. If you don't, you will loose any dimensions not covered in your own code.
    • Use setup tables for both, the dimension codes, and the dimensions values to set. NEVER use codes from table fields hardcoded in your code.
    • If you created a setup, you will have a loop anyaway and this list item is obsolete. Else create a function to set up a TempDimSetEntry. Take a look at "G/L Budget Entry".OnInsert.
    • Validate "Dimension Set ID"
    samantha73 wrote: »
    Also instead of passing dimension code like 'AREA' can we say shortcutdimension3code so if someone change the name it wont break?

    You can configure which shortcut dimensions correspond to which dimensions. So switching from dimension code to shortcut dimension number probably makes things worse, not better, because renaming a dimension is handled transparently thorougout the system, but re-assigning shortcut dimensions is not. Moreover, your requirement to update certaing dimension values probably is tied to the dimension, and neither to the dimension code, nor to the assignment of that dimension to a global dimension or shortcut dimension. Use a Setup!

Sign In or Register to comment.