Renaming A Record In Code

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
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
0
Comments
-
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
Peter0 -
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 ...0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions