Sunday, November 6, 2022

D365 FO Adding custom lookup query code X++


D365 FO Adding custom lookup query code X++

Purpose:

The purpose of this document is to demonstrate how we can create a lookup in X++

To add a lookup form to a control. To create a custom lookup form, you override the lookup method of the control. You can then create a query and lookup form you can use to populate the control. However, the lookup that appears is associated only with the specified control and is not available to any other control or form.

  1. In the AOT, expand Forms, expand the form, expand Designs, right-click Design, click New Control, and then click StringEdit. A StringEdit control is added to the form. 

  2. Expand the control, right-click Methods, click Override method, and then click lookup. The lookup method opens in the code editor.




In the code editor, add Query, QueryBuildDataSource, and QueryBuildRange objects. The following code example adds the classes you use to construct the query for the lookup form

public void lookup()
    {
        Query query = new Query();
        QueryBuildDataSource queryBuildDataSource;
        QueryBuildRange queryBuildRange; 

Create an instance of SysTableLookup class. The following code example creates a lookup form for customers. Notice how this in the example represents the current form control.

SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(custTable), this);

Use the addLookupField method to specify the fields that appear in the lookup form. The following code example adds the AccountNum and CustGroup fields to the lookup form.

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

Use a query to retrieve data for the lookup form. The following code example uses a range limit so that the lookup form lists customers who have the customer group of "XYZ".
 queryBuildDataSource = query.addDataSource(tableNum(CustTable));

    queryBuildRange = queryBuildDataSource.addRange(fieldNum(CustTable, CustGroup));
    queryBuildRange.value('XYZ');

    sysTableLookup.parmQuery(query);


Use the performFormLookup method to open the lookup form.

sysTableLookup.performFormLookup();

When you override the lookup method, comment out the call to super. If you do not comment out the call to super, the standard lookup form might appear.

X++

Copy
    //super();
}


The following code example shows the complete lookup method for customers:

public void lookup() { Query query = new Query(); QueryBuildDataSource queryBuildDataSource; QueryBuildRange queryBuildRange; SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(custTable), this); sysTableLookup.addLookupField(fieldNum(CustTable, AccountNum)); sysTableLookup.addLookupField(fieldNum(CustTable, CustGroup)); queryBuildDataSource = query.addDataSource(tableNum(CustTable)); queryBuildRange = queryBuildDataSource.addRange(fieldNum(CustTable, CustGroup)); queryBuildRange.value('XYZ'); sysTableLookup.parmQuery(query); sysTableLookup.performFormLookup(); //super(); }
   


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();
}RefRef2
































No comments:

Post a Comment