tirsdag den 7. oktober 2014

Returning a weeknumber from the standard calendar lookup form

A customer had a requirement for setting a week number for approximate delivery of purchased goods on shipment as an indication for the sales department of when the goods will be arriving.

An integer field had been introduced on the table in question, and the customer wanted to be able to do a lookup in the standard calendar, but wanted a week number to be returned in stead of a date.

I solved it like this:

For the field that was to contain ETAWeek there was an Extended Data Type called ETAWeek.
On the EDT I put SysDateLookup in the FormHelp property

In the Form SysDateLookup I added a boolean variable in Classdeclaration.

boolean calledFromIntegerField;

3. In the Form SysDateLookup - method init I added check to see if the SysDateLookup form was called from an integer form control.

if (formRun.selectedControl() is FormIntControl) 
    calledFromIntegerField = true;

4.In the Form SysDateLookup - method closeSelect I added an extra else if block in the bottom to handle returning the week number.

else if (calledFromIntegerField)

Now the SysDateLookup works for the ETAWeek field.
UI design wise you can set the "ButtonImage" property on the Extended Data Type ETAWeek mentioned above to the value Calendar to get an calendar icon beside the field.

Dynamics AX 2012 R2 - Export to Excel command button

Today I had some trouble getting a normally simple thing to work in an AX form.

The form consists of to synchronized grids, and the user wanted an "Export to Excel" command button, so the active grid can be exported to Excel.

Normally this is very simple as you just need to add a command button to the form in the ActionPane somewhere and you're home free.

Not this time. It didn't work.

I googled and found this:


and put a breakpoint in the performPushAndFormatting method.
I didn't reach my breakpoint and Excel didn't even start.

Then I started to investigate the form.
Maybe the tables of the datasources of the form had some obscure property that needed tweaking, but no.

After pondering this for a while I found that the designer of the form had dropped a field group containing all the fields in the grid. Could that be the problem ?
Clearing the "DataGroup" property of the field group, allowed for dragging all the fields from the field group to the Grid node in the design, and presto, the Export button now worked like a charm.

Lesson learned. If you have a grid in your form, and you want to be able to use an "Export to Excel" command button, using field groups in the grid is a bad idea.

I experienced this on a Dynamics AX 2012 R2.

fredag den 2. maj 2014

Dynamics ax 2012 traversing selected records in a form data source

A classical developer challenge in Dynamics AX is to enable a form button when multiple records have been selected in the form by the user.

This usually involves writing some form of loop (for or while or do-while) that starts out with calling _ds.getFirst() and continuing the loop as long as _ds.getNext() returns a tablebuffer.

Well things got a little bit easier in AX 2012. In AX 2012 you can use the MultiSelectionHelper class.

One example is the following that I encountered in AX 2012:

Can you make the customer collection letter print out run for each selected collection record in the
Print/Post collection letters form (Accounts receiveable / Periodic / Collections / Print/Post Collection letters).

If we ignore the possibility for setting up print destination for running each report we can do this in two steps:

1) Change the "Multiselect" property of the "MenuButton" and the "Menuitembutton" in the MenuButton in the form from "Auto" to "Yes".

2) Change the CustCollectionJourController used to run the report to handle multiselected records from the form.
 I chose to implement it this way:

2.1) A new method "isMultipleSelected" was added to the class.
private boolean isMultipleSelected(Args _args)
    MultiSelectionHelper msh;
    int i;
    CustCollectionLetterJour cclj;

    if (_args.caller() is SysSetupFormRun)
        msh = MultiSelectionHelper::createFromCaller(_args.caller());
        cclj = msh.getFirst();
        while (cclj)
            if (i>1)
            cclj = msh.getNext();
    return i>1;


The method is takes the args passed from the menuitem / form and checks if it is a SysSetupFormRun object. If it is a MultiSelectionHelper object is instantiated.
This can help traverse the selected records in the datasource, and we simply traverse it until we're certain that more than one record is selected.

The following code

                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, CollectionLetterNum)).value(selectedRec.CollectionLetterNum);
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, AccountNum)).value(selectedRec.AccountNum);
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, Status)).value(enum2str(selectedRec.Status));
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, CollectionLetterCode)).value(enum2str(selectedRec.CollectionLetterCode));

is substituted for:

if (this.isMultipleSelected(this.parmArgs()))
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, CollectionLetterNum)).value(selectedRec.CollectionLetterNum);
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, AccountNum)).value(selectedRec.AccountNum);
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, Status)).value(enum2str(selectedRec.Status));
                SysQuery::findOrCreateRange(ds, fieldNum(CustCollectionLetterJour, CollectionLetterCode)).value(enum2str(selectedRec.CollectionLetterCode));


So MultiSelectionHelper can not only help you traverse the selected record, it can actually build a queryRange for your query based on the selected records in your form by a call to the createQueryRanges method. :)

Nice one.

onsdag den 23. april 2014

Dynamics AX 2012 ValidTimeState tables and form changing view from current to all

Valid Time State tables are new i AX 2012 a gives the developer the possibility to easily create tables that hold e.g. current setup data for various purposes, and at the same time keeping a "history" of the changes of the data in the table.

For more reading:

I was tasked with doing a setup table with rates for calculating Vendor Bonus and I chose to base this a valid time state table.

The customer asked for a button on the form, where you maintain the vendor bonus calculation setup data, so you could toggle viewing "Current setup" or "All setup" records (changing the view from actual to all records and vice versa in the form).

