AX – D365FO – Advanced filtering in code X++ on Query object


So, you are creating the query programmatically (of course using the Query* classes), and you need a statement like select * from tableA where columnB = val1 or columnC = val2 just do it like:

    QueryBuildDataSource    qbds;

    QueryBuildRange             qbr;
    str                                      range;

    // initializing the query and the datasource(s) here; will go like qbds = query.datasource …


    qbr = SysQuery::findOrCreateRange(qbds, fieldNum(tableA, columnB));


    range = strFmt(‘((%1.%2 == %3) || (%1.%4 == %5))’,
        qbds.name(), fieldStr(tableA, columnA), val1,
        qbds.name(), fieldStr(tableA, columnB), val2);


    qbr.value(range);
    qbr.status(RangeStatus::Hidden);
 

You might have seen this also written with tableStr(tableA) instead of qbds.name(). This works when your datasources name is the same as the table (of course), but it will fail when this doesn’t happen, so best to keep it safe. As I was saying you could end up writing complex logic like select * from tableA where (columnB = val1 or columnC = val2) and columnD = val4.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s