Wednesday, October 19, 2011

Change axapta form color based on the company

how to make a form color change in Dynamics AX based on the company that was logged in.


To do this, we need to create/override run() method in SysSetupFormRun class. 


Please refer to the code sample below. 
It shows how to change the color to red or yellow based on the company DAS or DMO.

public void run()
{
    int red;
    int yellow;
    ;


    super();


    red = WinAPI::rgb2int(255,0,0);
    yellow = WinAPI::rgb2int(204,255,0);


    this.design().colorScheme(FormColorScheme::RGB);


    switch(curext())
    {
        case "DAS":
            this.design().backgroundColor(red);
            break;


        case "DMO":
            this.design().backgroundColor(yellow);
            break;


        default:
            break;
    }
}

Find method for Dynamics AX temporary tables

how to create a find method for a temporary tables in Axapta, and the differences between normal and temporary tables in this case.

In general, each instance of a temporary table, and it's associated data, will only exist while the buffer variable used to access it is in scope.
You can point multiple buffer variables to the same instance of a temporary table by using either the .setTmpData() method or by directly assigning the buffers to each other, identically to normal tables. In this way, even if your original buffer variable goes out of scope, your data will be retained while one of the other referencing variables remains.
Be aware that static table methods -such as find()- will not work with temporary tables unless you pass through the buffer variable to the method.
For example, this method will not work on a temporary table, as the tempTable variable used is newly created and will always contain no records.


//This won't work on temporary table
public static TempTable find(AccountNum _accountNum, 
                             boolean    _forUpdate = false)
{
    TempTable tempTable;
    ;

    if(_accountNum)
    {
        tempTable.selectForUpdate(_forUpdate);

        select firstonly tempTable
            where tempTable.AccountNum == _accountNum;
    }

    return tempTable;
}

If you want to have a find() method on your temporary table, then you will need to modify it slightly to pass through a reference to our populated temporary table.

//Use this pattern instead
public static TempTable find(TempTable  _tempTable,
                             AccountNum _accountNum, 
                             boolean    _forUpdate = false)
{
    if(_accountNum)
    {
        _tempTable.selectForUpdate(_forUpdate);
        select firstonly _tempTable
            where _tempTable.AccountNum == _accountNum;
    }

    return _tempTable;
}

And we call the method this way:

TempTable     localTempTable;
AccountNum    localAccountNum;
;

...

localTempTable.setTmpData(populatedTempTable);
TempTable::find(localTempTable, localAccountNum);

if(localTempTable)
... 

Read more...

Working with dates in Dynamics AX

Axapta x++ code to work with dates. Exists others ways to do that (DateTimeUtil), but I think this is the easiest way to work with dates.


Add days to a date:
systemdateget() + 5; //Add 5 days to a date


Add months to a date (we use a Global class function):
dateMthFwd(systemdateget(), 3); //Add 3 months to a date


Add years to a date (we use the same function than the add months to a date, because there isn't a specific function to add years in the Global class):
dateMthFwd(systemdateget(), 24); //Add 2 years to a date

Filtering Excel and CSV files in a dialog

Axapta x++ code, that allow us to filter XLSX and CSV files in a dialog box...


This is useful if you want to show only this type of files in a selection.

public Object dialog()
{
    DialogRunbase       dialog = super();
    #AviFiles
    #Excel


    dialogFilename = dialog.addField(typeId(FilenameOpen));
    dialog.filenameLookupFilter(["@SYS28576",#XLSX,"@SYS100852","*.csv"]);
    dialog.filenameLookupTitle("Upload from EXCEL/CSV");
    dialogFilename.value(filename);


    return dialog;
}

Using Like in axapta x++ Query

Sometimes, we want to filter records in Dynamics AX Query, using Like criteria and we don't know how to do it... 


The way to do that is using DataAreaId as a FieldId to filter.


In this example, we want to filter the InventTable records that the ItemId begins with "01", ends with "00" and has a length of 10 chars.

Query createQuery()
{
    Query                   q;
    QueryBuildDataSource    qbdsInventTable;
    ;


    q = new Query();


    qbdsInventTable = q.addDataSource(tablenum(InventTable));


    qbdsInventTable(fieldnum(InventTable, DataAreaId))
                    .value(strfmt('(ItemId like "%1")',"01??????00"));


    return q;
}

Browse lines in a form in Dynamics AX

Axapta X++ code to browse lines in a form in Microsoft Dynamics AX.


The function updates field value of all lines of the LedgerJournalTrans form (the field is a new created field):


Also, the function returns the initial position or line in the form.


void clicked()
{
    int pos;
    ;


    super();


    pos=LedgerJournalTrans_DS.getPosition();


    LedgerJournalTrans_DS.research();


    LedgerJournalTrans.DAS_Transfer=NoYes::Yes;
    LedgerJournalTrans.update();


    while(LedgerJournalTrans_DS.queryRun().next())
    {
        LedgerJournalTrans.DAS_Transfer=NoYes::Yes;
        LedgerJournalTrans.update();
    }


    LedgerJournalTrans_DS.research();


    LedgerJournalTrans_DS.setPosition(pos);
}