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
Comments
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 exit
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;
What is the key of the table?
Why isn't it a function which you call twice?
Why the second reset?
Ect...
If it was hard to write, it should be hard to understand."
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... - ?
If the key used is the primary key then you wouldn't need the reset, or the setfilters just use '
plus the code would be better off as function called twice.
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.
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;
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:
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...
If it was hard to write, it should be hard to understand."
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.
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.
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)
If it was hard to write, it should be hard to understand."
Field1 := Value1;
Field2 := Value2;
if insert then;
Field1 := Value3;
Field2 := Value4;
if insert then;
there will no error occure and you have an short solution
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 ?!?
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