Dynamics AX: Disable datasource from form in code while being able to use advanced filter/sort option

StackOverflow https://stackoverflow.com/questions/13951827

  •  10-12-2021
  •  | 
  •  

Question

I've modified the InventTable form so the users can filter the items depending on their "Stopped" status on the default order settings setup form. They get a list of the "buyable" items, "sellable" items, all items or blocked items for sale or purchase depending of the values of two checkboxes.

I've added the InventItemSalesSetup and InventItemPurchSetup datasources in the code and I enable or disable them when the user checks or unchecks a checkbox.

Everything works fine except when one of the datasources is disabled. Then the "Advanced filter/sort" option stops working. I get the error: "The data source is not enabled".

The error comes from the method "saveCueEnabled" of the SysQueryForm form. When it calls:

if (!CueRun::canSaveQueryAsCue(this.args().caller()))
    return false;

Which calls:

static boolean canSaveQueryAsCue(QueryRun qr)
{
    int numOfDataSources, i;
    QueryBuildDataSource ds;
    Query q;
    Common cursor;
    ;

    if (!qr)
    return false;

    q = qr.query();
    if (!q)
        return false;

    numOfDataSources = q.dataSourceCount();
    for(i = 1; i <= numOfDataSources; i++)
    {
        ds = q.dataSourceNo(i);
        if(ds.dynalinkCount() > 0)
            return false;

        // Check if it is temp
        cursor = qr.getNo(i);
        if (cursor.dataSource() && cursor.isTmp())
            return false;
    }

    return true;
}

When it gets the number of the datasources in the query, the "dataSourceCount" method also returns the count with the disabled data sources, and when it gets the QueryBuildDataSource of the disabled data sources in the loop you get an empty DS and it crashes when it checks if it's a temporary table.

I've solved the problem adding an extra if on the "saveCueEnabled" code but I wonder if there's a way to enable/disable the data sources without getting this error.

I hope I've explained myself well, thank you!

Was it helpful?

Solution

The only solution I can come up with requires you to modify canSaveQueryAsQue().

This update to the for loop is simple and should solve your issue.

for(i = 1; i <= numOfDataSources; i++)
{
    ds = q.dataSourceNo(i);
    if(ds && ds.dynalinkCount() > 0)
        return false;

    // Check if it is temp
    cursor = qr.getNo(i);
    if (cursor.dataSource() && cursor.isTmp())
        return false;
}

I have not tested this code, but I have used similar code in other situations. Hope that helps!

OTHER TIPS

The suggested edit doesn't actually work because the 'ds' variable is still 'truthy' when tested.

The following seems to work:

for(i = 1; i <= numOfDataSources; i++)
{
    ds = q.dataSourceNo(i);
    if(ds.enabled() && ds.dynalinkCount() > 0)
        return false;

    // Check if it is temp
    if(ds.enabled())
    {
        cursor = qr.getNo(i);
        if (cursor.dataSource() && cursor.isTmp())
            return false;
    }
}

Adding the '.enabled()' property to the test allows the code to skip over the disabled data sources as requested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top