Double insert

Harry888
Member Posts: 12
Hi Everyone!
I've got a problem:
There is a table (Rec) I need to make double insert when user inserts a new string in a form, something like this:
Rec.RESET;
Rec.SETFILTER(Field1,Value1);
Rec.SETFILTER(Field2,Value2);
IF NOT Rec.FIND('-') THEN BEGIN
Field1:=Value1;
Field2:=Value2;
INSERT;
END;
Rec.RESET;
Rec.SETFILTER(Field1,Value3);
Rec.SETFILTER(Field2,Value4);
IF NOT Rec.FIND('-') THEN
Field1:=Value3;
Field2:=Value4;
INSERT;
END;
But it does not works
Maybe someone has ideas about subj ?
Thanks
I've got a problem:
There is a table (Rec) I need to make double insert when user inserts a new string in a form, something like this:
Rec.RESET;
Rec.SETFILTER(Field1,Value1);
Rec.SETFILTER(Field2,Value2);
IF NOT Rec.FIND('-') THEN BEGIN
Field1:=Value1;
Field2:=Value2;
INSERT;
END;
Rec.RESET;
Rec.SETFILTER(Field1,Value3);
Rec.SETFILTER(Field2,Value4);
IF NOT Rec.FIND('-') THEN
Field1:=Value3;
Field2:=Value4;
INSERT;
END;
But it does not works

Thanks
0
Comments
-
Hi,
You did not tell us the problem.
However the last END in your code has no begin in the code because the second IF has no BEGIN after THEN:
IF NOT Rec.FIND('-') THEN //missing BEGIN
Field1:=Value3;
Field2:=Value4;
INSERT;
END;
so, the second insert and the assignment(Field2:=value4) are always carried out wether the combination(value3,value4) exist in REC or not.
record exit error then if combination already exit0 -
Ofcourse correct is
Rec.RESET;
Rec.SETFILTER(Field1,Value1);
Rec.SETFILTER(Field2,Value2);
IF NOT Rec.FIND('-') THEN BEGIN
Field1:=Value1;
Field2:=Value2;
INSERT;
END;
Rec.RESET;
Rec.SETFILTER(Field1,Value3);
Rec.SETFILTER(Field2,Value4);
IF NOT Rec.FIND('-') THEN BEGIN
Field1:=Value3;
Field2:=Value4;
INSERT;
END;0 -
What does not work?
What is the key of the table?
Why isn't it a function which you call twice?
Why the second reset?
Ect..."Real programmers don't comment their code.
If it was hard to write, it should be hard to understand."0 -
What does not work?
This code inserts only one string field1=value3 ;Field2=Value4
What is the key of the table?
Кеу is Field1,Field2
Why isn't it a function which you call twice?
Just do not know
Why the second reset?
Second reset - i need to check if same string already exists
Ect... - ?0 -
Harry
If the key used is the primary key then you wouldn't need the reset, or the setfilters just use 'IF NOT Rec.GET(Value1,Value2) THEN BEGIN Rec.Field1 := Value1; Rec.Field2 := Value2; Rec.INSERT; END; Currform.Update; IF NOT Rec.GET(Value3,Value4) THEN BEGIN Rec.Field1 := Value3; Rec.Field2 := Value4; Rec.INSERT; END; Currform.UPDATE;
plus the code would be better off as function called twice.Answer the question and wait for the answer.0 -
Thahks StephenG it works, but now there is another trouble: I plased this code to OnInsert of a form based on table Rec , now inserts 2 strings and then I get an error that tells combination(value1,value2) exist in REC. What is wrong?0
-
Hi Harry
Is Rec a declared variable or the Current Record of the Form?
The string the user enters, is this an actual field on the Table or Variable on the form?
How do Value1, Value2, Value3 & Value4 get set/assigned?
With this info we should know if the code should be placed on the OnInsert trigger or on a different trigger.Answer the question and wait for the answer.0 -
Hi Stephen.
Rec is a Current record of the Form
The strings user enters is a Field of a tale (actually user enters only Value2, other1,3,4 - i take from a table) The code that is now is:
Value1 & 3 already defined
IF NOT GET(Value1,Value2) THEN BEGIN
Field1:= Value1;
Field2:= Value2;
INSERT;
COMMIT;
END;
IF NOT GET(Value3,CodeunitThatgetsValue4(value2)) THEN BEGIN
Field1:= Value3;
Field2:= Value4;
INSERT;
COMMIT;
END;0 -
dude!
the following happens:
if you create a new record, the record will be inserted by Navision automatically. Before the actual insert he OnInsert on the forum and in the table are "triggered".
sample: If I program an INSERT in the OnInsert trigger the following happens. The actual insert of the record that is going to be inserted by the system (OnInsert trigger) is happening when the OnInsert trigger is completly finished. So If I do an INSERT in this trigger the record will already exist when the system tries to insert it. Therefor you will get an error. So don't try to insert a record in the OnInsert trigger that you are already inserting by running the OnInsert trigger.
Was this a nit clear?
BTW, I think the code should be something like this:Function InsertWhatever(Value1 ; Value2) record.field1 := Value1; record.field2 := Value2; if not record.find then record.insert;
Do !NOT! use commits!
You'll know when to use a commit the moment you'll need it. Untill then...
Do !NOT! use commits!
Last question...
What would you like to make? Tell us the exact situation please..."Real programmers don't comment their code.
If it was hard to write, it should be hard to understand."0 -
Hi Harry
Like Emiel said, create a function to insert the records, but in the function define Local Variable for Rec2 and use this for inserting the records.Function InsertWhatever(Value1;Value2) Local Variable Rec2 Record Table of Rec IF NOT Rec2.GET(Value1,Value2) THEN BEGIN Rec2.Field1 := Value1; Rec2.Field2 := Value2; Rec2.INSERT; END;
If i unerstood your last reply correctly, you will need to call this function twice from the OnAfterValidate Trigger of Value2. You will not need the COMMIT.
I hope this is more help.Answer the question and wait for the answer.0 -
StephenG,
You are right using the rec2. But this is what I meant by my variable record. But good thing you pointed it out to be sure.(just saving my ass)
"Real programmers don't comment their code.
If it was hard to write, it should be hard to understand."0 -
For some reasones I was unable to post it before now.Thanks everyone , now it works OK0
-
Why not just:
Field1 := Value1;
Field2 := Value2;
if insert then;
Field1 := Value3;
Field2 := Value4;
if insert then;
there will no error occure and you have an short solution0 -
It works, but it's not nice. It's not wise to change Primary Key fields of rec on OnInsert trigger. If you think you need to do this you should do it elsewhere.
What if user was entering value5 and value6 in the PK fields, then just different recs are inserted and user can ask himself what's happening ?!?®obi
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯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