How to: Create Form lookup / Lookup multiple tables
For the most complex scenarios, Dynamics AX offers the possibility to create and use a form as a lookup.
For example, I had to create a custom lookup with two tables, since the class SysTableLookup only supports one DataSource I had to find
alternative ways to do it, and after many tries I have found out that a Form Lookup using a View as DataSource was the simplest way.
On this example I will create a lookup using InventTable for ItemId and NameAlias and EcoResProductTranslation for Product Name.
1. In the AOT, create a query.
2. Drag and drop InventTable into Data Sources. Set the Fields properties Dynamic to No.
Now, drag and drop the fields ItemId and NameAlias to Fields.
3. Now, it’s time to create the relationship between InventTable and EcoResProductTranslation, after a bit of research I have found that InventTable.Product == EcoResProduct.RecId and EcoResProductTranslation.Product == EcoResProduct.RecId.
Which means that I will have to do an InnerJoin using the table EcoResProduct. Drag and drop EcoResProduct into InventTable Data Sources.
4. Set the EcoResProduct_1 properties folowing the image below.
5. Drag and drop EcoResProductTranslation into EcoResProduct Data Sources.
Set the properties as I did in the STEP 4 and then drag and drop the field Name to Fields. Set the Dynamic propertie to No.
6. Query done, now it’s time to create our View. In the AOT, create a new View.
Drag and drop our Query into Metadata.
7. Now, you will have to create the fields manually. On Fields, right-click it and then choose New > Field.
Set the following properties:
ItemId:
- Name: ItemId
- DataSource: InventTable_1
- DataField: ItemID
NameAlias:
- Name: NameAlias
- DataSource: InventTable_1
- DataField: NameAlias
Name:
- Name: Name
- DataSource: EcoResProductTranslation_1
- DataField: Name
8. At the end, your View should look like this:
9. In the AOT, create a Form.
10. Drag and drop the View you have just created into Data Sources and set with the following properties:
* AllowCheck: No
* OnlyFetchActive: Yes
11. Change the properties of the form’s design as follows:
* Frame: Border
* WindowType: Popup
12. Add a new Grid control to the form’s design, with the following properties:
* ShowRowLabels: No
* DataSource: <Your View>
13. Add three new StringEdit controls to the grid. Set your lookup primary key with the following properties:
* AutoDeclaration: Yes
14. Override the form’s init() method with the following code:
public void init() { super(); element.selectMode(ComercialItemsView_ItemId); // StringEdit Name }
15. Override the form’s run() method with the following code:
public void run() { FormRun callerForm; ; if (element.args() && element.args().caller()) { // Make sure this was via a form. if (SysDictClass::is(element.args().caller(), classnum(FormRun))) { // Cast the caller and make sure it is the right form. callerForm = element.args().caller(); } } super(); }
16. The form in the AOT should look similar to the following screenshot:
17. For test purpose, I have created a form which consists of InventTable as DataSource
and the field ItemId on the form’s Design.
18. Now, let’s override the StringEdit Lookup method with the following code:
public void lookup() { Args args = new Args(); FormRun itemLookUp; ; //Call the Form Lookup args.name( formstr (CommercialItems_Lookup)); args.parm(InventTable.ItemId); itemLookUp = new FormRun(args); itemLookUp.init(); this.performFormLookup(itemLookUp); }
19. In the AOT, your form should look like the image below:
20. To test the results, open the form and expand the StringEdit.
You will notice that the default lookup from InventTable has been replaced and is shown
only three columns from InventTable and EcoResProductTranslation.
This was originally posted here.
*This post is locked for comments