X++ and the insidious dangers of addLink (SysQuery)

A frequently used method can hide twisted secrets. Thread lightly, and put parameters in the right order. Type safety not included In X++ you can use the SysQuery framework to build SQL queries at runtime. For instance the following code Query q = new Query(); QueryBuildDataSource reqTransDs = q.addDataSource(tableNum(ReqTrans)); QueryBuildDataSource reqPoDs = reqTransDs.addDataSource(tableNum(ReqPO)); reqPoDs.addLink(fieldNum(ReqTrans, PlanVersion), …

Continue reading X++ and the insidious dangers of addLink (SysQuery)

Query filters and Having filters in X++ Query objects

Query objects in X++ can have two types of filters: query filters and having filters. They can be added to the query using the addQueryFilter and addHavingFilter methods respectively. Query filters apply to individual fields. In SQL, they add conditions to the WHERE clause of the query. The following code Query queryWithQueryFilter = new Query(); …

Continue reading Query filters and Having filters in X++ Query objects

Check parameters default value in X++ with prmIsDefault

In X++ objects, getters and setters are normally combined in a single method, the name of which conventionally starts with parm: class classWithParm { private int number; public int parmNumber(int _number = 0) { number = _number; return number; } } Depending on the specific logic, it can happen that setting the value (i.e. passing …

Continue reading Check parameters default value in X++ with prmIsDefault

Using filters and ranges in X++ Query objects to avoid self-joins

TL;DR If in X++ you have two Query objects on the same table and need to find the records that would be returned by both, you can avoid doing an expensive self-join using ranges and filters. While conditions on the same table field are combined with a logical OR within a group of ranges or …

Continue reading Using filters and ranges in X++ Query objects to avoid self-joins

X++ and the dangers of converting enum to string

Be careful when converting an enum to string in X++. You would probably be tempted to use enum2Str(), but beware because this method translates the resulting string depending on the user preferred locale! PurchRFQType prt = PurchRFQType::Received; str result = enum2Str(prt); info(result); // Prints the translated label for PurchRFQType::Received; // for instance "Ricevuto" in Italian …

Continue reading X++ and the dangers of converting enum to string

Week of year, previous and following months in X++

I had been wondering what some X++ date related functions would return in special cases. For instance: what values does wkOfYr return depending on the year?what date does nextMth returns for January 31st?what date does prevMth return for March 31st? So I wrote some code to find out. Let's start from wkOfYr(): private static void …

Continue reading Week of year, previous and following months in X++

Table field info from field name in X++

You can use SysDictType to get information about a table field in X++ (Dynamics 365 F&O). For that, you will need the TableId and the FieldId: TableId tableId = tableNum(PurchTable); FieldId fieldId = fieldNum(PurchTable, AccountingDate); SysDictField dictField = new SysDictField(tableId, fieldId); SysDictType dictType = new SysDictType(dictField.typeId()); If you have the table name or the field …

Continue reading Table field info from field name in X++