Rename records on tables

bekiobekio Member Posts: 204
Hello everyone,

I want to rename records on Product Group table, field code(that is part of primary key), and i can not do it. I have made a codeunit with code:
ProductGroup.RESET;
ProductGroup.INIT;
IF ProductGroup.FINDFIRST THEN
REPEAT
ProductGroup.Code:= ProductGroup."Item Category Code" +'/' +ProductGroup.Code;
ProductGroup.MODIFY;
UNTIL ProductGroup.NEXT=0;

But when i execute it it stops on the firs record displaying a error:
The Product Group does not exist.
Identification fields and values:
Item Category Code= xxx, Code= xxx/yyy;

Any suggestion how to do this, with function because i have a lot of records there.

Thanks in advice!
:!:

Answers

  • FelixAntonyFelixAntony Member Posts: 6
    You cannot use MODIFY command to modify a primary key fields.

    Instead use RENAME.

    See the code below;

    ProductGroup.RESET;
    IF ProductGroup.FINDFIRST THEN
    REPEAT
    if ProductGroup2.get(ProductGroup."Item Category Code",ProductGroup.Code) then
    ProductGroup2.Rename("Item Category Code",ProductGroup."Item Category Code" +'/' +ProductGroup.Code);
    UNTIL ProductGroup.NEXT=0
  • bbrownbbrown Member Posts: 3,268
    You cannot use MODIFY command to modify a primary key fields.

    Instead use RENAME.

    See the code below;

    ProductGroup.RESET;
    IF ProductGroup.FINDFIRST THEN
    REPEAT
    if ProductGroup2.get(ProductGroup."Item Category Code",ProductGroup.Code) then
    ProductGroup2.Rename("Item Category Code",ProductGroup."Item Category Code" +'/' +ProductGroup.Code);
    UNTIL ProductGroup.NEXT=0

    Improper use of FINDFIRST. Also why fetch records twice?

    ProductGroup.RESET;
    IF ProductGroup.FINDSET THEN
    REPEAT
      ProductGroup2 := ProductGroup;
      ProductGroup2.Rename("Item Category Code",ProductGroup."Item Category Code" +'/' +ProductGroup.Code);
    UNTIL ProductGroup.NEXT=0
    
    
    There are no bugs - only undocumented features.
  • bekiobekio Member Posts: 204
    thanks bbrown!
    =D>
  • gvolkovgvolkov Member Posts: 196
    how do take care of
    IF FINDSET 
    RENAME()
    UNTIL NEXT = 0
    

    where primary key contains multiple fields and you are renaming field number 4 for example?

    in my example working with 'analysis view entry'
    SETRANGE("Dimension 2 Value Code",'12345');
    IF FINDSET THEN
    REPEAT
    i := i+1;
    IF "Dimension 4 Value Code" = '' THEN
    RENAME("Analysis View Code","G/L Account No.","Dimension 1 Value Code","Dimension 2 Value Code",
    "Dimension 3 Value Code",'11111',"Business Unit Code","Posting Date","Entry No.");
    UNTIL NEXT = 0;

    it seems like after the rename it fails to find the next record due to resort?
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
  • bbrownbbrown Member Posts: 3,268
    That's why you don't rename using the record variable with which you are reading thru the table.
    There are no bugs - only undocumented features.
  • gvolkovgvolkov Member Posts: 196
    ahhh, i should have actually read the code. :thumbsup:
    Microsoft Certified Technology Specialist
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified Business Management Solutions Specialist

    http://www.navisiontech.com
Sign In or Register to comment.