Better way to insert Dimension Set ID from AL code

samantha73samantha73 Member Posts: 106
Hi All
I am developing an extension to import invoices from an excel file and the excel file contains columns for dimensions. Now I can get this to work but the code is long and looks inefficient. Is there a better way to handle this than below
which is based on blog
https://robertostefanettinavblog.com/2017/06/26/create-new-dimension-id-to-import-dimension-values-from-buffer-table/:


//*dim 1-8
//1***
DimVal.reset;

DimVal.SETFILTER("Dimension Code", '=%1', 'PRODUCT GROUP');
DimVal.SETFILTER(Code, '=%1', SalesImportLine.U_ShortcutDimension1Code);
IF DimVal.FINDFIRST THEN BEGIN
Dim1ValID := DimVal."Dimension Value ID";

TempDimSetEntry.RESET;
TempDimSetEntry."Dimension Code" := 'PRODUCT GROUP';
TempDimSetEntry."Dimension Value Code" := SalesImportLine.U_ShortcutDimension1Code;
TempDimSetEntry."Dimension Value ID" := Dim1ValID;
TempDimSetEntry.INSERT;
end;
//2****
DimVal.reset;
DimVal.SETFILTER("Dimension Code", '=%1', 'CUSTOMER GROUP');
DimVal.SETFILTER(Code, '=%1', SalesImportLine.U_ShortcutDimension2Code);
IF DimVal.FINDFIRST THEN BEGIN
Dim2ValID := DimVal."Dimension Value ID";

TempDimSetEntry.RESET;

TempDimSetEntry."Dimension Code" := 'CUSTOMER GROUP';
TempDimSetEntry."Dimension Value Code" := SalesImportLine.U_ShortcutDimension2Code;
TempDimSetEntry."Dimension Value ID" := Dim2ValID;
TempDimSetEntry.INSERT;
end;jtegiodoi2wt.png

Answers

  • lubostlubost Member Posts: 623
    Simply validating Shortcut(Global)DimensionX field does the trick.
  • AudriusAudrius Member Posts: 5
    edited 2021-07-29
    Hey i think this will work for you.


    DimensionTBL - Dimension table
    DimVal - Dimension Value table

    DimensionTBL.RESET; (I think its not lot dimensions in that table)
    DimVal.RESET;
    InsertedTRUE:=FALSE; //--You can use it or not(if you inserted you dont need look other dimensions, avoidong loop to end)

    IF DimensionTBL.FIND('-') THEN
    REPEAT
    DimVal.SETRANGE("Dimension Code",DimensionTBL.Code); //Filter all Dimension withhout HARDCODE (best to avoid hardcoding)
    DimVal.SETFILTER(Code, '=%1', SalesImportLine.U_ShortcutDimension1Code);
    IF DimVal.FINDFIRST THEN BEGIN
    Dim1ValID := DimVal."Dimension Value ID";
    TempDimSetEntry.RESET;
    TempDimSetEntry."Dimension Code" := DimensionTBL.Code; //Dimension Code
    TempDimSetEntry."Dimension Value Code" := SalesImportLine.U_ShortcutDimension1Code;
    TempDimSetEntry."Dimension Value ID" := Dim1ValID;
    TempDimSetEntry.INSERT;
    InsertedTRUE:=TRUE; //-- it's fuse for loop
    end;

    UNTIL (DimensionTBL.NEXT=0) OR (InsertedTRUE = TRUE);

    74zgc6co54xy.png
  • samantha73samantha73 Member Posts: 106
    Thanks, heaps for the suggestion and I could get ideas on a better way to manage Dim values. Now for the code itself , I don't think BC has the method
    InsertedTRUE. could be NAV only feature

    I have recoded without HARDCODING By looking at Gen Led Setup to get dim 1-7. And maybe what I need is a separate code unit to manage dims so it can be triggered from any other code unit ..reusable code
Sign In or Register to comment.