Friday, November 17, 2017

Dynamics 365 - Comparison operators on enum values


As you know, in D365 the comparison (<, >, <=, >=) operators are no longer supported in certain scenarios that have to do with base enums. When you come across a situation like that, there are three different ways in which you can rewrite it:

Example situation
Select all SalesLines within my order that have a status greater than ‘backorder’.

select firstonly salesLine
where salesLine.SalesId = _salesId
&& salesLine.SalesStatus > SalesStatus::Backorder; // no longer supported in D365

Rewrite strategy #1 – explicit inclusion


Explicity mention each of the enum values you wish to include in your selection.

select firstonly salesLine
where salesLine.SalesId = _salesId
&& (salesLine.SalesStatus == SalesStatus::Delivered ||
salesLine.SalesStatus == SalesStatus::Invoiced ||
salesLine.SalesStatus == SalesStatus::Canceled);

Rewrite strategy #2 – explicit exclusion


Explicity mention each of the enum values you do NOT wish to include in your selection.

select firstonly salesLine
where salesLine.SalesId = _salesId
&& salesLine.SalesStatus != SalesStatus::None 
&& salesLine.SalesStatus != SalesStatus::Backorder;

Rewrite strategy #3 – ordered list


Use the “ordered list pattern”. This requires a lot of additional overhead so this is not the preferred way. In case you feel rewrite strategy 1 & 2 are not suitable in your particular scenario, you can consider using this third approach. Also, in situations where we have added our own enum elements to a standard AX enum, and the standard codes uses a comparison operator on that enum somewhere, this third approach is the only way that will work. Check these standard AX classes for an example (PU8 and onwards): InventTestBlockProcessComparer, InventTestBlockProcessOrderedList, InventQualityManagementBlock

No comments:

Post a Comment