I love unsolvable problems. I simply do. I had one a few days ago in the office, and I solved it.
So, the problem went like this. A discussion has been raised among developers about whether it would be possible to display a blank form over a populated table, where user would be able to immediately enter the data of a new record, without having to press F3 (or inserting a new record manually). The problem is, this doesn’t work that way in Dynamics NAV.
There is a technical limitation which is completely due to the way how data is read and written from and to the database. First, there is no Save button anywhere in the system. All of the information entered is automatically saved to the database, so that nothing is lost in case of a an error. This is a good feature, although sometimes hard to come to friendly terms with, especially for new users who have been used to opening forms, entering information, and then being offered options of Cancel and Save. There is no cancel in Dynamics NAV for data entry forms.
What this basically means, is that no form can ever be opened empty if it displays information from a table which is not empty. The only situation when you get an empty form by default, is when you are entering the very first record in a table.
So, how do you have a form, which lays over a populated table, display no information and wait readily for the new entry? Technically, it is impossible to programmaticaly clear the form and have it display nothing, if there is information behind. You could try Rec.INIT() to initialize all the fields, but that won’t help. You could also try Rec.FIND(‘+’) followed by Rec.NEXT to simulate pressing the next button on the last record, but that won’t work either. No other programmatical options are available.
I started from the following fact: the only way to display an empty form if the table is not empty is to display it with a filter which yields no results. In this case you get your blank form, but it brings along a problem: as soon as you enter anything into it, your entry will disappear, because your no-yield filter will immediately filter it out. In order to keep it, you must remove your filter immediately after users entered something in the form.
My solution was the following:
- For form which has to have this functionality, go and define a function which sets a no-yield filter on the key field (in my case, it was No. field). A no-yield filter has to be contradictory in itself, containing an impossible condition, such as Rec.SETFILTER(“No.”,'<>A&A’); (effectively asking the system to show the record which are simultaneously different and equal to A).
- On the same form, define a flag which says that form is in blank-by-default mode. This flag is always false, except when you call the function from step 1 and set it to true.
- In OnInsertRecord trigger create conditional block of code, which executes only if flag from step 2 is true. This piece of code simply clears the filter on the key field and sets the flag back to false.
And that’s it. A few tricks, and you may have your system do the impossible.
P.S. There probably won’t be any posts for a while. I am going on vacation in less than two weeks, and I don’t think I will have time to write here before that. And as I promised in a previous post – there will be some news when I return. See ya!