Monday, 17 July 2023

Multi-Select Lookup on a Form with X++ Code D365 FO

Multi-Select Lookup on a Form with X++ Code


Introduction:

In today's fast-paced digital world, efficient data entry is crucial for businesses to streamline their operations. One common requirement is the ability to select multiple values from a lookup on a form. In this blog post, we will explore how to implement a multi-select lookup functionality on a form using X++ code. By the end of this article, you'll have the knowledge to enhance your forms with this powerful feature, making data entry a breeze.


Step 1: Setting up the Form

    1.Let's add one dialogue form

 

Step 2: Adding the Multi-Select Lookup Control

    1. add a string control 

     2.  add ok and cancel buttons



Step 3: Populating the Lookup Values

        1. override the control Lookup method


  1. declare Global variable for mult-select 
  2. SysLookupMultiSelectCtrl mltSelectCtrl;

 public void lookup()

        {

           

            Query query = new Query();

            QueryBuildDataSource qbd;

            qbd = query.addDataSource(TableNum(yourTable));

            qbd.addSelectionField(FieldNum(yourTable,Field1));

            qbd.addSelectionField(FieldNum(yourTable,Field2));

           

            qbd.clearRanges();

           

            mltSelectCtrl = SysLookupMultiSelectCtrl::constructWithQuery(this.formRun(),this,query);

        }


Step 4: Selected values 

  1. override the OK Click method, in the below method we are just displaying the selected values.

[Control("CommandButton")]

    class OkButton

    {

        public void clicked()

        {

            container sRecid;

            int i;

            

super();


            sRecid = mltSelectCtrl.get();

           

           for(i = 1; i <= conLen(sRecid) ; i++)

            {

                info(strFmt("selected %1 ", conPeek(sRecid,i)));

            }  


    }

How to: Refresh the Calling Form after Dialog Form action.


It is a very common scenario, where we need to refresh the data of a calling from, when you open a dialog or drop dialog.

To achieve this, we need to Override the CloseOk method of the Dialog form. below is the code for the same.

public void closeOk()

    {

        FormRun formRun;

        List dsList;

        ListEnumerator dsListEnumerator;

        FormDataSource formDS;


        super();


        // Get an instance of the calling form.

        formRun = element.args().caller();


        // If the caller is a form, find and refresh the specified root data source.

        if(formRun)

        {

            dsList = formRun.rootFormDataSources();


            if(dsList && dsList.elements() > 0)

            {

                dsListEnumerator = dsList.getEnumerator();


                while(dsListEnumerator.moveNext())

                {

                    formDS = dsListEnumerator.current();

                    formDS.research(true);

                }

            }

        }

    } 

How to Disable “Advanced Filter or Sort” and Enforce Custom Filters on Any D365FO Form

 In Dynamics 365 Finance and Operations, users can apply filters through the “Advanced filter or sort” feature found under the Options tab...