I found that you can not change the ValidTimeStateAutoQuery property on the form data source in a form at run-time. It simply does not change anything, so I came up with the following solution:
A boolean class member in the classdeclation method of the form:

boolean showCurrent;

containing which "kind" of view is currently used in the form.

A button that changes the view and on the clicked method of the button:

void clicked()
    showCurrent = !ShowCurrent;
    this.text(showCurrent ? "@SYS38980" : "@NDI892");
    this.helpText(showCurrent ? "@NDI893" : "@NDI894");

And finally on the executeQuery method on the datasource using the valid time state table:

public void executeQuery()
    if (showCurrent)

I encountered a strange little quirk when I was testing using jobs that made lookups on my table. Microsoft has introduced a keyword for the select command called validTimeState which is used for specifying on which date you want to select records from the table.

Supposing you have a valid time state table called VendorBonusTable and you write a piece of code that get records on a specific date, you can not use:

VendorBonusTable v;

select validTimeState(systemDateGet()) v;

This will simply not compile.

But you can however do:

VendorBonusTable v;
date d = systemDateGet();
select validTimeState(d) v;

which I personally think is a bit weird.

fredag den 28. marts 2014

Show elements in a model from the ABOUT Dynamics AX form

Being in a situation where I needed to determine if a certain AOT-element was included in an installed model, I wonder why Microsoft didn't link the form showing installed models (Help / About Microsoft Dynamics AX / (link) Show installed models) or (Tools / Model management / Models installed) with the form showing Elements in installed models (Tools / Model management / Model elements.

Opening the About Dynamics AX form you are able to access the list of installed models in the application, but you can not drill down to the elements included in the model. You'll have to go to the other form and make a filter to view the elements in a model.

So I quickly made the attached code project, which simply adds a button (Elements) to the Installed models form, so that you can see a list of the elements included in the model.

In the element list you can open the cross reference for each element, to make further drill down as to which elements are using the element included in the installed model with the button (Used by) which opens the cross reference for the chosen element. Be aware however that the cross reference form might "hide" behind the Model windows as it is workspace Window.


As always - use at own risk.

fredag den 14. februar 2014

A little extension for diagnosing tax code combination errors

In Dynamics AX 2012 if General Ledger module is setup to validate Check sales tax by entering either "Warning" or "Error" in the "Check sales tax groups" field, then Dynamics AX will throw an error (stop execution) or issue a warning in the infolog if it encounters a tax setup error when doing calculation of tax.

The error looks like this:

As you might know the tax calculation is based on getting the intersecting tax codes connected to the item tax group and the tax group of a given source document line.

In this case I encountered we wanted to diagnose the origins of the error, because the error occurred when changing a customer group, which we thought was a bit odd.

Digging a bit deeper, the error was not related to changing the customer group, but instead related to the following recalculation of credit limit on the customer that Dynamics AX wanted to perform.

I traced the error to the class Tax and the method insertInterSection.

Seing that the Tax class is a super class the reason for the not very informative error message became clear. The insertIntersection method is inherited by the sub classes in the Tax class hierachy and therefor contains logic that is not module specific, so there are no references to the origin-lines (e.g. sales lines og purch lines) that the tax group set up comes from.

To get info about the origins of the error I did the following:

I added a new method to be used when an error is to be reported:

private str invalidTaxCodeCombErrortxt(ErrorTxt _errorTxt)
DictTable dictTable;
Common dCommon;
ErrorTxt foundInErrorTxtPostfix;

dictTable = new dictTable(this.sourceTableId());
dCommon = dictTable.makeRecord();
select firstOnly dCommon where dCommon.recid == this.sourceRecId();
switch (dCommon.TableId)
case tableNum(SalesLine):
foundInErrorTxtPostfix = strFmt(" found in : %1 linje %2",dCommon.(fieldNum(SalesLine,SalesId)),dCommon.(fieldNum(SalesLine,LineNum)));
case tableNum(PurchLine):
foundInErrorTxtPostfix = strFmt(" found in : %1 linje %2",dCommon.(fieldNum(PurchLine,PurchId)),dCommon.(fieldNum(PurchLine,LineNumber)));
foundInErrorTxtPostfix = '';
return _errorTxt + ' ' + foundInErrorTxtPostfix;

The method attempts to evaluate a generic tablebuffer to determine what is the source of the tax codes being evaluated, and which cause an invalid combination.
It does so by instantiating a DictTable object using the table id found in the tax class (this.sourceTableId()). It then does a call to the kernel method makeRecord() on the same object to get a tablebuffer.
The we do a select firstonly on this tablebuffer using the recid in (this.sourceTableId()) to get the record that is the origin of the tax groups.

The it evaluates the table id of the found record and constructs and error message text, that can be post fixed to the standard error message.

To notice in the


construct which actually let's you access the fields of a common table buffer.

The InsertIntersection method is changed so that the switch case construct in line 175 looks like this:

switch (this.taxParameters().CheckIntersection)
    case CheckTaxGroups::Warning :
        //warning(strFmt("@SYS75213", _taxGroup, _taxItemGroup));
        warning(this.invalidTaxCodeCombErrortxt(strFmt("@SYS75213", _taxGroup, _taxItemGroup)));

    case CheckTaxGroups::Error :        //warning(strFmt("@SYS75213", _taxGroup, _taxItemGroup));
        warning(this.invalidTaxCodeCombErrortxt(strFmt("@SYS75213", _taxGroup, _taxItemGroup)));
effectively adding more info to the error message.

This way I was able to pinpoint the source of the problem: An open sales order line on the customer we edited was missing an item tax group.