Thursday, 14 May 2009

Import csv job

Here is an import job for importing tuples from a csv file:


static void importJob(Args _args)
{
    FileIOPermission        permission;
    AsciiIo                 file;
    Filename                fileName = @"c:\test\test.csv";
    CustTable               custTable;
    container               line;
    #define.fieldDelimiter(";")
    #define.recordDelimiter("\n")
    #define.read("r")
    ttsBegin;
    permission  = new FileIOPermission(fileName, #read);
    permission.assert();
 
    file = new AsciiIo(fileName, #read);
    file.inFieldDelimiter(#fieldDelimiter);
    file.inRecordDelimiter(#recordDelimiter);
 
    if(!file || file.status() != IO_Status::Ok)
    {
        throw error(strFmt("@SYS76826", file));
    }
 
    while(file.status() == IO_Status::Ok)
    {
        line = file.read();
     
        custTable.AccountNum = conPeek(line, 1);
        custTable.insert();
    }
    ttsCommit;
     


}
Happy hacking

Saturday, 2 May 2009

Opening a form on start up of AX

I've recently implemented a variation of Palle Agermark's post on how to run a form on start up of AX. He opens a form through calling the Display menu item. I needed was to call a form with conditions, so I created a class which would handle the conditions in question and instead of calling the menu item I called the main method of the class




Thanks to GregP for his helpful tips and insisting that he could get it to work.

EDIT: There is a small aber dabei ( which roughly translates "there is a but", I just happen to like the German term better ). In order for the AutoRun to function, development rights for the user-group are necessary, as the function we utilize is typically used to automate installation/configuration. Giving the user group full development rights though is not an option in this scenario, so after some fiddling about I found that the only security setting the user group needs is SysLastMinuteCheck




which, by the way, only can be set giving the user group full development right and then removing all but this SysLastMinuteCheck.

Tuesday, 17 February 2009

How to tell if a form was opened with 'Go to main table' function

I found this sample code really helpful in connection with a customization,where the customer wanted to alter the 'Go to main table' function.

Respect to original poster (though I can't remember where I found it - but the idea is not mine)

On main datasource:
public void executeQuery()
{
query q;
querybuilddatasource qbds;
querybuildrange qbr;
int fldLookupField;
str sLookupValue;
;
if ( element.args().caller() &&
!element.args().dataset() &&
element.args().lookupField() &&
element.args().lookupValue()) //jumpref
{
sLookupValue =element.args().lookupValue();
fldLookupField =element.args().lookupField();

q=this.query();
qbds=q.dataSourceTable(this.table());
qbr=qbds.addRange(fldLookupField);
qbr.value(sLookupValue);
this.query(q);
}
super();
}

Friday, 13 February 2009

Invoking a method on a field in a form's datasource

I needed to call the modified method on a form's datasource's field to invoke the logic, which updated a couple of fields on the form. I had defaulted a value in the field on the table's initValue()-method but this - of course - means that the modified method is not in play.
Therefore I needed to call the method in code, for which I had two options, either calling the form controls method by setting AutoDeclaration to Yes and then calling modified or calling the datasource table's field's method which I chose.

e.g. ProdTable_ds.object(fieldnum(ProdTable, ItemId)).modified();

but where to place the code? After some time I found out that if I placed it on active() of the datasource the form showed the correct information.

/Happy hacking

Monday, 5 January 2009

Show current configuration in title bar of windows

This code snippet shows the current configuration of the AX client you have opened. Really helpful if you have several different AX clients opened at the same time.

void workspaceWindowCreated(int _hWnd)
{
SqlSystem sqlSystem = new SqlSystem();
LoginProperty loginProperty = sqlSystem.createLoginProperty();
;

// Put workspace window specific initialization here.

// Show application details in title bar
if (loginProperty)
{
WinAPI::setWindowText(_hWnd, strFmt("%1 - %2@%3 (%4)", WinAPI::getWindowText(_hWnd), loginProperty.getDatabase(), loginProperty.getServer(), this.currentAOLayer()));
}
}

Monday, 20 October 2008

RunBase - classes and Best Practice

There are certain classes which serve as excellent templates. One such class the LedgerExchAdj-class. If only all runbase-classes were as this one. In my opinion its simplicity and readability is top-notch. I've edited the class-specific code so that only the run() template is present, which included that all important error handling, which one might forget



public void run()
{
#OCCRetryCount
;

try
{
ttsbegin;
// Insert code here
ttscommit;
}

catch (Exception::Deadlock)
{
retry;
}

catch (Exception::UpdateConflict)
{
if (appl.ttsLevel() == 0)
{
if (xSession::currentRetryCount() >= #RetryNum)
{
throw Exception::UpdateConflictNotRecovered;
}
else
{
retry;
}
}
else
{
throw Exception::UpdateConflict;
}
}
}

Friday, 3 October 2008

Setting Cross-reference update in a Schedueld task

An updated Cross reference is an indispensable tool when developing for AX but the client-side dependency can be annoying. So after a little research I found the command line parameter/Windows build-in Scheduled task combo and it does the trick.

In the class SysStartupCmd's construct the command line parameter compileAll-startup command the the parm _+ instantiates a complete compile and cross reference update which means that if you set-up a Scheduled task in Window's standart scheduled task tool with the parameter -startupcmd=compileAll_+ you can update when it suits your development team best.