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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to get selected recor from grid?

(0) ShareShare
ReportReport
Posted on by 932

Hi, 

I created new form that 

in the method init I add this code :

public void init()
{
    Query                  query;
    QueryBuildRange        queryBuildRange1;
    QueryBuildDataSource    queryBuildDataSource;
    SecurityTreeNode      securityTreeNodeMenuItem;
    

    ;
    super();
    if(!element.args().caller())
    {
        throw error ("can't run directly");
    }

        securityTreeNodeMenuItem = element.args().record();

        MatrixID.text(securityTreeNodeMenuItem.MatrixRole);
        parmid                  = element.args().parm();
        query                    = new Query();
       queryBuildDataSource = query.addDataSource(tablenum(SecurityTreeNode));
        queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurableName));
        queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurityLevel));
        queryBuildDataSource.orderMode(ordermode::GroupBy);


        queryBuildRange1         = query.addDataSource(tablenum(SecurityTreeNode)).addRange(fieldnum(SecurityTreeNode,MatrixRole));
        queryBuildRange1.value(queryValue(MatrixID.valueStr()));
        SecurityTreeNode_ds.query(query);
}












}

1641.form.png

I want to get all marked Record when I click on button validate

I add this code in the method clicked 

void clicked()
{
    int                     recordsCount;
    SecurityTreeNode    _securityTreeNode;
    str                    recordMarked;
  
    super();
     recordsCount     = SecurityTreeNode_ds.recordsMarked().lastIndex();  // Total number of marked records.
    if(recordsCount ==0)
    {
        throw error("Veuillez choisir un élément !");
    }
    else
    {

       
       _securityTreeNode = SecurityTreeNode_ds.getFirst(1);

        while (_securityTreeNode)
        {

             
            _securityTreeNode = SecurityTreeNode_ds.getNext();
        }
       
        element.task(2876);
    }

}

But I don't get any values .

I have the same question (0)
  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    What value do you expect? Your code doesn't seem to return anything.

    By the way, I suggest that you use the MultiSelectionHelper class.

  • BASMA Profile Picture
    932 on at

    Hi Martin ,

    I want to get the RecId from the table SecurityTreeNode , When I debug code the recId =0

    means That I don't get the selected record.

    Have you any example about MultiSelectionHelper

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Just search the web for Dynamics AX multiselectionhelper and you will get dozens of examples :)

  • BASMA Profile Picture
    932 on at

    But I want to understand Why my code doesn't work.

    in the init method : when I delete these lines I get the recId from table

    queryBuildDataSource = query.addDataSource(tablenum(SecurityTreeNode));

           queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurableName));

           queryBuildDataSource.addSortField(fieldnum(SecurityTreeNode, SecurityLevel));

           queryBuildDataSource.orderMode(ordermode::GroupBy);

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    I don't know about your code, but could you try to use MultiSelectionHelper together with your form data source and see if you get the selected records?

    If not, please share your MultiSelectionHelper code.

    I think this approach is better than having us try to figure out your current code, which is not using MultiSelectionHelper.

  • Martin Dráb Profile Picture
    237,807 Most Valuable Professional on at

    You won't get RecId (or any other field) that is neither grouped by nor aggregated.

    Let's say you have five records (each with its own distinct RecId, of course) for a certain combination of SecurableName and SecurityLevel. You clearly can't get all five RecIds in the single RecId field. You could use an aggregation (e.g. to get the highest RecId), but I'm not sure it makes sense in this case.

  • BASMA Profile Picture
    932 on at

    Hi Martin,

    you means that  : with the approch MultiSelectionHelper I can't get the selected records?

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    You can get selected records of any grid with MultiSelectionHelper.

    I'm suggesting now for the second time: please try it! And if you have problems, please describe them and share your code with us. This is the best way how you can get closer to solving your issue.

  • BASMA Profile Picture
    932 on at

    I use the MultiSelectionHelper and I get the same issue :

    MultiSelectionHelper helper = MultiSelectionHelper::construct();

        helper.parmDatasource(SecurityTreeNode_ds);

       _securityTreeNode = helper.getFirst();

       while (_securityTreeNode.RecId != 0)

       {

           info(_securityTreeNode.SecurableName);

           _securityTreeNode = helper.getNext();

       }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Are you sure that your _securityTreeNode has RecId? If not, your while condition will never be met.

    Do you understand why it is like this? Martin explained it above.

    Anyway, if you change it to "while (_securityTreeNode.SecurableName)" do you get some results?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 660 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 512 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 291 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans