Sep 16

Since Dynamics NAV version 5, when RIM was introduced there has been the functionality for Data Migration and Master Data Templates. I have never really used the Migration tool, as it just seemed to be too limited, compared to what you can accomplish with standard dataports. During my last go-live the customer was using the Master Data Templates, which is a nice little tool – still with a lot of limitations.

The usages I have seen of the tool has primarily been around the common master data tables, such as Customer, Vendor and Items. Often there are some confusion around templates used for Customers, as there are “Data Templates” (generic templates) and “Customer Templates” (customer specific templates used from Contacts / Relationship Management).

Lets get into some details, (in the classic client):

The setups are found under “Administration->Application Setup->General->Setup Master Templates”:


The setup screen is pretty much self explaining, give you template a name, set the table it is related to, and then fill in the fields and their default values:


That is the setup part of it. Very simple!

Now let’s look at the Item Card, and the code that triggers the template functionality. This is located on the Item Card, Functions, Apply Template:

RecRef.GETTABLE(Rec);
TemplateMgt.UpdateFromTemplateSelection(RecRef);

So it uses a RecordRef variable, which is initialized with your current form record. Then you call the UpdateFromTemplateSelection in codeunit 8612 Template Management. This opens up a form with all associated templates for your current record, and when you select one, it updates the fields on your master data.

Notice if the fields have dependencies, like eg. “Base Unit of Measure” that needs to have a record created in the “Item Unit of Measure” table, then it will fail, if this hasn’t been created ahead of time.

The functionality can be found here in standard NAV: Item, Customer, Vendor and Contact. Maybe other places too, that I haven’t seen, but the code to add is very simple.

So yesterday I talked to colleague Henrik (aka E-Ship Guy), he was about to do a mod for a customer that needed a generic report to update master data records. Without going into any detail of the original approach, we ended up using the Data Templates with a small modification to do batch updates on data. Lets see how it was done:

On table 8618 Data Template Header I added a custom field of type TableFilter. This fieldtype is not commonly used and wouldn’t be of much use without the TableFilter2View function created by another of my colleagues Peter D. Jørgensen (see this post on Mibuso.com).


In order to get the TableFilter fields working, you need to look into the properties:


The property “TableIDExpr” is the key here. It needs to have a source expression that tells it the table id. As we already have a field for TableID (field 3), we using this.

Now open the table, and click on the assist button in the field:


You will see a standard filter form (like when you do CTRL+F7), on doing the lookup in the field column takes you to the fields of your source table.

After setting your filters, you will see the value stored in the field in a format like this:

“Item: No.=1*,Manufacturer Code=CISCO”

Our goal is to use this on a RecordRef with SETVIEW. Unfortunately the syntax is slightly different, and this is where Peter’s function comes in handy, it translates it into this:

“WHERE( No.=FILTER(1*),Manufacturer Code=FILTER(CISCO))”

Lets add the functionality to form 8618, to do batch updates. Here are the steps:

  1. Add the new field to the form, and set AssistEdit=Yes. (otherwise the assist edit will not work)
  2. Add a new menuitem under functions and add this code:
    //TESTFIELD("Data Filter");
    RecRef.OPEN(TableID);
    RecRef.SETVIEW(TableFilter2View(FORMAT("Data Filter")));
    IF NOT CONFIRM(
    STRSUBSTNO('This will update %1 record(s) in table ''%2'', continue?\\Filters: ''%3''',
    RecRef.COUNT,RecRef.NAME,RecRef.GETFILTERS),FALSE)
    THEN
    ERROR('');
    Window.OPEN('Applying template @1@@@@@@@@');
    IF RecRef.FINDSET THEN REPEAT
    CurrRec += 1;
    Window.UPDATE(1,ROUND(CurrRec / RecRef.COUNT * 10000,1));
    TemplateMgmt.UpdateRecord(Rec,RecRef);
    UNTIL RecRef.NEXT = 0;
    Window.CLOSE;
    

Important notice: A TESTFIELD on a TableFilter seems to crash the client, so it is commented out for now!

That is it, enjoy. Next I will show you how to use the Data Templates for mandatory field management. Stay tuned!

The function from Peter in its original version is here:

TableFilter2View(p_TableFilter : Text[1024]) r_View : Text[1024]
// Funtion made by pdj - Aalborg
// Feel free to use, but please keep all comment lines and report any errors at <a href="http://mibuso.com/forum/viewtopic.php?t=7587">http://mibuso.com/forum/viewtopic.php?t=7587</a>
// TableFilter format:
// <TableName>:<FieldCaption>=<FieldFilter>,<FieldCaption>=<FieldFilter>,..
// View format:
//   [SORTING(<Key>)] WHERE(<FieldCaption>=FILTER(<FieldFilter>),<FieldCaption>=FILTER(<FieldFilter>),...)
IF p_TableFilter = '' THEN
EXIT('');
r_View := 'WHERE(';
FOR l_CharNo := STRPOS(p_TableFilter,':') + 1 TO STRLEN(p_TableFilter) DO BEGIN
CASE p_TableFilter[l_CharNo] OF
'=': r_View := r_View + '=FILTER(';
',': r_View := r_View + '),';
'"':
BEGIN
l_CharNo := l_CharNo + 1;
REPEAT
r_View := r_View + FORMAT(p_TableFilter[l_CharNo]);
l_CharNo := l_CharNo + 1;
UNTIL p_TableFilter[l_CharNo] = '"';
l_CharNo := l_CharNo + 1;
END;
ELSE
r_View := r_View + FORMAT(p_TableFilter[l_CharNo]);
END;
END;
r_View := r_View + '))';

5 Responses to “Cloning – Extending Data Templates in Dynamics NAV using TableFilter”

  1. Thanks again, Soren for pointing me to the direction. Let’s just say I would still be pulling hair out of my skull if it hadn’t been for your information.

    First time I even noticed the TableFilter field type also. My Customer is very exited (They have thousands of items, that haven’t been updated for years)

  2. SNielsen says:

    You might want to take a look at the 2009 version of the codeunit 8611 Migration Management, which has some improvements for handling type conversions.

  3. Peter says:

    Hi Søren and Henrik,
    Great you could use my function after so long time 🙂 Strange that MS haven’t made any proper solution for it. I guess they find it hard to improve my solution… 😉

    • SNielsen says:

      Exactly! Don’t fix what is not broken. – But still it would be nice that they stored the TableFilter in the same format as the VIEW, eliminating the need for you function :).

  4. navuser1 says:

    When we store any informtion in TablFilter field using NAV UI that can be read easily from NAV UI, but how we can able to read the same from SQL Server Side.

Leave a Reply

preload preload preload
pornpants.com pornofri.com kilporn.com