Showing posts with label date effective. Show all posts
Showing posts with label date effective. Show all posts

Tuesday, January 21, 2020

Date effective child form in Dynamics 2012

in a form to display the date effective filter pane you have to add this snippet in the form init method:

DateEffectivenessPaneController::constructWithForm(
        this,
        MYD_ClinicianPositions_ds);

the DateEffectivenessPaneController is responsible of creating all the user interface and the interaction.

image

if you also have a child form which is also date effective and you want it also to be filtered based on the date effective selection on the parent form you can do this:

public class FormRun extends ObjectRun
{
    SysFormSplitter_X verticalSplitter;
    DateEffectivenessPaneController effectivenessPaneController;
}

public void init()
{
    super();
    //Initialize splitter
    verticalSplitter = new SysFormSplitter_X(VSplitter, GridContainer, element, 300);

    //initialize the DateEffectivenessPaneController
    effectivenessPaneController = DateEffectivenessPaneController::constructWithForm(this, MYD_Mentor_ds);
}

then override the executeQuery method of the subform darasource:

public void executeQuery()
{
    FromDate fromDate = dateNull();
    ToDate toDate = dateMax();
    
    TransDate showAsOfDate;
    FormCheckBoxControl showAllCheckbox;
    boolean showAll;
    
    //get the selection from the parent form date effective toolbar
    showAsOfDate = effectivenessPaneController.parmShowAsOfDate();
    showAllCheckbox =  effectivenessPaneController.parmShowAllCheckbox();
    showAll = showAllCheckbox.value();
    
    if (showAll)
        this.query().validTimeStateDateRange(fromDate, toDate);
        
    if (showAsOfDate)
        this.query().validTimeStateAsOfDate(showAsOfDate);

    super();
}

Wednesday, September 4, 2019

Example of Date Filter in a form DataSource using SysQuery and DateEffectivenessCheck

The following code is an example of how to build a query range based on a Form Data Source, which displays only "daily" records.

In the example the form-dataSource is named DataSourceName is a date effective table. Depending on a check box only records should be displayed, which that are valid for todays date.

While it is quite a simple task, I want to remind how much easier and more reliable the code is if we use AX built in methods: in particular those two classes:

  • SysQuery
  • DateEffectivenessCheck
public void applyFilter()
{
    queryBuildRange qbr;

    qbr = sysQuery::findOrCreateRange(DataSourceName_ds.queryBuildDataSource(), fieldNum(DataSourceName, recId));

    if( !ShowExpiredCheckBox.checked())
    {
        qbr.value(
               strfmt(
                    DateEffectivenessCheck::queryRange(true,false,false),
                    DataSourceName_ds.queryBuildDataSource().name(), // queryBuildDataSource name
                    fieldstr(DataSourceName, ValidFrom), // table field from date
                    fieldstr(DataSourceName, ValidTo), // table field to   date
                    DateEffectivenessHelp::queryDate() // test date (MUST use the queryDate method for correct formatting), alternatively can use also: DateTimeUtil::toStr(DirUtility::getCurrentDateTime())
                 )                                              
                     );
    }
    else
    {
        qbr.value(SysQuery::valueUnlimited());
    }
}

how much more code would you need without using those two classes!!

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));
}