As you have already noted, regardless of how many ranges are added, all of them will be added together using SQL's AND operator. In most cases, this is fine, but sometimes complex user requirements demand ranges to be added using SQL's OR operator. There might be a number of workarounds, such as using temporary tables or similar tools, but we can use the Dynamics 365 for Operations feature that allows you to pass a part of a raw SQL string as a range.
In this case, the range has to be formatted in a manner similar to a fully-qualified SQL where clause, including field names, operators, and values. The expressions have to be formatted properly before you use them in a query. Here are some of the rules:
- The expression must be enclosed within single quotes
- Inside, the whole expression has to be enclosed within parentheses
- Each subexpression must also be enclosed within parentheses
- String values have to be enclosed within double quotes
- For enumerations, use their numeric values
For value formatting, use various Dynamics 365 for Operations functions, such as queryValue() and date2StrXpp(), or methods from the SysQuery class.
Let's replace the code snippet from the previous example with the following lines of code:
qbr2.value(SysQuery::valueLike (queryValue('00005')));
with the new code:
qbr2.value(strFmt('((%1 like "%2") || (%3 = %4))',
fieldStr(ProjTable,ProjId),queryvalue('00005*'),
fieldStr(ProjTable,Status),ProjStatus::InProcess+0));
Notice that by adding zero to the enumeration in the previous code, we can force the strFmt() function to use the numeric value of the enumeration. The strFmt() output should be similar to the following line:
((ProjId like "00005*") || (Status = 3))
Now if you run the code, besides all the projects starting with 00005, the result will also include all the active projects, as shown in the following screenshot: