Renaming A Record In Code

Jonathan2708
Jonathan2708 Member Posts: 552
Hi,

In my app, customers are coded based on their customer type - FINance, RETail, COMmercial, so Customer No. is FIN, RET or COM plus an incrementing number. e.g. FIN-0030

When a user changes the customer type field in a record I need to change the primary key to reflect their new account no. Presently I have the following code in the Customer Type - OnValidate() event on the Customer table :


IF (Rec."Customer Type" <> xRec."Customer Type") THEN BEGIN
// Procedure For Changing Contract Type
// First Rename Account Record
SalesSetup.GET;
SalesSetup.TESTFIELD("Finance Customer Nos.");
NewAccountNo := NoSeriesMgt.GetNextNo(SalesSetup."Finance Customer Nos.", 0D, TRUE);

NewRecordVar.RESET;
NewRecordVar.GET(Rec."No.");
NavResult := NewRecordVar.RENAME(NewAccountNo);
END;

This renames the record ok, but I need the screen to refresh properly and automatically move to the newly recoded record.

Any help appreciated, including any alternative (better) methods of doing this.

Jonathan

Comments

  • pdj
    pdj Member Posts: 643
    The main problem is that you rename a variable and not "Rec". After the trigger is executed Navision does a FIND('=<>') using the primary keys of Rec, which still contain the orignal No.

    So you can do two things. Either rename Rec directly or add in the end of your code a GET(NewRecordVar."No.")

    But your code contains a more hidden and serius flaw. If the user changes multiple fields before changing the "Customer Type" these changes will be lost. I can't even see you get the new "Customer Type" stored? I will suggest you start the OnValidate trigger with a MODFY(TRUE) to insure it gets updated in the db before the renaming.
    Regards
    Peter
  • J
    J Member Posts: 10
    IF (Rec."Customer Type" <> xRec."Customer Type") THEN BEGIN
    // Procedure For Changing Contract Type
    // First Rename Account Record
    SalesSetup.GET;
    SalesSetup.TESTFIELD("Finance Customer Nos.");
    NewAccountNo := NoSeriesMgt.GetNextNo(SalesSetup."Finance Customer Nos.", 0D, TRUE);

    NewRecordVar.RESET;
    NewRecordVar.GET(Rec."No.");
    NavResult := NewRecordVar.RENAME(NewAccountNo);
    Rec := NewRecordVar;
    END

    Just add the red piece of code. This should do the trick without getting the data from the db again which is more efficient ...