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

2

6

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!

Lunseth answered 19/12, 2012 at 11:50 Comment(0)
L
4

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!

Lachrymose answered 25/1, 2013 at 20:29 Comment(1)
I think that should work. Thanks, I'll try if I ever come with this problem again.Bluh
A
0

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.

Abscise answered 18/4, 2013 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.