torsdag den 9. april 2015

Dynamics AX 2012 AOT add-in copying field list to clip board

Before Dynamics AX 2012 and the new editor which makes things a bit more Visual Studio like, we had some features in the AOT, that are gone now but I miss.

I *might* have rambled and raved about this before (http://gotdax.blogspot.dk/2012/03/dynamics-ax-2012-annoyances-for-old.html) ;-).

E.g. I miss the ability to mark all fields on a table in the AOT and simply copy them to code.
In the olds days this could be done by simply marking the fields in AOT and dragging them to the editor window.

I needed this and got fed up with having to type the field names my self so I made a little class with the main method:

public static void main(Args _args)
{
    #AOT

    DictTable   dt;
    DictField   df;
    int f,start,end;
    Set s;
    SetEnumerator se;
    int tableno;
    TextBuffer txtb = new TextBuffer();
    str fieldNameList;
    str tableName;
    TreeNode treeNode;
    str searchFor = 'Path: '+#TablesPath+#AOTDelimiter;
    #define.FIELDS('\\Fields')

    if (SysContextMenu::startedFrom(_args))   // started from SysContextMenu
    {
        treeNode = _args.parmObject().first();
        _args.record(xRefPaths::find(treeNode.treeNodePath()));
    }
    else    // started with a button (or from a menu!)
    if (_args.dataset() == tablenum(UtilElements))
    {
        treeNode = xUtilElements::getNodeInTree(_args.record());
        _args.record(xRefPaths::find(treeNode.treeNodePath()));
    }
   
    if (strScan(treeNode.toString(),#TablesPath,1,1024))
    {
        start = strScan(treeNode.toString(),searchFor,1,1024)+strLen(searchFor);
        if (strScan(treeNode.toString(),'\\Fields',1,1024))
        {
            end = strScan(treeNode.toString(),#FIELDS,1,1024);
        }
        else
        {
            end = strScan(treeNode.toString(),' Layer: ',1,1024);
        }
        tablename = subStr(treeNode.toString(),start,end-start);
        s = new Set(Types::String);

        if (tableName)
        {
            tableno = tableName2id(tableName);
            dt = new DictTable(tableno);
            if (dt)
            {
                for(f=1;f<=dt.fieldCnt(tableno);f++)
                {
                    df = new DictField(tableno,dt.fieldCnt2Id(f));
                    if (!df.isSystem())
                        s.add(strFmt("%1",dt.fieldName(dt.fieldCnt2Id(f))));
                }
                se = s.getEnumerator();
                while (se.moveNext())
                    fieldNameList += (fieldNameList ? "\n" : "") + se.current();

                txtb.setText(fieldNameList);
                txtb.toClipboard();
                info("Field names copied to clipboard.");
            }
        }
    }
}



And dragged the class to the Menu Items \ Action to create the action menu item:
AOTCopyFieldNamesToClipBoard.

I then added this menu item to the SysContextMenu:


Then I can go to a table in the AOT and right click and choose add-ins / Copy field names to clipboard.




and afterwards just paste into word or excel:


I think its useful for making documentation.

You can download a private project from:

https://onedrive.live.com/redir?resid=9b63d38f981ffd1b!80242&authkey=!AH8DxBaNZUc1LTg&ithint=file%2cxpo

onsdag den 25. marts 2015

Calculating easter sunday in different ways


Once a long time ago I found an interesting document on the inter web, describing different calendar systems.

From that I made a php-class for my website that can calculate easter sunday in the gregorian calendar. Then it is easy to calculate the rest of the danish holidays as they are offset according to easter sunday (except of course those that have a fixed date).

I translated that class in to x++.

You can find it here:

https://onedrive.live.com/redir?resid=9B63D38F981FFD1B!39710&authkey=!ABv0yYPULV3ReIE&ithint=file%2cxpo

Today I got talking with a colleague about calculating easter sunday so he mentioned that he had also made a version of the calculation:

http://stackoverflow.com/questions/11048524/how-to-calculate-easter-sunday-in-x

So of course - being a bit nerdy - we just *had* to check if the routines arrived at the same result. So we came up with:


static void EasterTest(Args _args)
{
    Yr x;
    date easter(Yr yr) // Påskedag / påske søndag
    {
        int g,c,h,i,j,l;
        int easterday, eastermonth;
        g = yr mod 19;
        // Gregorian Calendar
        c = real2int(rounddown(yr / 100,1));
        h = (c - roundDown(c/4,1)-rounddown(((8*c)+13)/25,1)+(19*g)+15) mod 30;
        i = h - rounddown(h/28,1)*(1- rounddown(29 / (h+1),1) * rounddown((21-g) / 11,1));
        j = (yr + (rounddown(yr/4,1))+i+2 - c + (rounddown(c/4,1))) mod 7;
        l = i - j;
        easterday = 3+rounddown((l+40)/44,1);
        return mkdate(l+28-31*rounddown(easterday / 4,1), easterday, yr);
    }
    date dateOfEaster(Yr y)
    {
        int a = y mod 19;
        int b = y div 100;
        int c = y mod 100;
        int d = b div 4;
        int e = b mod 4;
        int f = (b+8) div 25;
        int g = (b-f+1) div 3;
        int h = (19*a+b-d-g+15) mod 30;
        int i = c div 4;
        int k = c mod 4;
        int l = (32+2*e+2*i-h-k) mod 7;
        int m = (a+11*h+22*l) div 451;
        int n = (h+l-7*m+114) div 31;
        int p = (h+l-7*m+114) mod 31;
        return mkdate(p+1,n,y);
    }
    for (x = 1900; x <= 2154; x++)
        if (dateOfEaster(x) != easter(x))
            info(strFmt('E1=%1 E2=%2', dateOfEaster(x), easter(x)));
}

And the routines calculated the same dates for easter sunday.

We stopped ourselves when discussing if we should implement a tick-couting-measurement to see if my colleagues routine was faster than mine. :-)

But I think mine is slower as it uses several function calls and not pure arithmetic.

torsdag den 5. marts 2015

Forcing the Name field of a salesline to be synchronized to the purchline when using Drop shipment

Using non-stock items in the daily business can be handled in Dynamics AX 2012 by using Direct delivery.

You can use the Button "Direct delivery" from a sales order you have created, to create a matching purchase order.

However if you use the Name field on the salesline to describe the specifications of the item you want to the vendor, the standard functionality does create the matching purchase order lines so that the name of the originating sales line is also used on the sales lines.

Direct deliveries are handled so that the inventory transactions of the salesline are marked against the purchline, so I wrote this small script to be able to get the hang of how to find the direct delivery purchaselines from the saleslines records of a sales order.

static void Job235(Args _args)
{
    SalesLine   salesLine;
    PurchLine   purchLine;
    InventTransOriginSalesLine itosl;
    InventTransOriginPurchLine itopl;
    InventTrans it,it1;
    InventTable iTbl;
    InventHandlingGroup ihg;
   
   
    ttsBegin;
    while select salesLine
        where salesLine.SalesId == "1001135"
        join itosl
        where itosl.SalesLineDataAreaId == salesLine.dataAreaId
           && itosl.SalesLineInventTransId == salesLine.InventTransId
        join it
            where it.InventTransOrigin == itosl.InventTransOrigin
        join it1
            where it1.InventTransOrigin == it.MarkingRefInventTransOrigin
        join itopl
            where itopl.PurchLineDataAreaId == it1.dataAreaId
               && itopl.InventTransOrigin == it1.InventTransOrigin
        join forupdate purchLine
            where purchLine.dataAreaId == itopl.PurchLineDataAreaId
               && purchLine.InventTransId == itopl.PurchLineInventTransId
               && purchLine.Name != salesLine.name
        join itbl
            where itbl.itemid == salesLine.ItemId
         
    {       
        info("***** FØR ********");
        info(strFmt("Salg: %1 %2 %3 %4",salesLine.SalesId,salesLine.ItemId,salesLine.LineNumber,salesLine.name));
        info(strFmt("Indkøb: %1 %2 %3 %4",purchLine.purchid,purchLine.ItemId,purchLine.LineNumber,purchLine.name));
        purchLine.Name = salesLine.Name;
        purchLine.doUpdate();
        info("***** EFTER ********");
        info(strFmt("Salg: %1 %2 %3 %4",salesLine.SalesId,salesLine.ItemId,salesLine.LineNumber,salesLine.name));
        info(strFmt("Indkøb: %1 %2 %3 %4",purchLine.purchid,purchLine.ItemId,purchLine.LineNumber,purchLine.name));
    }
    ttsCommit;
}


With this code at hand I could continue to customize the PurchAutoCreate_Sales class to sync the names of the salesLines to the purchLines.

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:

1.
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

2.
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)
{
    super(num2str(weekOfYear(selectedValue),0,0,0,0);
}

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:

http://blogs.msdn.com/b/emeadaxsupport/archive/2009/09/07/how-does-the-export-to-excel-feature-work-under-the-hood.aspx

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)
                break;
            i++;
            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.
2.2)

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()))
{
    ds.clearRanges();
    MultiSelectionHelper::createFromCaller(this.parmArgs().caller()).createQueryRanges(ds,fieldStr(CustCollectionLetterJour,CollectionLetterNum));
}
else
{
                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:
http://msdn.microsoft.com/en-us/library/gg861781.aspx


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()
{
    super();
    showCurrent = !ShowCurrent;
    this.text(showCurrent ? "@SYS38980" : "@NDI892");
    this.helpText(showCurrent ? "@NDI893" : "@NDI894");
    VendorBonusTable_ds.executeQuery();
}

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

public void executeQuery()
{
    if (showCurrent)
    {
        VendorBonusTable_ds.query().validTimeStateDateRange(systemDateGet(),systemDateGet());
    }
    else
    {
        VendorBonusTable_ds.query().validTimeStateDateRange(dateNull(),maxDate());
    }
    super();
}


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.