Wednesday, June 30, 2010

How to hide/unhide the task pane from code

Small post, with a small job to do: How can we hide or unhide the task pane in Ax from code? (You know, that pane on the right that some love and others hate.)

This small piece of code will do the trick


static void TaskPaneSet(Args _args)
{ boolean visible=true;
;
new xInfo().taskPane().setVisiblity(visible);
}

How to translate an enumerated text

You can easily convert an enumerated text to a string value with the enum2str function. Example:

info(enum2str(SalesType::Sales));

But this will get you the label in the default language. What about other languages?
The following small job will get you the translated label info for all values of a specified enum, with a specified language.

static void Enum2Label(Args _args)
{   DictEnum                dictEnum;

int                     valueIndex;
LanguageId              languageId='de';

int                     enumId=enumNum(SalesType);
str                     labelId;
;

dictEnum = new DictEnum(enumId);
if (dictEnum)
{
for (valueIndex = 0 ; valueIndex < dictEnum.values(); valueIndex++)
{
labelId = dictEnum.index2LabelId(valueIndex);
info(SysLabel::labelId2String2(labelId, languageId));
}
}
}
Result: Journal Angebot Dauerauftrag Auftrag Zur├╝ckgegebener Auftrag Abrufauftrag Artikelbedarf

Similar code as above is used in the SRS classes in Ax. They make it possible for humans to understand and interpret the different enum values, as they merely are integer values in the database and as such for third party applications.

How to create complex SQL statements in Ax

I posted a reply to a forum discussion the other day, but somehow the post never showed up in the discussion. So I'm dedicating this blog post to the issue.

Ax comes with it's own set of supported SQL commands. You can do your basic select statements, sorting, grouping, counting etc
But there are things with SQL you cannot do in Ax. Like this

select SUM ( A * B) from myTable

Of course, you can create a while select statement, performing the sum record by record. But this comes with a performance penalty.

There is an alternative. Ax allows you to connect to the database, writing your own SQL statements.
We need to setup a new database connection, assign permissions and perform our own custom SQL statement.

Example:
server static void TestSumSelect()

{   Connection      connection;
Statement       statement;

str             sql;
ResultSet       resultSet;

SqlStatementExecutePermission perm;
;

connection = new Connection();
sql = strfmt( "SELECT SUM(QtyOrdered * SalesPrice) FROM SALESLINE where SalesId='xyz'");

perm = new SqlStatementExecutePermission(sql);
perm.assert();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql); 

while(resultSet.next())
{
info(num2str(resultSet.getReal(1),0,2,1,0));
}

CodeAccessPermission::revertAssert();
}




In order for this to work, the code must be executed on the server. (Encapsulate this in a class of your own.)
You can supply additional parameters (a salesid for example).