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.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...