Monday, May 12, 2025

How to Create Multiselection Lookup in D365FO

How to Create Multiselection Lookup in D365FO

How to Create Multiselection Lookup in D365FO

In this blog post, we will walk through the steps to create a multiselection lookup in Dynamics 365 for Finance and Operations (D365FO). We will use a sample code to demonstrate the process. Also, you can refer to the standard class 'ENGIEMultiSelectionLookupUIBuilder'.

Step 1: Create the Data Contract Class

First, we need to create a data contract class. This class will hold the list of project IDs that we want to select.


internal final class DemoMultiSelectionLookupContract implements SysOperationValidatable, SysOperationInitializable
{
    private List projIdList;

    [DataMemberAttribute("ProjectId"), 
    AifCollectionTypeAttribute("ProjectId", Types::String),
    SysOperationLabelAttribute(literalStr("@SYS103748")),
    SysOperationDisplayOrderAttribute('7')]
    public List parmProjIdList(List _listProjId = projIdList)
    {
        projIdList = _listProjId;
        return projIdList;
    }
    
    public void initialize()
    {

    }
}
    

Step 2: Create the UI Builder Class

Next, we create a UI builder class to add the dialog field for the project ID selection.


public final class DemoMultiSelectionLookupUIBuilder extends SysOperationAutomaticUIBuilder
{
    protected DialogField addDialogField(IdentifierName _methodName, Object _dataContract = this.dataContractObject())
    {
        DialogField dialogField;

        switch (_methodName)
        {
            case methodStr(DemoMultiSelectionLookupContract, parmProjIdList):
                projIdField = this.dialog().addField(extendedTypeStr(ProjId), "@SYS103748", "Select project id those invoice want to export");
                dialogField = projIdField;
                break;

            default:
                dialogField = super(_methodName, _dataContract);
                break;
        }

        return dialogField;
    }

    public void postRun()
    {
        DemoMultiSelectionLookupContract dataContract = this.dataContractObject() as DemoMultiSelectionLookupContract;
        container selectFieldSources = this.selectField();
        super();

        ctrlProjId = SysLookupMultiSelectCtrl::constructWithQueryRun(
            this.dialog().formRun(),
            projIdField.control(),
            new QueryRun(queryStr(ProjTable)),
            true,
            selectFieldSources);
    }

    public container selectField()
    {
        return [tableNum(ProjTable), fieldNum(ProjTable, ProjId)];
    }

   
}
    

Step 3: Create the Service Class

Finally, we create a service class to handle the business logic for the multiselection lookup.


internal final class DemoMultiSelectionLookupService extends SysOperationServiceBase
{
    System.Exception ex;
    internal void runExport(DemoMultiSelectionLookupContract _exportAttachmentsContract)
    {
        try
        {
            List projectList = _exportAttachmentsContract.parmProjIdList();
        }
        catch (ex)
        {
            this.logException(ex);
        }
    }
}
    

Step 4: Create the Controller Class

Finally, we create a controller class to call the service class.


public class DemoMultiSelectionLookupController extends SysOperationServiceController implements BatchRetryable
{
    public static void main(Args _args)
    {
        DemoMultiSelectionLookupController controller = DemoMultiSelectionLookupController::construct();
        controller.parmDialogCaption("@ApplicationFoundation:ExportAttachments");
        
        controller.startOperation();
    }

    public static DemoMultiSelectionLookupController construct(SysOperationExecutionMode _executionMode = SysOperationExecutionMode::Synchronous)
    {
        return new DemoMultiSelectionLookupController(
            classStr(DemoMultiSelectionLookupService),
            methodStr(DemoMultiSelectionLookupService, runExport),
            _executionMode);
    }

    [Hookable(false)]
    public final boolean isRetryable()
    {
        return false;
    }
}
    

And that's it! You now have a multiselection lookup in D365FO. This example demonstrates how to create a data contract, UI builder, service class, and controller class to achieve this functionality.

No comments:

Post a Comment

How to Create Multiselection Lookup in D365FO

How to Create Multiselection Lookup in D365FO How to Create Multiselection Lookup in D365FO In this blog p...