![]() |
#1 |
Участник
|
goshoom: Expression builder in AX 7
Источник: http://dev.goshoom.net/en/2016/10/ex...ilder-in-ax-7/
============== If you want to allow users configure certain conditions by themselves, considering using the Expression Builder control. This is how it looks like: ![]() It exists already in AX 2012, where you can add it through the ManagedHost control. AX 7 doesn’t support managed controls anymore, but the Expression Builder has been redesigned and it’s now available as a native AX control. ![]() The control allows you to add and remove conditions and combine them with AND and OR operators. You can select fields, possibly from several tables. ![]() Then you choose an operator – which operators are available depends on field’s data type. ![]() And finally you select a value. It will give you a lookup for available values, if applicable. Some data types are handled in a special way. For instance, fields with data types extending Money gets an additional field for currency, and the amount is converted to the right currency for comparison at runtime. ![]() Dealing with dates is even more complex. You can either pick a fixed date: ![]() or define a date relatively to the current date or the current month: ![]() In addition, Expression Builder understands surrogate keys, expands financial dimension fields to individual dimensions and has a support for hierarchies. If you want to see an example in the standard AX application, look at Organization administration > Workflow > Work item queue assignment rules. A bit of technical details To be able to use your own tables in Expression Builder, you have to define an AOT query (that’s where fields are taken from) and a class (“document”) inhering from WorkflowDocument. If you want to add Expression Builder to your own form, you have to add a little bit of code, most importantly to indicate which document class (and – indirectly – which query) it will use. Expression Builder has ExpressionDocumentClass() method for that. Then you also need some logic for loading and saving expressions – look at an existing implementation for details. The document class can have a few useful attributes and it can also define calculated values, which are then displayed in the list among normal fields. Calculated fields are important – not only they allow you to do any kind of calculation, but you can also use them to work around limitations of what expression you can write. Consider this scenario: You want to define a condition for ShippingDate > Deadline. How can you put the Deadline field on the right side of an expression in expression builder? You can’t (or at least I haven’t figured out how), but you can create a calculated field, such as DaysToShipDeadline, defined as Deadline – ShippingDate. Then it’s trivial to configure the condition as DaysToShipDeadline < 0. When your expression is defined and saved, you can call Expression::evaluate() to see if the condition it true or false for a given record: ExpressionResultType result = Expression::evaluate( 'usmf', // Company tableNum(MyTable), // Table where is the record to check 5637144576, // ID of a record in MyTable expressionId, // From ExpressionTable ExpressionDataSources::newExpressionDataSources()); This indicates a problem with how conditions are evaluated. Because you always provide only TableId and RecId, the evaluation logic must always make a query to database. If you need to run it once, it’s not a big deal, but if you’re evaluation many expressions, it may become a problem. To make it worse, methods for computed fields and the currency convertor also get only TableId and RecId, making additional DB queries. Caching helps a bit, but it’s still all quite expensive. I consider making a child expression class accepting a temporary buffer (or buffers) instead of just a record ID; the first prototype suggests it’s feasible and worth the effort. If you’re interested in inner workings of the evaluation, let me give you a brief overview:
It looks useful, doesn’t it? Источник: http://dev.goshoom.net/en/2016/10/ex...ilder-in-ax-7/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|