Changing numbers

ImaspalImaspal Member Posts: 68
Hi Experts!

I have four records in a table. These records are numbered: 1, 2, 3 and 4. These numbers are in a primary key-field of a table.

I have also a form, and with this form it is possible to modify these 1-4 fields. What kind of code do I have to write, if I want a functionality like this: When number is changed from 1 to 4, the other number 4 is changed automaticly to 1 and when number is changed from 2 to 3, the other number 3 is changed automaticly to 2 etc.?

************
Mr. Nice Guy
************

Comments

  • garakgarak Member Posts: 3,263
    if the fields are part of PK than, you can use the Rename Trigger with xRec
    Do you make it right, it works too!
  • ImaspalImaspal Member Posts: 68
    Thanks. I tried to use following code:

    SETRANGE("Order No.", "Order No.");
    IF FINDFIRST THEN
    "Order No." := xRec."Order No.";
    Name := xRec.Name;
    Code := xRec.Code;

    ..but I didn't succes. Can you give me an example code?

    ************
    Mr. Nice Guy
    ************
  • DenSterDenSter Member Posts: 8,305
    RENAME won't work because he wants to switch the records around. this is a very strange request, and I'm not sure I follow the reasoning behind it. If this is something that changes a lot, then you should really use a different field as the PK and handle uniqueness through code.
  • EugeneEugene Member Posts: 309
    when changing primary key field value the rename trigger of a table is called so you should write your code in OnRename trigger.

    But your desire is very odd and weird.
    Why when renaming one record would you ever want to rename another one and which one ? how do you know that 1 corresponds to 4; that 2 corresponds to 3 ?

    when you try to rename record 1 to 4 then the record 4 has to be renamed to 1 but this will cause circular/recursive OnRename trigger calls and you have to stop the recursion somehow.
  • garakgarak Member Posts: 3,263
    sorry, change the user the field in form or u will give them an seperate function :?: Because rename is not the fastes process. Second problem: before the rename trigger will run, the onvalidate trigger of the field runs, and is bet my 2 Cents, there cames an error message (Record still exists).

    So, if its not necessary store this information in an other field. Because after rename all "Subtables" with Relations must also changed.

    If u must use this field as PK, and must rename, then do this per separate function and use insert and delete. Its faster. But you need for this process an Buffer variable (to store old information before delete)
    Do you make it right, it works too!
  • kinekine Member Posts: 12,562
    1) If you really need to rename, you need to
    a) Rename record 1 to XXX
    b) Rename record 3 to 1
    c) Rename record XXX to 3
    else you have the conflict that the record already exists...

    2) What about just transfering (exchanging) the rest of the record? But this depens on if the records in another tables are refering this table and if you need to correct the reference too... than you really need to rename.

    3) Or you can delete/re-insert the record, but again, linked fields will not be updated

    4) Or you can find another solution which do not need to rename - e.g. do not use the number as PK, use something else as PK and use this no. just as another field...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ImaspalImaspal Member Posts: 68
    Thank you for your wise advice. I think I'm trying to find another solution.

    My task was only to make sure that user don't choose same number twice. The thing, why I tried to use this field as a PK-field based on idea that in that case the modified records in a table would automaticly get in order 1, 2, 3 and 4. Anyway thanks for your advice I now understand the solution I was planning was too complicated to carry out.

    Maybe OK-button and under this button a code, where the unequality of numbers is checked. If someone of you have a good hint of how to do such a verification thanks on beforehand.

    ************
    Mr. Nice Guy
    ************
  • kinekine Member Posts: 12,562
    1) You can use Autoincrement to assign the number automatically
    2) You can use No. Series to assign some code automatically
    3) You can use GUID datatype and function for generating the guid to generate unique value for each record.
    4) etc...

    It depends on how the PK is used in other processes...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.