Saturday, November 9, 2019

D365/AX7 – Passing Multiple Form Datasource References to Class

Problem

We often use the form data sources to get the selected records on form to execute operations within a class. Usually we work with a single data source reference to get the selected records using formDataSource.cursor() or the MultiSelectionHelper class. However, we sometimes come across few scenarios where multiple form data sources have to be passed to the class to get the selected records.

Solution

In order to pass multiple form datasource references to the class, the required datasources must have common parent datasource on form.
Following steps must be followed.

Step#1
Make sure the required form data sources have same join datasource on form. E.g. we need to pass form datasource named MY_CriticalTaskTable and MY_SubTaskTable (as shown below) to the class to get their selected records. Both the datasources have same Join Source i.e. MY_TaskTable




Step#2 Create a new class and add main method with the code below to get the form data source references.

public static void main(Args _args)
    {
        MY_TaskCompleteAction action = new MY_TaskCompleteAction();
        FormRun callerForm = _args.caller();
        FormDataSource subTaskDs, criticalTaskDs;

        subTaskDs       = callerForm.dataHelper().FindDataSource(formDataSourceStr(MY_TasksListDetail, MY_SubTaskTable));
        criticalTaskDs  = callerForm.dataHelper().FindDataSource(formDataSourceStr(MY_TasksListDetail, MY_CriticalTaskTable));

        action.iterateSubTasks(subTaskDs);
        action.iterateSubTasks(criticalTaskDs);
    }

public void iterateSubTasks(FormDataSource _subTaskDs)
    {
        MY_SubTaskTable subTask;
        MultiSelectionHelper helper = MultiSelectionHelper::construct();
        helper.parmDatasource(_subTaskDs);

        subTask = helper.getFirst();

        while(subTask)
        {
            info(subTask.Name);
            subTask = helper.getNext();
        }

    }

Step#3 Create an action menu item for the class and add to the form. Set Datasource property of the menu item on form to the parent datasource of both the datasources i.e. MY_TaskTable in the current scenario.


Conclusion


If parent datasource reference is passed to the action menu item, then the child datasource references can be get within the operating class.

No comments:

Post a Comment