Double insert

Harry888Harry888 Member Posts: 12
edited 2004-05-11 in Navision Attain
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

  • kailoukailou Member Posts: 14
    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 exit
  • Harry888Harry888 Member Posts: 12
    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;
  • eromeineromein Member Posts: 589
    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."
  • Harry888Harry888 Member Posts: 12
    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... - ?
  • StephenGStephenG Member Posts: 99
    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.
  • Harry888Harry888 Member Posts: 12
    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?
  • StephenGStephenG Member Posts: 99
    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.
  • Harry888Harry888 Member Posts: 12
    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;
  • eromeineromein Member Posts: 589
    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."
  • StephenGStephenG Member Posts: 99
    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.
  • eromeineromein Member Posts: 589
    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."
  • Harry888Harry888 Member Posts: 12
    For some reasones I was unable to post it before now.Thanks everyone , now it works OK :D
  • WarfoxWarfox Member Posts: 53
    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 solution
  • RobertMoRobertMo Member Posts: 484
    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           
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sign In or Register to comment.