Skip to main content

Notifications

Get multiple selected rows using X++

Get multiple selected rows using X++ in Microsoft Dynamics 365 Finance & Operation and Microsoft Dynamics AX 2012.

In Microsoft Dynamics 365 for Finance and Operations and Microsoft Dynamics AX 2012, it is usually not a simple task for developers to get multiple selected rows using X++.

In this article, I am sharing a code snippet and properties to set which will help you to get multiple selected rows using X++.

Have a look!

How to get multiple selected rows using X++ in Microsoft Dynamics 365 for Finance and Operations and Microsoft Dynamics AX 2012?

Steps:

  • Create a table named MoeenTable
  • Create a form name MoeenForm
  • Create the data set in MoeenForm named as MoeenTable
  • Create a grid in MoeenForm named as MainGrid
  • Add fields from data set named MoeenTable into the MainGrid
  • Create a Action Menu Item named MoeenSelectMultipleRowsJob
  • Create a Runnable Class (Job) named MoeenSelectMultipleRowsJob
  • Add the Action Menu Item MoeenSelectMultipleRowsJob on the MoeenForm
  • Add the below code in to the class named MoeenSelectMultipleRowsJob:
class Moeen_SelectMultipleRowsJob
{    
    public static void main(Args _args)
    { 
        YourTableName  yourTableName; //Table name which 
        FormDataSource yourTableName_ds; //Data source attached with the specified form from where this job is running
        Object caller;        
        MultiSelectionHelper    helper;
        if(_args) //Checking that the args are coming from the form
        {
            if(_args.dataset() == tableNum(YourTableName)) //Checking that the arguments have the dataset equals to our specific table
            {              
                yourTableName_ds = _args.callerFormControl().formRun().dataSource(); //Getting the data source from argument and assigning it to the object created above
                caller  = _args.caller(); //Getting the caller and assigining it to caller object created above
                helper  = MultiSelectionHelper::createFromCaller(caller); //Creating the object of MultiSelectionHelper class using the caller
                helper.createQueryRanges(yourTableName_ds.queryBuildDataSource(), fieldStr(yourTableName, RecId)); //Creating query ranges in the helper object so that it only reads which the user selected from the form

                yourTableName = helper.getFirst();             //Getting the first row  
                while (yourTableName)
                {
                   info(strFmt("%1", yourTableName.FieldName)); //Printing just to check whether we get the data or not
                   //You can do your work here
                   yourTableName = helper.getNext(); //Reading the next row
                }
                //Clearing the ranges we have created above to come back to original state at form level, otherwise if you refresh the form it will show only one row.
                yourTableName_ds.query().dataSourceNo(1).clearRanges();
                //Executing the query at form level
                yourTableName_ds.executeQuery();
            }
        }           
    }
}
  • Set the properties of Action Menu Item named MoeenSelectMultipleRowsJob as follows:
    • Object Type: Class
    • Object: MoeenSelectMultipleRowsJob
    • Needs Record: Yes

In conclusion, in this way you can get multiple selected rows using X++ in D365FO and AX 2012.

Moreover, please Like, Comment and Share to help other people. If you found any ambiguity or a better solution, please feel free to ask.

See also:

Website: Click here

Blog: Click here

YouTube: Click here

GitHub: Click here

Facebook: Click here

The post Get multiple selected rows using X++ appeared first on NevoiTech Blog.

Comments

*This post is locked for comments