Transferfields Versus Assignment Method (Rec1:=Rec2)

KeyNavPro
Member Posts: 1
Transferfields Versus Assignment Method (Rec1:=Rec2)
1. What is the main Difference?
2. What is Best Practice and Why?
3. I have a code that causes an error which might be from assignment method, it goes something LIKE this:
Function()
Rec1:=Rec2;
Rec1.Insert(True);
---
function Rec1 - Insert()
Main_Key:= LastNumber + 1;
----
And I get an error: Main_Key (Number) already exists
Can you explain if this happens as a result from assignment method Or should I check my code for other issues?
1. What is the main Difference?
2. What is Best Practice and Why?
3. I have a code that causes an error which might be from assignment method, it goes something LIKE this:
Function()
Rec1:=Rec2;
Rec1.Insert(True);
---
function Rec1 - Insert()
Main_Key:= LastNumber + 1;
----
And I get an error: Main_Key (Number) already exists
Can you explain if this happens as a result from assignment method Or should I check my code for other issues?
0
Best Answer
-
First - welcome to Mibuso Keren
While it is good to know the difference between those methods, it's not relevant to the error that you are getting. You would get the same error using all three methods.
The reason why you are getting that error is because you are trying to insert a record that already exists. Both Rec1 and Rec2 are variables based on the same table. I assume that somewhere you read Rec2 from the database, and then you assign Rec1 with the same values. Then, you do an INSERT, which attempts to write that record into the database. It already exists, so then you get the error.
You are on the right track by assigning a new value for the key field, but it is too late. OnInsert checks key values before running the code. Try setting the key value before calling INSERT(TRUE).5
Answers
-
Thanks
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/0 -
First - welcome to Mibuso Keren
While it is good to know the difference between those methods, it's not relevant to the error that you are getting. You would get the same error using all three methods.
The reason why you are getting that error is because you are trying to insert a record that already exists. Both Rec1 and Rec2 are variables based on the same table. I assume that somewhere you read Rec2 from the database, and then you assign Rec1 with the same values. Then, you do an INSERT, which attempts to write that record into the database. It already exists, so then you get the error.
You are on the right track by assigning a new value for the key field, but it is too late. OnInsert checks key values before running the code. Try setting the key value before calling INSERT(TRUE).5 -
Hi,
LastNumber is a global variable in the table object?
LastNumber is not the same in Rec1, as it is in Rec2, regardless the method you use to populate the fields. (I'm not absolutely sure about this, when it comes to the COPY method, but I am pretty confident.)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