Alex Kwitny
Alex Kwitny

Reputation: 11564

Trying to join firstonly using AX query object

My request is that a client wants to put in a date range (typically a month) and pull all general ledger journals that have at least one line posted in that date range. They will post journals in March for January's period for example, and want to know which journals have that data.

The basic idea is LedgerJournalTable to a firstonly LedgerJournalTrans. I'm not the best with the query object. Why isn't my query working? It's returning multiple journals instead of just one. I was thinking I could group by and be OK, but I'd think this would work.

static void Job38(Args _args)
{
    Query                   q;
    QueryRun                queryRun;
    QueryBuildDatasource    qbd;
    QueryBuildDatasource    qbd2;
    QueryBuildRange         qbr;
    LedgerJournalTable      ledgerJournalTable;
    ;

    info(strfmt("%1", date2strxpp(str2date('10/01/2011', 213))));

    q = new Query();
    qbd = q.addDataSource(tablenum(LedgerJournalTable));
    qbd2    = qbd.addDataSource(tableNum(LedgerJournalTrans));
    qbd2.relations(true);
    qbd2.firstOnly(true);
    qbd2.joinMode(JoinMode::InnerJoin);
    qbr = qbd2.addRange(fieldNum(LedgerJournalTrans, TransDate));
    qbr.value(strfmt('(TransDate > %1) && (TransDate < %2)', Date2StrXpp(str2date('10/01/2011', 213)), Date2StrXpp(str2date('10/31/2011', 213))));

    queryRun = new QueryRun(q);

    while (queryRun.next())
    {
        ledgerJournalTable = queryRun.get(tableNum(LedgerJournalTable));

        info(strfmt("%1", ledgerJournalTable.JournalNum));
    }
}

Upvotes: 1

Views: 6936

Answers (1)

Jan B. Kjeldsen
Jan B. Kjeldsen

Reputation: 18061

Have you tried JoinMode::ExistsJoin?

Upvotes: 2

Related Questions