Thursday, 30 August 2007

Any Dataport in a Storm

I'm doing some programming and it's fun! I haven't done any serious programming for a few years and it's good to get back into it, but today I came across some top tips for programming dataports and I don't think I would ever have guessed. What a perfect subject for a blog posting.

Dataports are Dynamics NAV's object for importing and exporting data. The dataport looks a bit like a report when the user launches it with some options to specify field filters and an Options tab for selecting Import/Export and for picking the file name to be used.

For my dataport, I wanted to provide the user with a couple of fields to pick the Gen. Journal Template Name and Gen. Journal Batch Name that the journal lines are to be inserted into. When I added a field to the Request Form, the standard Import/Export radio buttons and the File name field dissappear.

In this post, I'll tell you about a couple of tricks that will help you to put these fields back on the request form and also how to put lookups on the journal fields. I wanted my dataport to look like this:

As you can see, I have the usual File Name and Direction radio buttons and have my two new fields with lookup buttons for the user to select the correct value.

So let's start with the File Name field. Create a global variable of Text 1024 and use this as the Source Expression property for a new text box on the request form. Now here's the trick. If you want the file requester dialogue box to appear without any programming needed, simply make sure the control has an ID of 1. That's it! Thanks to a work colleague for that, I never would have guessed and I thought I was facing a lot of coding around the common dialogue requester.

Next challenge for me was the direction radio buttons. Not being an experienced NAV programmer, I thought that these would use a Boolean control as a Source expression. I tried this and I could not get the buttons to click, no matter what code I added. One of the postings on Dynamics User pointed me to using an Option type field. I created a new global variable as an option string with choices of Import and Export. I then assigned the variable to both of the radio buttons but specified the Import option as the Option property for the top one and Export option as the the Option property for the bottom one. How easy was that?

Now on to the look-up buttons and pick lists. The Journal Template was easy, I just set the TableRelation property on the field to be "Gen. Journal Template". The next bit was a little harder. I wanted the journal batch look-up to only show batches that belong to the journal template I had already selected.

To start, I looked at the G/L Journal form as I knew that this had a batch name field that provided a lookup that worked in the way I wanted. I saw that the field was using a LookupName function in codeunit 230. I put in some code in the OnLookup trigger for the field (after setting the Lookup property to Yes of course) as follows:

GenJournalLine.SETFILTER("Journal Template Name", GenJournalLine."Journal Template Name");

On this line, GenJnlManagement is my variable of type codeunit (230), GenJnlBatch is my variable of code 10 which is storing my batch name and GenJournalLine is a record type variable of type Gen. Journal Line (that is required by the function.) The filter, I figured would provide the magic of only showing the batch names for the correct template.

When I called this function, I got an error saying that I needed to set a filter on the Batch Name field. This seemed a bit weird to me since I was trying to lookup the field, so why would I be filtering on it?

I then figured that maybe NAV wanted to know where to put the cursor when it displays the lookup form so I added this line before the call to the codeunit function.

GenJournalLine.SETFILTER("Journal Batch Name", '-');

This line tells NAV to find the first Batch Name that matches the filters I have set on my record handle.

All was sweet! A few little error checks and validations and I was in business. Hopefully, if someone has the same issues and searches for "How to use Radio Buttons in a Request Form" or "Filter must be set before calling LookUpName name function" they will find this post and get a nice shortcut.

No comments: