/ Microsoft / How to create lookup in D365FO using X++

How to create lookup in D365FO using X++

Learn, share and spread knowledge

Lookup makes the data entry for the users easy and error-less. I have already shared the video in which I have described about how to create lookup in Microsoft Dynamics 365 for Finance and Operations automatically, but in today’s article I will discuss about how to create the lookup in D365FO using X++.

First of all, you should be clear that we show lookup on the form. They are basically form string control which becomes lookup if we write the the lookup method on it, but in the video in which we have only used the extended data type to create lookup automatically in Microsoft Dynamics 365 for Finance & Operations.

In this tutorial, we are going to make custom lookup using x++ for form in D365FO. Most people think that we have to put a lot of effort to make this happen, but, it is very easy. And some people got stuck on this because they don’t follow the steps. So, I need you to relax, make your focus correct and I need your concentration and obviously your appreciation too. Let’s start!

  • Create a form
  • Open the form
  • Go to the form string control on which you want to show lookup
  • Go to the events of the form string control on forms
  • Right click the OnLookup event and click Copy event handler method
  • Create a new class and paste the event handler method in the class you have copied in the previous step
  • It would be like this:
[FormControlEventHandler(formControlStr(EcoResProductDetailsExtended, MainAccount), FormControlEventType::Lookup)]
public static void MainAccount_OnLookup(FormControl sender, FormControlEventArgs e)
{
     //Add the lookup logic here.
}

Note:

  • Form name: EcoResProductDetailsExtended
  • Form string control name: MainAccount
  • Add the logic of the lookup in the event handler method like below:
[FormControlEventHandler(formControlStr(EcoResProductDetailsExtended, MainAccount), FormControlEventType::Lookup)]
public static void MainAccount_OnLookup(FormControl sender, FormControlEventArgs e)
{   
     Query query = new Query();
     QueryBuildDataSource queryBuildDataSource;
     QueryBuildRange queryBuildRange;
     SysTableLookup sysTableLookup = 
     SysTableLookup::newParameters(tableNum(MainAccount), sender);
     sysTableLookup.addLookupField(fieldNum(MainAccount, MainAccountId));
     sysTableLookup.addLookupField(fieldNum(MainAccount, Name));
     queryBuildDataSource = query.addDataSource(tableNum(MainAccount));
     sysTableLookup.parmQuery(query);
     sysTableLookup.performFormLookup();
}

Note:

Table:

  • MainAccount

Fields:

  • MainAccountId
  • Name

The two lines below add the two fields in the lookup and you will see Main Account Id and the Name of the account in the lookup. So, you can also add more lookup fields in the lookup as per your requirement.

sysTableLookup.addLookupField(fieldNum(MainAccount, MainAccountId));
sysTableLookup.addLookupField(fieldNum(MainAccount, Name));

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 lookup in D365FO using X++. If this helps you and you have learnt something, please share.

Don’t follow someone’s path, create you own.

4 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

4 Comments