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?
0
Answers
dynamicsuser.net/nav/f/106/t/8364
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
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).
RIS Plus, LLC
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.)