Inserting records into table while in OnAftergetRecord

gurleengurleen Member Posts: 39
I am trying to insert records into a table (that I created )from the OnAfterGetRecord of a form. The system throws an error saying that "you cannot make any changes to the database in this trigger" . Is there any way to do what I want ?

Comments

  • bbrownbbrown Member Posts: 3,268
    Not from that trigger. Can you better describe what you are trying to do?
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    While Looping through the records I have to insert some values from the sales header ( it's a temp record) values into my own table.
  • bbrownbbrown Member Posts: 3,268
    What type of form is this? Also are you changing data in the form or just displaying it and writing to the other table?

    Can you post a code sample?
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    The form is 10038 Sales Order Statistics
    Form - OnAfterGetRecord()
      .....
      Navision Code....
      ....
       // My Code in which I try to insert a record into Company_Setup Table
       // where Company_Setup is a record type of a table that I define
      Company_Setup.NAVISION_USER_ID := 'aaa';
      Company_Setup.Prefs := 0;
      Company_Setup.SERIAL_NUMBER := '2223423';
      Company_Setup.INSERT;
      MESSAGE('inserted');
    

    Actually it does not matter what I try to insert or which table I try to insert into , it always fails...
  • bbrownbbrown Member Posts: 3,268
    You cannot modify the database from that trigger. You will need to redesign your process to populate the table from another trigger.
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    Is there any documentation on the order in which the triggers are called on a form ?

    I tried onDeactivateForm() and navision does not let me write to the tables there as well.
  • bbrownbbrown Member Posts: 3,268
    If you find that document let me know.

    You can write to the database from the following form triggers:

    OnCloseForm
    OnOpenForm
    OnQueryCloseForm
    OnInsertRecord
    OnModifyRecord
    OnDeleteRecord
    OnTimer

    For information on these triggers, see the c/side help.
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    My problem is that I need to save the values to our table before any other form in opened up . If I dont save the values to my table it might cause some inconsistencies.
  • bbrownbbrown Member Posts: 3,268
    You might be able to leverage the timer but probably need to rethink your solution. What are you trying to accomplish? (business issue not technical).
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    The timer might not be a good idea as it will be unnecessarily called again and again and would not guarantee 100% that a user cannot open up another form before my code is called. Is there a way to cause a trigger to occur from another?
  • bbrownbbrown Member Posts: 3,268
    There are ways to control whether or not timer code is run, but I agree it would not guarantee the code was run before another form was opened. This is why I am actually suggesting that you rethink the solution. What business problem are you trying solve?
    There are no bugs - only undocumented features.
  • bbrownbbrown Member Posts: 3,268
    Is the data your are writing to your table based on existing data or is it dependent on the user entering additional info into this form?

    If it is based on existing info, why not call your update code from the OnOpenForm?
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    It is based on the user entering data on the form. The logic tries to maintain the state of the navison sales order transactions coupled with custom data that I provide . The problem is that the form 10038 Sales Order Statistics can be opened from many forms. So at one time I can have multiple Sales Order Statistics forms open with each one having a different set of values of the state that I want ot maintain. The data that I collect won't be written to the tables in the database till I close these Sales Order Statistics forms. Each one of them might overwrite the data in the table underneath.
  • jpjesusjpjesus Member Posts: 45
    If I understand correctly you are running through the records on you table (10038) and getting info from the sales header table and inserting it into table 10038.

    Since you are doing this on the trigger OnAfterGetRecord of the form and the form is based on table 10038 it's quite normal that you get an error.

    So here's a workaround that might do the trick:
    Create a new local instance for your table and perform the insert over this new instance. This should work since you are inserting into 'another' table for all purposes.

    Nevertheless, I would review the logic of all the process. I am sure that there must be other (better) ways to achieve your goal.
  • bbrownbbrown Member Posts: 3,268
    You cannot make any database changes from this trigger, it not just restricted to the sourcetable of the form.
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    it's the other way around. I am trying to insert records into my table while I loop over Navision tables. I can always insert records into a temp table but not to the actual table ( ie a record variable directly linked to my table)
  • bbrownbbrown Member Posts: 3,268
    That' right, you can insert records into temp tables as much as your want. They are not part of the database. They only exist in client memory. You cannot however update the actual database from this trigger.
    The data that I collect won't be written to the tables in the database till I close these Sales Order Statistics forms

    Based on your above statement, why are you not updating from the OnCloseForm trigger?
    There are no bugs - only undocumented features.
  • gurleengurleen Member Posts: 39
    The data that I collect won't be written to the tables in the database till I close these Sales Order Statistics forms

    I was just stating the fact that if I use onFormClose then till the 10038 form closes I will have to keep the data in my temp table only. So if there are four 10038 forms open, each one of them will have its own state and I will have to code in a lot of logic to keep the data consistent( which is what I am coding in right now ).

    The reason for wanting to write to the tables in onAfterGetRecord is that most of the work is being done by navision code. I just need to pick up the values from their temp record variables. If I have to code everything myself it will take me a long time and I will certainly miss something out which in turn may make navision unstable if I mess up the data in navision tables
  • bbrownbbrown Member Posts: 3,268
    You cannot write to the database from the OnAfterGetRecord trigger, there is no way around this. If you want to write the data before closing the form, then put your code in a new function and call it from the OnTimer trigger. Set a variable to indicate the code has been run. Check this code from the OnCloseForm trigger and run the function if it has not been run. Also abort the timer once the code is run.
    There are no bugs - only undocumented features.
  • krikikriki Member, Moderator Posts: 9,118
    Just a wild idea I never tried out.
    -Create a singleinstance codeunit with an entry-point that receives a changed record and it stores this in a temptable in the codeunit, BUT IT DOESN'T WRITE IT TO THE DB.
    -Import the Navision timer dll to get it triggered every few seconds. If it finds records in it's temptable, it writes the temptables to the DB.
    -In your form, in the "OnAfterGetRecord trigger", you send your record to the singleinstance-codeunit that stores it in a temptable. So no problem, you aren't writing to the DB from your "OnAfterGetRecord trigger". This will be done by the timer-function in the codeunit that works INDEPENDENTLY.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • bbrownbbrown Member Posts: 3,268
    Interesting idea, but I still vote for redesign.

    Question:

    If the data you are trying to save is based on user input, then why are you attempting to use the OnAfterGetRecord trigger? This will already have fired before the user can input anything.
    There are no bugs - only undocumented features.
  • kinekine Member Posts: 12,562
    This will not work, because you are not allowed to change any table from this record. Why? Because this trigger is called many times and you do not know when will be called etc. It is not good point for modify something. And it is why it is not allowed.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.