Rename Fnction within a repeat loop

SteveSteve Member Posts: 81
Hello All,

I tried to rename mulitple records within a repeat loop and it seems to rename the first record and then breaks the loop. Any ideas?

Code below:

Lines.SETRANGE("Document No.",MasterNo);
IF Lines.FIND('-') THEN
REPEAT
Lines.RENAME(MasterNo,Lines."Line No.");
UNTIL Lines.NEXT=0;
Steve

Comments

  • bostjanlbostjanl Member Posts: 107
    Steve wrote:
    Hello All,

    I tried to rename mulitple records within a repeat loop and it seems to rename the first record and then breaks the loop. Any ideas?

    Code below:

    Lines.SETRANGE("Document No.",MasterNo);
    IF Lines.FIND('-') THEN
    REPEAT
    Lines.RENAME(MasterNo,Lines."Line No.");
    UNTIL Lines.NEXT=0;

    When you rename, you change order of records and it is possible, that the newly renamed record is last in recordset.

    why not use RENAMEALL?


    regards

    BostjanL
  • 2tje2tje Member Posts: 80
    or define a second record variable which you use in the repeat loop to rename the record

    ...
    repeat
    rec2 := rec1;
    rec2.RENAME(...);
    until ..next = 0;
  • SteveSteve Member Posts: 81
    Is RENAMEALL vaild? I get an error when I tried it.

    Thanks everyone.

    After i wrote the help posting, I tried the second rec var approach and it works OK.

    Thanks
    Again
    Steve
  • GoMaDGoMaD Member Posts: 313
    As far as i know, a RENAMEALL doesn't exist in the C/AL environment.

    You even get an error stating "unkown variable".

    :-k ](*,)
    Now, let's see what we can see.
    ...
    Everybody on-line.
    ...
    Looking good!
  • kinekine Member Posts: 12,562
    RENAMEALL not exist... :whistle:

    If you are browsing (looping) through some table and want modify something, use separate variable (as written) for the change... it is only solution for that to not lost the position in the loop and if you are on MS SQL, only solution for good performance... Same for DELETE...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ara3nara3n Member Posts: 9,257
    You need to create a new variable and call rename on that.

    Lines.SETRANGE("Document No.",MasterNo);
    IF Lines.FIND('-') THEN
    REPEAT
    Lines2.get(Lines."Document No.",Lines."Line No.");
    Lines2.RENAME(MasterNo,Lines."Line No.");
    UNTIL Lines.NEXT=0;
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • bostjanlbostjanl Member Posts: 107
    kine wrote:
    RENAMEALL not exist... :whistle:

    If you are browsing (looping) through some table and want modify something, use separate variable (as written) for the change... it is only solution for that to not lost the position in the loop and if you are on MS SQL, only solution for good performance... Same for DELETE...

    Mea culpa! :oops:

    I post that post way to early on Monday morning,..


    BostjanL
  • kinekine Member Posts: 12,562
    I know that Monday's morning... :-({|= :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • SteveSteve Member Posts: 81
    I changed my to to the following.


    Hdr.SETCURRENTKEY("Document Type","No.");
    IF Hdr.FIND('-') THEN
    REPEAT
    CLEAR(THdr);
    THdr.GET(Hdr."Document Type",Hdr."No.");
    THdr.RENAME(Hdr."Document Type",Hdr."No." + COPYSTR(COMPANYNAME,1,1));
    UNTIL Hdr.NEXT=0;

    Var.
    Name DataType Subtype Length
    Hdr Record Sales Header
    THdr Record Sales Header

    This just append the first letter of the Copany Name to the end on the current no. so SO-01112 becmes SO-01112C in the Cronus company.

    THE WEIRD PART.

    This will work for amster tables ( Customer, Item, Vendor) BUT NOT for Sales header, Purchase Header, etc....

    Test if you like, but i am stumped..... ](*,)
    Steve
  • SteveSteve Member Posts: 81
    SOLVED....

    By putting a letter to the end of a code field the newly renamed record would fall below the current record by the default sort. So this would casue the repeat loop to keep grabbing the just renamed record.

    Thanks All =D>
    Steve
  • kinekine Member Posts: 12,562
    it is why is better to use otherrecord for rename and have the original set filtered for record you want to rename (which have for example no last char added or something...)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.