Wednesday, June 18, 2008

How to enable or disable joined dataSources

Here the problem: I need a method to enable or disable an exist join link between two datasources, so, I found two practical solutions:

1. Using only X++ code:

- The init method (in this sample, I'm using the vendTrans datasource):
void init()
{
    super();

    ...

    // Here starts my code
    myDS = this.query().dataSourceTable(tableNum(VendTrans)).addDataSource(tableNum(myTable));
    myDS.joinMode(JoinMode::ExistsJoin);
    myDS.addLink(fieldNum(VendTrans, DocumentNum), fieldNum(myTable, DocumentNum));
    myQueryRange = myDS.addRange(fieldnum(myTable, anyField));
    myDS.enabled(false);  // Disabled until we need it
    // End
}
- Then, in anywhere:
    if ((myFilterValue == "") || (myFilterValue == "*"))
    {
        myDS.enabled(false);  // Disable the DS, then none filter is applied
    }
    else
    {
        ...

        myQueryRange.value(myFilterValue);
        myDS.enabled(true);  // Enable DS, then any filter is applied with the existing join link
    }
    
    VendTrans_ds.executeQuery();


2. Using DataSources in design mode:

- If the DataSource was added in design mode, we need to change the previous code as this:
    if ((myFilterValue == "") || (myFilterValue == "*"))
    {
        myTable_ds.query().dataSourceTable(tablenum(myTable)).enabled(false);  // Disable the DS, then none filter is applied
    }
    else
    {
        ...

        myQueryRange.value(myFilterValue);
        myTable_ds.query().dataSourceTable(tablenum(myTable)).enabled(true);  // Enable DS, then any filter is applied with the existing join link
    }
    
    VendTrans_ds.executeQuery();


NOTE: myQuery value must be initialized in the init method from myTable datasource and the relation between tables must be specified (In this sample, myTable.myField = vendTrans.DocumentNum).

1 comments:

Liobrice said...

Thanks for this post. It was helpful for me


View My Stats