RE: Modify Dimension Set Entry table through code
Thank you for helpful tips. Problem solved using CodeUnit "DimensionManagement"!
I had problem, that after adding new Dimension and changing SalesLines."Dimension Set ID" I was losing rest dimensions on that line.
Solution was to use: DimManagement.GetDimensionSet(TempDimSetEntry, SalesLines."Dimension Set ID");
to store all dimensions.
This topic was helpful: community.dynamics.com/.../801556
My actual code:
codeunit 50101 "Before Post Validation"
{
Permissions = tabledata "Dimension Set Entry" = RM;
[EventSubscriber(ObjectType::CodeUnit, Codeunit::"Sales-Post (Yes/No)", 'OnBeforeConfirmSalesPost', '', false, false)]
local procedure OnBeforeConfirmSalesPost(var SalesHeader: Record "Sales Header"; var HideDialog: Boolean; var IsHandled: Boolean; var DefaultOption: Integer; var PostAndSend: Boolean)
var
SalesLines: Record "Sales Line";
Dimensions: Record "Dimension Set Entry";
TempDimSetEntry: Record "Dimension Set Entry" temporary;
DimManagement: Codeunit DimensionManagement;
begin
SalesLines.SetRange("Document No.", SalesHeader."No.");
SalesLines.SetRange("Document Type", SalesHeader."Document Type");
SalesLines.SetRange(Type, "Sales Line Type"::"G/L Account");
SalesLines.SetRange("No.", '1234');
if SalesLines.FindSet() then begin
repeat
Dimensions.SetRange("Dimension Set ID", SalesLines."Dimension Set ID");
Dimensions.SetRange("Dimension Code", 'SPECIFICCODE');
if Dimensions.Find('-') then
if (Dimensions."Dimension Value Code" = '123')
or (Dimensions."Dimension Value Code" = '1234') then
begin
DimManagement.GetDimensionSet(TempDimSetEntry, SalesLines."Dimension Set ID");
TempDimSetEntry.Init();
TempDimSetEntry.Validate("Dimension Code", 'MYCODE');
TempDimSetEntry.Validate("Dimension Value Code", 'MYVALUE');
if not TempDimSetEntry.Insert then
TempDimSetEntry.Modify;
SalesLines.Validate("Dimension Set ID", DimManagement.GetDimensionSetID(TempDimSetEntry));
SalesLines.Modify;
end;
until SalesLines.Next() = 0;
end;
end;
}