Rename Item code by using Codeunit

abnainasabnainas Member Posts: 174
Dear All,

I'm tring to rename the item code for 3700 items, So I made a codeunit to do the following:
OnRun()
  itemrec.FIND('-');
  REPEAT
    itemrec.INIT; 
    itemrec.RENAME(itemrec."No." + 'V');
  UNTIL itemrec.NEXT = 0;

Do you have another code to do the same result.
Ahmad Bani Naser
Dynamics NAV Developer

Comments

  • abnainasabnainas Member Posts: 174
    Dear All,

    When I'm used the following Code.... Nothing Happend.
    OnRun()
      itemrec.FIND('-');
      REPEAT
        itemrec.INIT; 
        itemrec.RENAME('V' + itemrec."No.");
      UNTIL itemrec.NEXT = 0;
    

    Please note that Item Code is a numbers.

    Please advise
    Ahmad Bani Naser
    Dynamics NAV Developer
  • mihail_kolevmihail_kolev Member Posts: 379
    OnRun()
      itemrec.REST;
      IF itemrec.FIND('-') THEN
      REPEAT
        itemrec.RENAME('V' + itemrec."No.");
      UNTIL itemrec.NEXT = 0; 
    

    what about that?

    Also as far as i know, the Primary key in item table is No with type Code.
    Enabled Field No. Field Name Data Type Length
    Yes 1 No. Code 20
    -Mihail- [MCTS]
  • abnainasabnainas Member Posts: 174
    The attached error have beed raised.

    Please advise
    Ahmad Bani Naser
    Dynamics NAV Developer
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Problem is that if your item numbers are digits only, then adding V in front of any number moves your current position to the end of the table.

    items before rename :
    01
    02
    03

    renaming 01 to V01 will give you
    02
    03
    V01

    So if ItemRec.No was '01', and ItemRec.No is 'V01' after rename, ItemRec.NEXT cannot find any more records.

    try this:
    itemrec.REST;
      itemrec.ASCENDING(FALSE);
      itemrec.SETRANGE("No.", <first current item no here>, <last current item no here>);
      IF itemrec.FIND('+') THEN
      REPEAT
        itemrec.RENAME('V' + itemrec."No.");
      UNTIL itemrec.NEXT(-1) = 0;
    
    or this
    itemrec.REST;
      itemrec.SETRANGE("No.", <first current item no here>, <last current item no here>);
      IF itemrec.FIND('-') THEN
      REPEAT
        itemrec2 := itemrec;
        itemrec.RENAME('V' + itemrec."No.");
        itemrec := itemrec2;
      UNTIL itemrec.NEXT = 0;
    

    Don't forget set proper filters on itemrec."no."

    Regards,
    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • abnainasabnainas Member Posts: 174
    it's worked,

    thanks alot
    Ahmad Bani Naser
    Dynamics NAV Developer
  • krikikriki Member, Moderator Posts: 9,115
    [Topic moved from 'NAV 2009' forum to 'NAV/Navision' forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


Sign In or Register to comment.