Showing posts with label filters. Show all posts
Showing posts with label filters. Show all posts

Monday, September 27, 2021

Form has outer join on a datasource and user wants to filter for blank value on the outer joined table

 As you may have found, if you have a form with an outer joined datasource and a user wants to filter the form so that a value on the key field in the table is blank (in other words, you want not exists join instead of outer join), the form will show NO records returned.

This occurs because the form adds a queryFilter to the outer joined datasource. My solution was to catch this scenario and change the queryFilter to a queryRange.

Example:

On the executeQuery() method on the main datasource, I added this code.

This code will find the existing queryFilter (this one looks specifically for a blank batch number) and then add the same queryFilter to a range.

Tuesday, July 21, 2020

D365 Visual Studio Application Explorer filters

When I learned that the Application Explorer search window uses regular expressions, it made finding objects so much easier.
Everyone should know that you can use: type:"form" to only find form type elements but if you add regular expressions to it, you can greatly limit the results returned.
For example, you can add: List$ to find only forms that end in the word List or you can add: ^Cust to only find forms that start with Cust.
My favorite is to find objects that start with one word (often the prefix we are using for customizations) and ends with another word. For example, to find all objects that start with MCA and end with the word TEST you can do: ^MCA.*Test$
I use this every single day!
Happy coding!

Wednesday, July 8, 2015

Filter controls on a form with user initiated filters

I added filters to the standard work order form in Dynamics AX. They are checkboxes for what status the user would like to filter. However, if a user filtered their own fields using the filtering grid (CTRL-G) and then they checked or unchecked a status box, their user filters would disappear and the form would reset using the checkbox filters.
Here is how I fixed this:

Set the initial ProdStatus filter using the usage data of the user for the original checkboxes that will be used. Do this in the init() method of the form after getting the usage data and setting the controls.

    xSysLastValue::getLast(this);
    StatusCreated.value(bStatusCreated);
    StatusCostEstimated.value(bStatusCostEstimated);
    StatusScheduled.value(bStatusScheduled);
    StatusReleased.value(bStatusReleased);
    StatusStartedUp.value(bStatusStartedUp);
    StatusReportedFinished.value(bStatusReportedFinished);
    StatusCompleted.value(bStatusCompleted);

For each status, add it to the status string field you are building:

    if (bStatusCreated)
    {
        if (!strLen(statusFilter))
        {
            statusFilter += int2str(enum2int(ProdStatus::Created));
        }
        else
        {
            statusFilter += ',' + int2str(enum2int(ProdStatus::Created));
        }
    } . . .

Then set your query range:
    qbrStatus            = this.query().dataSourceTable(tablenum(ProdTable)).addRange(fieldnum  
                                                                                                              (ProdTable, ProdStatus));
    if (strLen(statusFilter))
    {
        qbrStatus.status(RangeStatus::Hidden);
        qbrStatus.value(statusFilter);
    }
    else
    {
        qbrStatus.value(SysQuery::valueUnlimited());
    }

In the modified method of the checkbox status controls, retrieve the prodTable_ds.queryRun().query() and then modify it with your current status checkbox. Then call ProdTable_DS.research(). Calling executeQuery() again was what was resetting the form to the original query. Using research() will keep all the user filters the same.

    str                     statusFilter;
    Query                   queryProdTable;
    QueryBuildDatasource    qbdsProdTable;
    ;
    if (!ProdTable_DS.queryRun()) // just in case
    {
        return;
    }
    queryProdTable       = ProdTable_DS.queryRun().query();
    qbdsProdTable        = queryProdTable.dataSourceTable(tableNum(ProdTable));

Just like in the init() method, for each status, add it to the statusFilter string and then set your status. This new qbrStatus is using the the query from queryRun() though so it will maintain user filters. Calling research() on the datasource will run this filtered query on your form.

    qbrStatus = SysQuery::findOrCreateRange(qbdsProdTable, fieldNum(ProdTable, ProdStatus));
    qbrStatus.status(RangeStatus::Hidden);
    qbrStatus.value(statusFilter);

    ProdTable_DS.research();