Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

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

Posted on by Microsoft Employee

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

  • Dave W Profile Picture
    Dave W 380 on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    Hi Nitesh,

    I have followed this through and implemented a working filter but this form is also opened via another menu item which does not require the filter applied so we want all records returned, is it possible to bypass the filter in this scenario?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    This is good that you got your solution.

    You are also good as you are able to manage to get your solution based on my pointers

    Please don't forget to mark the posts as verified answers, this will help others looking for similar solution

    Please help.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    Hi Nitish, You are too good. Thanks for helping me solve this problem.  Your suggestion worked. I had to first declare the querybuildrange object in class declaration of form being filtered.  Then I had to write the code in data source executequery method so the querybuildrange object is locked.

    public class FormRun extends ObjectRun
    {
        QueryBuildRange qr;//declare the querybuildrange object
    }
    
    public void executeQuery()
    {
    
        str itemId;
        itemId = element.args().parm();
       // this.query().dataSourceTable(tableNum("ItemInventory")).addRange(fieldNum(ItemInventory,ItemId)).value(itemId);
    
        qr = this.query().dataSourceTable(tableNum("ItemInventory")).addRange(fieldNum(ItemInventory,ItemId));
        qr.value(queryValue(itemId));
        qr.status(RangeStatus::Locked);//lock the querybuildrange object so user cannot filter the grid on other item id values
    
        super();
    
    }

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    For Clear filter not to erase the filter you need set the range as locked(true)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    But I did use the executequery method and still I am seeing the problem you mentioned.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    Yes, this is what I was saying...

    now it depends what is your requirement...

    but best practice is to have filter code written in executeQuery()

    writing code elsewhere leaves form with some what not a proper behaviour

    Please verify.

    Appreciated if my solution you feel is good for you please mark it as verified

    it will help others

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    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?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    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
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    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
    Community Member Microsoft Employee on at
    RE: How to filter a form's data source in form init based on a parameter passed to it

    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans