To set record variable as temporary
Below is the example
Dimensionsetentrytemp: Record "Dimension Set Entry" Temporary;
Else it will insert directly into table.
Create new codeunit and add this subscriber
[EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnaftervalidateEvent', 'Sell-to Customer No.', false, false)]
local procedure OnafterValidateSellToCustomerNo(var Rec: Record "Sales Header";var xrec :Record "Sales Header")
Var
DimensionSetEntry: record ”dimension set entry";
Dimensionsetentrytemp: Record "Dimension Set Entry" Temporary;
DimMgmt : codeunit dimensionmanagement;
ProjectDimVal : code[10];
DimCode : code[10];
Begin
Dimensionsetentrytemp.deleteall();//This will clear the temptable(table will be empty)
DimensionSetEntry.Get( xRec."Dimension Set ID", 'Project');
ProjectDimVal := DimensionSetEntry."Dimension Value Code";
DimCode := 'Project';
DimensionSetEntry.reset(); DimensionSetEntry.SetRange("Dimension set id", rec."dimension set id");
DimensionSetEntry.setfilter("Dimension Code" ,'<>%1', DimCode);
if DimensionSetEntry.findset() then
repeat
DimensionValue.get(DimensionSetEntry."Dimension code",DimensionSetEntry."Dimension value code");
DimensionSetEntryTmp.Init();
DimensionSetEntryTmp.Validate("Dimension Code", DimensionSetEntry."Dimension Code");
DimensionSetEntryTmp.Validate("Dimension Value Code", DimensionSetEntry."Dimension Value Code");
DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";
DimensionSetEntryTmp.Insert();
until DimensionSetEntry.Next = 0;
DimensionValue.Get('Project', ProjectDimVal);
DimensionSetEntryTmp.Init();
DimensionSetEntryTmp.Validate("Dimension Code",DimensionValue."Dimension code" );
DimensionSetEntryTmp.Validate("Dimension Value Code", ProjectDimVal);
DimensionSetEntryTmp."Dimension Value ID" := DimensionValue."Dimension Value ID";
DimensionSetEntryTmp.Insert();
Rec."Dimension Set ID" := DimMgmt.GetDimensionSetID(DimensionSetEntryTmp);
Rec.Modify();
End;