Insert 4 dimensions how to improve code

samantha73
Member Posts: 118
Hi Guys
I have the below code to enter shortcut dim 1 to 4:
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?
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?
0
Best 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!
0
Answers
-
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!
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions