onsdag den 9. december 2009

Batch-renaming a primary key.

For a programmer who has experienced the "terror" of having to batch-rename primary keys in Dynamics AX's predecessor XAL/C5, I was pleasantly surprised to find how easy it is in Dynamics AX 2009.

Though not documented a method called renamePrimayKey exists on the kernel class xRecord which each table apparently inherits from.

If you call this method after changing the primarykey field on a tablebuffer, this change will be cascaded to related tables. :0)

I was given a task to perform renaming of Items in the InventTable.
In an environment that is to go live - so THERE WERE NO TRANSACTIONS OG STOCK LEVELS. If this has been the case we would have adviced the customer to live with it, as the renaming process potentially would have taken a very long time

Nearly 75% of the item numbers were named with 4 digits, the rest with 5.
The customer wanted only 5-digit item numbers.

How to do this in one go ?
A simple job is enough:


static void Job7(Args _args)
{

InventTable inventTable;
;

ttsbegin;
while select forupdate inventTable
where inventTable.ItemId LIKE "????"
{
inventTable.ItemId = "0"+InventTable.ItemId;
inventTable.renamePrimaryKey();
}
ttscommit;
}

As always USE AT OWN RISK.

DYNAMICS AX Import Export Tool - digging a bit deeper.

Normally when I've used the export/import-tool in AX, it's because you want to dump data from a small database and import it in another environment, or if you want
to install some sort of demonstration database in an environment.

Up until now, if I've been presented with a task of importing data into a customers environment, i've always programmed some sort of class that handles the import.

A couple of weeks ago, a colleague of mine showed me that the import/export tool, can be used for the same task - that is importing data, from e.g. a .csv-file, that your customer prepared in Excel.

I was thrilled to discover that I do not actually always need to make a class to import data in a specific (or more specific) table(s).

The procedure is:

1) Create your own Definition group - make sure it is empty by removing all checkmarks on the Set up and include table groups tabs pages, and choose the type User defined.
Save the def. group.

2) Now click the Table setup button. Choose the table that you want to import data into. Choose the import status delete and import (if you want to clear the table of data before you import), or just import. Set up the file name of the file containing the data.

3) On the general tab you can specify field delimiter (in my case ; for a .csv-file).

4) On the Conversion tab you can actually write x++ code, that will be compiled and executed for each line in the file that is read in. In the method you get a table buffer of the table you choose in step 2, and a container containing all the fields read from the file.
This means you can make custom transformation, and even create records in related tables if you want, while at the same time importing data in to the chosen table.
You can let the compiler parse the code to verify syntax before saving the definition.

5) Using the last tabpage on the form, you can have the first record read from the file and shown to you where the values read are mapped to the fields chosen in step 6.

6) Using the button Field setup you can map the fields/values read from the file, to the fields of the table chosen in step 2.
Even on the field level you can write conversion code similar to the code mentioned in step 4.

All this works very nicely for importing data.

Now I've used this for making imports in the supply chain modules, Invent locations, wms locations, planning data and Item coverage data, preparing the go-live at a customer site.
This was done in a test environment.

When the customer had verified the test data, we wanted to move the import templates created using the above steps, to the live production environment, to make the "real" import.

How to be done ?

Moving the import-definition between environments (test and production) is done by exporting data in the tables:

SysExpImpGroup (which contains the definition group)
SysExpImpTable (which contains the tables of the def. group)
SysExpImpField (which contains the fields of the tables of the def. group)
And maybe
SysExpImpQuery (which contains any Export criteria - if used).

One little problem with the abovementiond procedure of moving the SysExpImp table.

When you import the exported data, the code you write in the conversion tab pages will be messed up. The import process apparently strips all newlines in the code.

You'll have to go through the conversion code and insert linebreaks after each semicolon, after import. Otherwise the import will not function correctly !!!

onsdag den 2. december 2009

Weird experience with tablemaps and layers

Today we experienced something weird in AX2009.

We have installed an application module in the AX in the VAR and VAP layers, and are making CUS-layer modifications that are customer specific.
In the module a tablemap is used to be able to implement code once but for use on several tables.

However we experienced a run-time error when we ran a form, that called the code on the table map. The kernel complained about a field having id 0.

We searched high and low but couldn't find any reason for the run-time error that occurred. We then tried making a cus-layer edition of all the mappings on tablemap and voila, no more run time error.

Weird. :0S

mandag den 30. november 2009

New job.

As of january 1st 2010, I am no longer employed by thy:data.
I will be employed as a Senior System Consultant by Columbus IT Partner Denmark A/S situated at the office in Aalborg.

fredag den 23. oktober 2009

Check on which tier code is running.

A colleague of mine needed to determine (runtime) what ax-tier his code was running on.

We digged around, and he came up with the solution:

if (isRunningOnServer())
{
// Server
}
else
{
// Client
}

As a footnote he needed this check to make some code to refresh the AOS-code cache, when implementing quick-fixes to an environment running with multiple AOS-instances.

mandag den 19. oktober 2009

Merging code/elements in Layers

I've been assigned a task where the merge between VAR and CUS-layer of the application is necessary.

I like to make my self a TO-do-list of elements to be processed, so I can check an item when it is done.

I wrote a small job to identify elements in the application that were represented in both VAR and CUS-layers thus representing a potential layer-conflict.

The job produces an info-log with the potential conflicts that can be copied in to Excel to be used as a TO-DO-list.

static void JSOVarVapAndCusConflictsJob9(Args _args)
{
UtilIdElements utilIdElements;
UtilIdElements VarVapUtilIdElements;
Map elemMap;
MapIterator elemMI;
UtilElementType recType;
RecId utilId;
int pos;
str 60 elementName;

elemMap = new Map(Types::String,Types::String);
while select UtilIdElements
where (utilIdElements.utilLevel == UtilEntryLevel::cus)
&& UtilIdElements.parentId == 0
{
elemMap.insert(enum2str(UtilIdElements.recordType)+";"+num2str(UtilIdElements.recid,0,0,0,0),UtilIdElements.name);
}

elemMI = new MapIterator(elemMap);
while (elemMi.more())
{
pos = strscan(elemMI.key(),";",1,strlen(elemMi.key())-1);
recType = str2enum(recType,substr(elemMI.key(),1,pos-1));
// utilId = str2num(substr(elemMI.key(),pos+1,strlen(elemMI.key())-pos));
elementName = elemMi.value();

select firstonly VarVapUtilIdElements
where VarVapUtilIdElements.recordType == recType
&& VarVapUtilIdElements.name == elementName
&& (VarVapUtilIdElements.utilLevel == UtilEntryLevel::var ||
VarVapUtilIdElements.utilLevel == UtilEntryLevel::vap);
if (VarVapUtilIdElements.RecId)
{
info("Potentiel conflict between CUS- og VAR/VAP-lag: "+enum2str(recType)+" "+elemMI.value());
}
elemMi.next();
}
}

fredag den 4. september 2009

Extracting labels from one labelfile to another

Recently I've had an assignment where we needed to "split up" a label file.

The assignment was to:
* read thorugh a label file.
* If any labels were encountered with a number bigger then N these should be written to a new label file AND any references to these labels should be replaced in the code.

So I exported all adjustments/code made to an .xpo file, and
I took a copy of the label file.

Then I wrote a class that did the "splitting up" of the label file and the search/replace of labels in the code (.xpo file).

This made for use of an other entry in this blog, namely extracting text from a label, and for some interesting use of the TextBuffer object for search/replace.



Here you can download the code.
Use at own risk !!!

mandag den 20. juli 2009

Making a form modal i X++

Put the following code in the GLOBAL class in a new method called setFormModal:


static void setFormModal(int _thisHWND, boolean _bModal)
{
    DLL _winApiDLL;
    DLLFunction _EnabledWindow;
    DLLFunction _getTop;
    DLLFunction _getNext;
    DLLFunction _getParent;

    void local_enableWHND(int _lHWND)
    {
        int lnextWnd;
        lnextWnd = _getTop.call(_getParent.call(_lHWND));
        while (lnextWnd)
        {
            if (lnextWnd != _lHWND)
                enabledWindow.call(lnextWnd, (!_bModal));
            lnextWnd = _getNext.call(lnextWnd, 2);
        }
    }
    ;
    _winApiDLL = new DLL('user32');
    _getNext = new DLLFunction(_winApiDLL,"GetWindow");
    _EnabledWindow = new DLLFunction(_winApiDLL,"EnableWindow");
    _getTop = new DLLFunction(_winApiDLL,"GetTopWindow");
    _getParent = new DLLFunction(_winApiDLL,"GetParent");
    _getParent.returns(ExtTypes:: DWORD);
    _getParent.arg(ExtTypes:: DWORD);
    _EnabledWindow.returns(ExtTypes:: DWORD);
    _EnabledWindow.arg(ExtTypes:: DWORD, ExtTypes:: DWORD);
    _getTop.returns(ExtTypes:: DWORD);
    _getTop.arg(ExtTypes:: DWORD);
    _getNext.returns(ExtTypes:: DWORD);
    _getNext.arg(ExtTypes:: DWORD, ExtTypes:: DWORD);
    local_enableWHND(_thisHWND);
}


In the form the following methods are overridden with calls to the Global::SetformModal method:

public void run()
{
    super();
    Global::setFormModal(this.hWnd(), true);
}

public void close()
{
    super();
    Global::setFormModal(this.hWnd(), false);
}

Usefull debugging tip.

Useful debugging tip for tracking down the point where an exception is thrown.

Some times it can be difficult to determine where an exception is thrown.
Is it e.g. the validatefield method on a data source field on a form that throws the exception,
or is it the validatewrite method on the data source or even the validatewrite method on the table it self.

A useful trick to establish the point in the code where the exception is thrown, is to set a break point in line 11 in
the method add in the class Info.

















You can find the Info class at the bottom at the class subtree in the AOT.

This will stop execution and activate the debugger each time something is added to the infolog which is normally done when an exception is thrown.

Generating and running code RUNTIME.

How to create, compile and run code at RUNTIME.

In some situations the need for making "generic" code arises.

This is a little example that generates, compiles and runs code to delete the contents of table PBACustGroup.

The code is made as a job, but transforming this code into a method on a class, and calling this method with parameters that allows it to identify the table, suddenly makes us able to delete several selected tables with very little code.
It also allows for USER CONTROLLED actions, for example letting the user build a liste of tables he would like to delete, and then calling the code for each of these tables.


static void TestGenericCode(Args _args)
{
    XppCompiler XppCompiler;
    str code;
    str tableName = "PBACustGroup"; // Here we define the name of the table
;

    // A compile object is created
    XppCompiler = new XppCompiler();
    // Here the code to delete the tabel is created
code = "void x() {"+tableName+" "+tablename+"; ttsbegin; delete_from "+tableName+"; ttscommit; }";
    // And compiled
    if (XppCompiler.compile(code))
    {
        // If the compiler was satisfied with the code we run it
        XppCompiler.execute();
        // And inform the user that we deleted data
        info("Deleted all record in table "+tableName);
    }
    else
    {
        // Otherwise the compiler lets us know the code was errorneous
        info(XppCompiler.errorText());
info("fejl !!");
    }
}

Extracting text from a label.

How to extract label text.

This code shows how to get the text of a specific label for a specific module, for a specific language.

This might come in handy when constructing a text to be printed on a report, that will more than one label text.


// This code allows you to extract the label text of a specific label, for a specific module,
// for a specific language
//
// Handy when constructing text fields containing many values and labels for printing
//
// str = labeltxt("gls",element.design().languageId(),200);

str labeltxt(str _labelModule,LanguageId _languageId, LabelIdNum _labelNo)
{
    Label l;
    l = new label(_languageId);
    return l.extractString(l.name(_labelModule,_labelNo));
}

Getting field values of more than one data source when calling a menu item

When a form has called another form (the user has opened a form by clicking a button in another form), you sometimes need to get information from the calling form.

The traditional way is to use element.args().record(); but this construction only allows for transferring ONE datasource at a time.

Sometimes it would be nice to be able to transfer more than one datasource.
This is an example of how that can be done.

The code must be implemented in the

init

-method of the CALLED form:

SysSetupFormRun s;
APMObjectTable apmObjectTable;
int dataSourceNo;

// Determine value of fields APMProductId and APMModelId of APMObjectTable record
// which might have been active at the time of pressing CTRL+F4 in the previous form
// (CTRL+F4 on model field in form APMCustOverview)
s = element.args().caller();
if (s)
{
    for (datasourceNo = 1; dataSourceNo <= s.dataSourceCount(); dataSourceNo++)
    {
        if (s.dataSource(dataSourceNo).cursor().TableId == tableNum(APMObjectTable))
        {
            apmObjectTable = s.dataSource(dataSourceNo).cursor();
            break;
        }
    }
}

Tokenizing a string

How to tokenize a string.

In Java you can use an object of the stringTokenizer class to break up a string in to tokens.
Dynamics AX has a handy class called TextBuffer with which you can achieve a similar effect.
This example shows how (the code is written as a job):


static void Job7(Args _args)
{
    TextBuffer t;

    t = new TextBuffer();
    t.ignoreCase(true);
    t.regularExpressions(false);
    // Set the text to break in to tokens
    t.setText("When I find myself in times of trouble, mother Mary comes to me, speaking words of wisdom, let it be. And in my hour of darkness she is standing right in front of me, speaking words of wisdom, let it be.");

    // The delimiter to search for when finding tokens is space
    while (t.nextToken(false," "))
    {
        info(t.token());
    }
}

Dynamics AX - Getting the value of a given field regardless of tablebuffer

How to get the value of a given field regardless of which tablebuffer is active.

Foreign keys in Dynamics AX are scattered throughout the tables of the system, connecting the modules to each other.

For example ItemId fields store the item identification in a lot of tables, for storing information related to the item in question.

This example shows how you can write a generic method that gets the value of the field ItemId regardless of which table is active at runtime.

The example was originally implemented as a method on a form.

The method was able to return the value of the field ItemId regardless of which tablebuffer was active, when the form was called (element.args().record()).

public ItemId getFormItemId()
{
    common table; // Generic table buffer
    DictTable dt; // Dictionary object for handling table information



    // Get the table in question
    table = element.args().record();
    // Make DictTable object
    dt = new DictTable(table.TableId);

    return table.(dt.fieldName2Id('ItemId'));
}

Debugging and inspecting values of the fields in a form datasource

For debugging purposes code that can inspect the values of the fields in a datasource in a form or a report can be quite handy.

This is how it can be done in e.g. the

active


method of the datasource :


QueryBuildDataSource qbds;
QueryBuildRange qbr;
int dsc,dsi,rc,ri;


// Get the number of datasource on the query
dsc = queryRun.query().dataSourceCount();
// Loop over datasources
for (dsi=1;dsi<=dsc;dsi++)
{
    qbds = queryRun.query().dataSourceNo(dsi);
    info("Table: "+tableid2name(qbds.table()));
    rc = qbds.rangeCount();
    // Loop over fields
    for (ri=1;ri<=rc;ri++)
    {
        qbr = qbds.range(ri);
        info(fieldid2name(qbds.table(),qbr.field())+" value: "+qbr.value());
    }
}

Overriding SysLastValue Dynamics AX 3.0

This is an old blog entry from my old blog.

Today I solved a problem, that has long puzzled me.

From Axapta 3.0 (or was it 2.5 can't quite remember) it became best pratice to wrap reports in runbase-report classes.

For a long time it has irritated me, that if you called your report class from something else than the menu, you were having trouble overriding syslastvalue, without clearing all saved last value completely.

Example:
You have a report called from a runbasereport class.For the class you naturally have a output menuitem, that is attached to the menu. So when you call this report, and make selections in the ranges of the query of the report, they are shown in the report dialog, and saved, so that the next time you call your report, the last used selections is being restored for you in the dialog for reuse.

However sometime you experience, that the report can be called from BOTH the menu and somewhere else like a form.
When you call the report from a form, it is customary to synchronize certain ranges in the query of the report with values found on the particular record the cursor has been placed in the form.

Then what about syslastvalue in this case. Normally they would be saved as the last used values destroying the values saved when the report was called from the menu.

So how do we make the report function normally saving queryvalues etc as syslastvalues when calling it from the menu, and avoid doing so when calling the report from e.g. a form.

The solution is actually fairly simple, and does not require a whole lot of work:

If you make an extra output menuitem for your report class, which includes a parameter indicating that this has NOT been called from the menu, by putting a value in e.g. the PARM property and test for this it is quite easy.

First you need a parameter method to set and return a flag indicating if or if not the class has been called from the menu:

boolean calledromMenu(boolean _calledFromMenu = calledFromMenu)
{
    calledFromMenu = _calledFromMenu;
    return calledFromMenu;
}

Of course you must have a class member of the boolean type called calledFromMenu.

In your

main

method in the runbasereport class you would have:

MyRunBaseReportObject myRunBaseReportObject;

myRunBaseReportObjec = new myRunBaseReportObject();
if (_args.parm() != '')
{
    myRunBaseReportObject.calledFromMenu(false);
}
else
{
    myRunBaseReportObject.calledFromMenu(true);
}
if (myRunBaseReportObject.prompt())
{
    myRunBaseReportObject.run();
}


The

getLast

method on your wrapper class (extending RunBaseReport) must be overwritten:
public void getLast()
{
    getLastCalled = true;
    inGetSaveLast = true;
    if (this.calledFromMenu()) // Do not save last value if not called from menu

    {
        xSysLastValue::getLast(this);
    }
    else
    {
        this.initParmDefault();
    }
    inGetSaveLast = false;
}


In this case the calledFromMenu() method returns either true or false according to the parameter passed from the menuitem, which indicates that the report is called from the menu.

If it has been called from the menu, we want everything to function as normal, so we get the syslastvalues. Otherwise, we call initParmDefault to setup report with query and queryRun objects etc.

It is important that super() is NOT called.

The other method to overwrite is:

public void saveLast()
{
    if (this.calledFromMenu()) // Only save last value if called from menu
    {
        inGetSaveLast = true;
        xSysLastValue::saveLast(this);
        inGetSaveLast = false;
    }
}

Actually the overwriting this method with the above code, just makes saving syslastvalues dependant on the calledFromMenu, thus NOT saving anything if the report was not called from the menu.

That's it. Now your report will function as normally saving syslastvalues when called from the menu, but not doing so when you call it using the other menuitem which indicates a call from somewhere else than the menu.

Subtle but important difference between _ds.executeQuery() and ds.Research()

This is actually an old entry.

Been tumbling with a problem for the last few days.

A form in our Dynamics AX module for Preventive Maintenance Control was not behaving.

The form has "explicit" filter fields that the user can see without having to activate the form filter (CTRL+F3), for setting up filters most commonly used in an easy way.

And this is working ok. However at this customer site, the form has been adjusted so that the user can have the form refreshed automatically periodically, and when the users at the customer site were making use of the "explicit" filter combined with the AX's normal filtering (CTRL+F3), the form simply threw away the normal form filtering.

I discovered a subtle but very important difference between writing

_ds.executeQuery();

(which was the way our code was doing it)

and

_ds.Research();

The difference is that _ds.Research() will retain the filter ranges in the forms query as they are right now.
_ds.executeQuery() will NOT.

It is mentioned in the Developer's Guide, but I guess the responsible programmer hadn't noticed that one.

Developer's guide states:

"research vs. executeQuery

If you want to refresh the form with records that were inserted in a method or job that was called, then you should use research.

If you want to change the query to show other records, perhaps based on a modified filter, then you should use executeQuery."

Dynamics AX 4.0 - showing AOS instance

I Dynamics AX version 4.0 the information about on what AOS you are currently running your code, has been removed.
In version 3.0 (3-tier) the client showed the AOS instance info. With a development, a test and a production environment at a site, the missing info can be of great annoyance when you are working with clients started in multiple environments. Today I found that other developers and consultants have the same problem, and one developer even solved the problem, with the very usefull hack:

http://coolhake.wordpress.com/2008/07/22/configuration-in-title-bar/

Oh yay, saved my day.

Indicating mandatory field in a dialog (RunBase) class.

A classical problem is indicating that a field is mandatory in a dialog, when the field is not bound to a datasource/field in a datasource.

Normally fellow developers will tell you that, that is not possible.

I found a way to do this.

In your Runbase-based class you can implement the putToDialog-method e.g like this:

protected void putToDialog()
{
super();
fieldMask.fieldControl().mandatory(true);
}


where fieldMask is a DialogField object in your dialog.


This will make the field act like it was a mandatory field from a datasource in a form, showing a red-wavy line under the field, and requiring the field to have a value.


Attention:

Your class has to run on the client.If you set your class to run on the server, you get a run-time error, when the fieldMask.FieldControl()-call is made.

Getting the active company accounts programmatically

While working on a client case where custom made data export was needed,
I coded some classes to make the export.

While working with the code, I decided it would be a good idea to make the currently chosen company id a part of the file name when exporting, as the customer have several company accounts, and we need to export from all of them.

I spent a little time to find out how to get the current selected company account id and I came up with:

static void Job77(Args _args)
{
;
info(appl.company().ext());
}

So I made a function for constructing the file name and put the


appl.company().ext()


bit in that.

How to programmatically (X++) calculate a mathematical expression i Dynamics AX

This code snippet shows how to calculate a mathematical expression and get the result in X++ code (the shown code is made as a job):


static void calc(Args _args)
{
XppCompiler x;
// In str s we write the expression that we want to evaluate
Str s = "1+2*(8+4)*cos(25)";
Str result;
;
x = new XppCompiler();
if (x.compileExpr(s))
{
result = x.execute();
}
else
{
result = "Error in formula";
}
info(result);
}

Let there be light ...

Let there be sound ....
Let there be ... :)

Just created a blog to put all my dax thoughts in.

Chose this blog-system because my own ISP's
blog-software suxxx.