web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to filter a form's data source in form init based on a parameter passed to it

(0) ShareShare
ReportReport
Posted on by

I have a form with a grid control. The form's datasource is a table called 'ItemInventory'.  This table contains  item records for many different items.

A parameter is being passed to form init method of this form as in code below. How can I filter the data source to return only records with an item id equal to parameter passed into form init method? This would only show the filtered records in this form's grid control.

public void init()
{
    str  itemId;
    ;
    super();

    // Check for passed arguments
    if( element.args() )
    {
        // get the item id string parameter passed to this form
        itemId = element.args().parm();

//TO DO?? now filter the table ItemMaster on above itemId value } }

*This post is locked for comments

I have the same question (1)
  • Verified answer
    Community Member Profile Picture
    on at

    Override the executequery() method of the datasource table

    in executequery() before super()

    set the range of the query

    Please verify and update us if you need more technical details

  • Community Member Profile Picture
    on at

    Could you please provide some sample code as I am new to AX? The table name is ItemMaster which is the data source for form.

  • Verified answer
    Community Member Profile Picture
    on at

    From standard Dynamics AX we have lots of example

    please review following methods

    \Forms\CustInvoiceJournal\Data Sources\CustInvoiceJour\Methods\executeQuery

    \Forms\InventJournalTable\Data Sources\InventJournalTable\Methods\executeQuery

    Please verify and update us if this helps you.

  • Community Member Profile Picture
    on at

    Please read the post just after this.  It seems my post got duplicated and I couldn't delete it.


  • Community Member Profile Picture
    on at

    I used the code given below to filter the data source of form.  The part that filters the datasource must come after the super call in datasource init method.  So I had to override the datasource's init method. Make sure that the query related code is placed after super().

    public void init()
    {
        str itemId;
        itemId = element.args().parm();
        super();
        this.query().dataSourceTable(tableNum("ItemInventory")).addRange(fieldNum(ItemInventory,ItemId)).value(itemId);
    }

  • Verified answer
    Community Member Profile Picture
    on at

    This is also fine,

    but this filter will work only when you open the form

    let suppose after opening the form you refresh the field then this range will get removed

    so it is best practice is to write the filter criteria in ExecuteQuery method

    please verify and update us with your findings

  • Community Member Profile Picture
    on at

    Hi Nitesh, I clicked on File > Refresh after the new filtered form opened, but I didn't find the data source getting changed.  I am new to AX. I will check how I can refresh a field though I can only see File > Refresh.

  • Community Member Profile Picture
    on at

    Open form with your code

    Once open, on the form try sort cut key Ctrl+g

    then try to filter based on any field and validate

  • Community Member Profile Picture
    on at

    Hi Nitesh, Another solution was to put same code as in data source init method, except that I had to add it before the call to super. If I put this code in datasource init method then I had to place it after super(). The code that works equally well on overriding execute query is as below.

    public void executeQuery()
    {
    
        str itemId;
        itemId = element.args().parm();
        this.query().dataSourceTable(tableNum("ItemInventory")).addRange(fieldNum(ItemInventory,ItemId)).value(itemId);
        super();
       
    }


  • Community Member Profile Picture
    on at

    Hi Nitesh, You are right. But when I select 'clear filters' then it shows all records whether I override executequery or init method of datasource.  So then what is proper solution here?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
Henrik Nordlöf Profile Picture

Henrik Nordlöf 2 User Group Leader

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans