Tuesday, August 27, 2019

Effective date Form datasource filter Queryrange using check boxes to select expired active future records in Dynamics Ax

I had a requirement to create create a form where the date effective selection appears as check boxes.

image

I found a similar implementation in LogisticsPostalAddress, so here the code I will show you below based is on what you can see in \Forms\LogisticsPostalAddress\Data Sources\LogisticsLocation\Methods\executeQuery

1) override the click event of each checkbox like this:

public void clicked()
{
    super();

    element.setEffectiveDateFilter();
}

2) add this method

here I added some logic to check uncheck based the checkboxes to avoid invalid selections

public void setEffectiveDateFilter()
{
    if (DisplayExpired.value() && !DisplayActive.value() && DisplayFuture.value())
        DisplayActive.value(true);
    if (!DisplayExpired.value() && !DisplayActive.value() && !DisplayFuture.value())
        DisplayActive.value(true);

    MYTable_ds.executeQuery();
}

3) override the executeQuery of the datasource

public void executeQuery()
{
    //based on the template: \Forms\LogisticsPostalAddress\Data Sources\LogisticsLocation\Methods\executeQuery
    QueryBuildRange qbrValidFrom, qbrValidTo;
    RecId curRecordRecId;
    ValidFromDate validFrom = Global::dateNull();
    ValidToDate validTo = Global::dateMax();
    boolean expired;
    boolean active;
    boolean future;
    str queryRangeStr='';

    // Get the record currently selected
    curRecordRecId = MYTable_DS.cursor().RecId;

    expired = DisplayExpired.value();
    active = DisplayActive.value();
    future = DisplayFuture.value();

    qbrValidFrom = SysQuery::findOrCreateRange(this.query().dataSourceNo(1), fieldNum(MYTable,ValidFrom));
    qbrValidTo   = SysQuery::findOrCreateRange(this.query().dataSourceNo(1), fieldNum(MYTable,ValidTo));

    if (expired && !active && !future)
    {
        queryRangeStr = '(%1.%3 <= %4)';
        MYTable_ds.query().validTimeStateDateRange(Global::dateNull(), systemDateGet());
    }
    else if (!expired && active && !future)
    {
        queryRangeStr = DateEffectivenessCheck::queryRange(true,false,false);
        MYTable_ds.query().resetValidTimeStateQueryType();
    }
    else if (!expired && !active && future)
    {
        queryRangeStr = DateEffectivenessCheck::queryRange(false,false,true);
        MYTable_ds.query().validTimeStateDateRange(systemDateGet(), dateMax());
    }
    else
    {
        queryRangeStr = DateEffectivenessCheck::queryRange(true,true,true);
        validFrom = element.calcValidFrom();
        validTo = element.calcValidTo();
        //Debug::printDebug(strFmt('validFrom %1; validTo: %2', validFrom, validTo));
        MYTable_ds.query().validTimeStateDateRange(validFrom, validTo);
    }

    MYTable_ds.validTimeStateUpdate(ValidTimeStateUpdate::Correction);

    if (queryRangeStr)
        {
            qbrValidFrom.value(
                strFmt(queryRangeStr,
                    this.query().dataSourceTable(tableNum(MYTable)).name(),
                    fieldStr(MYTable,ValidFrom),
                    fieldStr(MYTable,ValidTo),
                    DateTimeUtil::toStr(DirUtility::getCurrentDateTime())
                )
            );
        }


    super();

        //focus again on the previously selected value
    MYTable_DS.findValue(fieldname2id(MYTable.TableId, 'RecID'), int642str(curRecordRecId));
}

No comments:

Post a Comment