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 ?
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...
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.
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?
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?
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.
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.
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)
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?
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
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.
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!
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.
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.
Comments
Can you post a code sample?
Actually it does not matter what I try to insert or which table I try to insert into , it always fails...
I tried onDeactivateForm() and navision does not let me write to the tables there as well.
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.
If it is based on existing info, why not call your update code from the OnOpenForm?
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.
jpjesus@netcabo.pt
Based on your above statement, why are you not updating from the OnCloseForm trigger?
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
-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.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
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.
MVP - Dynamics NAV
My BLOG
NAVERTICA a.s.