Tuesday, March 20, 2012

COMVariant type to work with Excel in AX

To avoid eventual issues while working with Excel objects in X++, do not forget about COMVariant type in AX!

Lets say we output some information directly to an Excel file and want to change the current row height based on the number of strings on notes.

COM             rng;
COMVariant      rowHeightVariant;
real            rowHeight;

rng = wks.Range(strFmt("A%1", curRow));   cell.Value2(_route.OprNum);
// to keep the current row height
rowHeightVariant = rng.RowHeight();
rowHeight = rowHeightVariant.double();
//to adjust the row height accordingly to the number of strings in notes

Unfortunately, I had spent so much time trying to understand the reason of errors until I found this posting. Thank you Max!

Tuesday, March 6, 2012

RouteId Lookup

In AX2012 we cannot input a new route number manually because the type RouteId has a direct reference to RouteTable.

To solve this issue I created a new extended type WmpRouteIdBase and a related lookup form WmpRouteIdLookup similarly to BOMIdBase type.

The final step is to rewrite promptCreateRoute method on RouteTable:

if (! inventTable.inventItemType().canHaveRoutes())
        throw error(strfmt("@SYS22874",inventTable.ItemId));

    dialog = new Dialog("@SYS25123");
    if (manual)
        // to allow to input the route id manually
        fieldNumber = dialog.addField(extendedtypestr(WmpRouteIdBase),"@SYS21709");
    fieldName       = dialog.addField(extendedtypestr(Name),"@SYS6303","@SYS50977");

The whole project you download from here.

Thursday, March 1, 2012

How to make a temporary instance of a database table to be shown on the form

This short code shows how to work with a temporary table without creating it in AOT.

// how to use temporary table
// method Init of the form
public void init()
    WmpInventNutrition        nut; // regular table in AOT
    WmpInventNutrition        tmp; // instead of creating in AOT a temporary table
                                   // we can create a temporary instance of the preivous regular table

    // here we make it temporary;
    // it will disappear from memory when loses the scope
    // in other words, when we close the form

    // simply selecting some of records from the regular table
    while select nut
        where nut.ItemId like '_wmp*'
        // and putting them in the temporary one
    // finally to show them on the form
    // we set the form data source to the temporary table

Alternatively, if you know exactly how the field match, it will be faster to use insert_recordset:

 // simply selecting some of records from the regular table
    insert_recordSet tmp (itemid) select ItemId from nut where nut.ItemId like '_wmp*';