Skip to main content

Update Query Range at Runtime in Dynamics AX

In Dynamics AX we can update the Query Range at run-time in code, This is similar to "Filter by Grid" feature except that we are handling this in code. For example my user asked me to provide a Button on Sales LineItems which would toggle between displaying "Current SalesLineItems" VS "All SalesLineItems". By Current SalesLineItems i mean displaying LineItems with more than zero quantity, other addendum to original requirement is to preserve the existing user filters on the form.

In this image you can see a SalesOrder with 3 LineItems (notice that one LineItem has zero quantity), blue arrow pointing to new button("Show Only Current LineItems"):



If the User clicks this new button then :
a) LineItems with zero quantity will be filtered out, in the image below you can see only 2 LineItems
b) the label of the button will be updated to "Show All LineItems" , see the following image:



Let's start DAXing now - All i need to update for this requirement is SalesTable form:
1. Add a new boolean variable to ClassDeclaration method
boolean showOnlyCurrentLineItems;

2. If the user wants to display only current LineItems by default when the form loads, then initialize our boolean variable to true in the Form->init()
showOnlyCurrentLineItems = true;

3. Now, let's add our query range just before the query is exceuted by overriding the SalesLine DataSource's executeQuery() method (SalesTable -> DataSources->SalesLine->Methods->executeQuery() ) .
Before the super() method is called we need to add our range to the SalesLine query, AX form maintains two instances of the query objects at run-time
a) formDataSource.query() - original datasource query added at the design time to the form's datasource node
b) formDataSource.queryRun().query() - the query originated from a) + filters applied by the User
So it is better to update both the queries to cover all the scenarios. Here is the code to write within the executeQuery():


public void executeQuery()
{
// instances for query type b)
Query queryForSalesLine;
QueryBuildDataSource qbdsForSalesLine;
QueryBuildRange qbrForSalesLine;

// instances for Query type a)
Query origQueryForsalesLine;
QueryBuildDataSource origQbdsForSalesLine;
QueryBuildRange origQbrForSalesLine;

boolean updateOrigQuery;

// check if query type b) exists, if so use it otherwise use Query type a)
if(salesLine_DS !=null && salesLine_DS.queryRun() != null && salesLine_DS.queryRun().query() != null)
{
queryForSalesLine = salesLine_DS.queryRun().query();
updateOrigQuery = true;
}
else
queryForSalesLine = salesLine_DS.query();

qbdsForSalesLine = queryForSalesLine.dataSourceName("SalesLine");
qbdsForSalesLine.clearRange(fieldnum(SalesLine, SalesQty));
qbrForSalesLine = qbdsForSalesLine.addRange(fieldnum(SalesLine,SalesQty));

// update both the queries in both the cases
if(showOnlyCurrentLineItems) // this is initialized to true and we will update this variable variable thru a button
{
qbrForSalesLine.value(">0");
if(updateOrigQuery)
{
origQbdsForSalesLine = salesLine_ds.query().dataSourceName("SalesLine");
origQbdsForSalesLine.clearRange(fieldnum(SalesLine, salesQty));
origQbrForSalesLine = origQbdsForSalesLine.addRange(fieldnum(SalesLine,SalesQty));
origQbrForSalesLine.value(">0");
}
}
else
{
qbrForSalesLine.value("*");
if(updateOrigQuery)
{
origQbdsForSalesLine = salesLine_ds.query().dataSourceName("SalesLine");
origQbdsForSalesLine.clearRange(fieldnum(SalesLine, salesQty));
origQbrForSalesLine = origQbdsForSalesLine.addRange(fieldnum(SalesLine,SalesQty));
origQbrForSalesLine.value("*");
}
}

super();
}

4. Add a new Button to the SalesLine section which will allow the User to toggle the Query's range
a) Add a new Button named "UpdateQueryButton" at SalesTable Form->Designs->[Group:Line] -> [ButtonGroup: ButtonLine]
b) Set its Text property to "Show All Line Items"
c) Override the button's clicked event method with this code:


void clicked()
{
super();
if(showOnlyCurrentLineItems)
{
showOnlyCurrentLineItems = false;
this.text("Show Only Current LineItems"); // Update button's text
}
else
{
showOnlyCurrentLineItems = true;
this.text("Show All LineItems"); // Update button's text
}

salesLine_DS.research(true); // this will call executeQuery method that we overrode earlier
}


That is it, you can verify that it preserves the User added Filters/Ranges on the form.

Popular posts from this blog

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...