Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 17314

How to: Pass multiple data source records to Class with MultiSelectionHelper

$
0
0

Good day everyone,

Today I will continue talking about how to pass multiple records to class, this time I will do it using the helper class MultiSelectionHelper. The MultiSelectionHelper class provides an interface to work with multiple selected records on a form data source and which can be cached, if called on server it will be cached by default.

For the sake of example purpose, I have modified a project which I had to reset all the remainder quantity of selected rows. I will change it a bit to show instead the selected rows RecId.

1. Create the class which will receive Args from our MenuItemButton.

ScreenClip

2. On classDeclaration, declare the Table Buffer:

class PurchLineBackOrderDialog
{
    PurchLineBackOrder  purchLineView;
}

3. Create the method main and use the following code:

static void main(Args _args)
{
   PurchLineBackOrderDialog    myClass;
   PurchLineBackOrder          purchLineBackOrder;
   FormDataSource              purchLine_ds;
   ;

   myClass = new PurchLineBackOrderDialog();

   //Check if the Args.Record() has the same TableID as the Table Buffer(PurchLineBackOrder).
   if (_args.record().TableId == tableNum(purchLineBackOrder))
   {
        //Assign the args records to table buffer and then pass it to purchLine_ds(FormDataSource)
        purchLineBackOrder  = _args.record();
        purchLine_ds        = purchLineBackOrder.dataSource();

        //Call Method which will be used to create the Dialog
        myClass.updatepurchLineTableBackOrder(purchLine_ds, _args);
   }
}

Not all times the Grid DataSource will be the same as Args.Record(), in this case you can’t use MultiSelectionHelper and will have to do it “Manually” you can do it following my other post. If you don’t know which Table is it by the TableID, you can use a SQL query to discover it.

4. This is the method which will be used to create the Dialog and it’s here that we are going to use MultiSelectionHelper.

private void updatepurchLineTableBackOrder(FormDataSource purchLine_ds, Args _args)
{
    Dialog                  dialog;
    DialogText              dialogTxt;
    FormRun                 caller;
    MultiSelectionHelper    helper;
    PurchLine               purchLineTable;
    ;

    //The method createFromCaller from MultiSelectionHelper has FormRun as parameter so we need to assign Args.Caller() to FormRun
    caller  = _args.caller();

    //This method will help to get only the marked records from the Grid
    helper  = MultiSelectionHelper::createFromCaller(caller);

    //Create a Dialog with title
    dialog  = new Dialog('Cancel Backorder' );
    //Create a "Label" do Dialog
    dialogTxt = dialog.addText( 'Do you wish to cancel the Backorder of selected rows?');

    //The code will only be executed if the user click on Yes
    if (dialog.run())
    {
        //Create the Query to filter using RecId
        helper.createQueryRanges(purchLine_ds.queryBuildDataSource(), fieldStr(purchLine, RecId));
        //Obtains the first query result and then assign it to table buffer
        purchLineView = helper.getFirst();

        //True if purchLineView contains RecId
        while (purchLineView)
        {
           //Simple example to show all RecIds from selected grid rows
           info(StrFmt('%1', purchLineView.RecId));

           //Gets next record
           purchLineView = helper.getNext();
        }
    }
}

In the example above we received the caller form with the _args object. Then, we have to find the correct FormDataSource object. This should be the table for which we want to get the list of records. After getting the correct data source, we can then tell the selection helper object to create ranges for the selected records on the specified QueryBuildDataSource, which we can now get directly from the FormDataSource in Dynamics AX 2012, with the queryBuildDataSource method. Then I created a Dialog asking if the user wishes to continue operation, if he confirms it will show RecId on the Infolog.

5. Create the Menu Item Display. For example, I have set the following properties.

ScreenClip

LabelUpdate Backorder
ObjectTypeClass
ObjectPurchLineBackOrderDialog
MultiSelectYes

6. Now, on the PurchLineBackOrderListPage form Design locate the ActionPaneTab, create a Button Group and then drag and drop the MenuItem to Button Group. It should look like this:

ScreenClip

7. For your information, I have set the following properties:

ButtonDisplayText & Image right
NormalImage10945
ImageLocationEmbeddedResource
MultiSelectYes
BigYes
MenuItemNamePurchLineBackOrderDialog

8. Now we are done! I hope my posted has saved you some time.

ScreenClip

ScreenClip



Viewing all articles
Browse latest Browse all 17314

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>