Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

Our blog provides a curated collection of tips, tricks, and code snippets that streamline your workflow and enhance your proficiency.
Unleashing Dynamics 365 Excellence: Your Source for Pro Tips and Snippets at D365Snippets.com

How to create lookup in D365FO using X++

1

In the previous article, I already showed the very simplest type of lookup in D365 FO, which was EDT lookup, for making an EDT type lookup, a developer does not need to worry more about writing x++ coding. In this tutorial, we are going to make a custom lookup using x++ for form in D365FO.

What is a lookup in D365 FO? 

Lookups are the standard way to display a list of possible selection values to the user. For a user, while editing or creating database records, a lookup makes the data entry easy and errorless. In Microsoft dynamics 365 finance and operations, there are many ways of creating lookups. 

Normally, standard lookups are created automatically by the system in Dynamics 365 for Finance and Operations and are based on the extended data types and table setup. They are based on table relations and appear automatically. No additional modifications are required. 

How to create lookup in D365FO using X++

Why Use a Lookup Method? 

Normally a look-up is used to list the master data in the system like customer master, vendor master, item master, etc. what will happen if a user is typing these values instead of selecting the values from the lookups. 

Consider a supplier order form, there we can select the vendor account using a lookup. Instead of selecting a vendor account using lookup, if the user is trying to type the vendor account details there are many chances to get an error in the input because the user doesn’t know the vendor account which are existing in the database. Also, while selecting a vendor from the lookup, a user can confirm the vendor by checking the other lookup columns which display all the details about the vendor. So, about a user, while editing or creating database records, a lookup makes the data entry easy and errorless. 

Suppose we have a requirement to display AccountNum, CustGroup, BankAccount, and PaymMode of all customers under the selected company. 

For this, you must follow the below steps.

How to create a custom lookup using X++ in D365FO?

1. Create a form

So, the first step is we need to create a form, for this

Right Click on Project Name > Add > New Item > User Interface > Form

here I created a form with name FrmCustomLookUp as shown in the figure.

How to create lookup in D365FO using X++

2. Open the Form

Once you created a new form, it will automatically open the designer window. Otherwise, you double-click on the form, and it will open in the designer window as shown below,

How to create lookup in D365FO using X++

3. Create a form Control String

Here we need to create a form string control on which you want to show the lookup. So create control of type “String” (“Traditionally it is StringEdit Control”) under the form. 

For this,

Right-click on the Design Node > New > String This actually creates a drop-down text box on which you want to show the lookup.

How to create lookup in D365FO using X++

Now the design part is over, next we have to write the x++ logic to create a lookup and add this look-up to the text box we already created under the form.

4. Add event handler method

For this, Expand the Events node under the formStringControl > Select OnLookup> Select Copy event handler method as shown in the figure.

How to create lookup in D365FO using X++

Then create a new class, here I created a class with the name CustomLookUp and paste the event handler method in Between the opening-closing brackets of the class, and paste the event code, we copied the code in the previous step.

How to create lookup in D365FO using X++

How to create lookup in D365FO using X++

You can copy and paste the below code snippets directly to your class.


class CustomLookUp
{
   
   
       [FormControlEventHandler(formControlStr(LookUpFrm, FormStringControl1), FormControlEventType::Lookup)]
       public static void FormStringControl1_OnLookup(FormControl sender, FormControlEventArgs e)
       {
           Query query = new Query();
           QueryBuildDataSource queryBuildDataSource;
           QueryBuildRange queryBuildRange;
           SysTableLookup sysTableLookup;
           sysTableLookup = SysTableLookup::newParameters(tableNum(CustTable),sender,true);
           sysTableLookup.addLookupField(fieldNum(CustTable,AccountNum ));
           sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup));
           sysTableLookup.addLookupField(fieldNum(CustTable, BankAccount));
           sysTableLookup.addLookupField(fieldNum(CustTable, PaymMode));
           queryBuildDataSource = query.addDataSource(tableNum(CustTable));
           sysTableLookup.parmQuery(query);
           sysTableLookup.performFormLookup();          
     
       }
   
 
}

The four lines below add the four fields in the lookup and you will see AccountNum, CustGroup, BankAccount, and PaymMode in the lookup. So, you can also add more lookup fields in the lookup as per your requirement.

sysTableLookup.addLookupField(fieldNum(CustTable,AccountNum ));
sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup));
sysTableLookup.addLookupField(fieldNum(CustTable, BankAccount));
sysTableLookup.addLookupField(fieldNum(CustTable, PaymMode));

This is just an example shown to you. You can use the table and the fields as per your own requirements about How to create a lookup in D365FO using X++. If this helps you and you have learned something, please share.

Output

Right-click on the form FrmCustomLookUp and set this form as StartUpObject, then after the successful build of the project, run the project, you can see the below output.

How to create lookup in D365FO using X++

All the tutorials are for beginners, if you don't have a virtual machine for your learning purposes you can use Microsoft's free Virtual machine. if this tutorial is useful, please share it with others.

Happy coding with D365 Snippets

Related Tags

How to create a custom lookup using X++ in D365FO?
How many kinds of lookups can be made and how in D365? 
How do I create a lookup field in Dynamics 365 CRM?
How do I create a custom sequence number in D365FO? 
What is COC in D365FO?
systablelookup.add lookup method in d365 
multi select lookup in d365 finance and operations 
EDT lookup in d365fo 
override lookup method in d365 lookup 
the event handler in d365 more than one form was opened at once for the lookup control. lookup field in dynamics 365 x++ table field lookup
How many kinds of lookups can be made and how in d365?
EDT lookup in d365fo
lookup method in d365fo x++ 
systablelookup.add lookup method in d365
addlookupmethod x++
edt lookup in d365fo 
d365fo coc lookup
how to create a lookup field in dynamics 365

Post a Comment

1Comments
  1. The post is very useful and keep posting such content

    ReplyDelete
Post a Comment