Monday, November 19, 2018

X++ | d365FO - Create a lookup with a table fields list

This post shows the same scenario with this X++ | ax 2012 - Create a lookup with a table fields list but now in version d365FO.

First of all, I would give all references I did research for this purpose here. Many thanks and really appreciate for all these post!!
http://dev.goshoom.net/en/2016/02/utilelements-in-ax-7/
http://dev.goshoom.net/en/2016/11/new-metadata-api/
AX7 Get information about table extension using Microsoft.Dynamics.Ax.Xpp.MetadataSupport
http://theinfolog.com/creating-a-lookup-to-aot-objects/

Scenario
Again, we have a table, for example SalesTable. Then we would like to create a lookup with all fields from that table. Now, this is a way we can do in d365FO.

Solution

In this version, we cannot use Utilelements table anymore, so the steps will be like this.

1. Create a temp (InMemory) table, YourFieldNameTemp. In this case, there is only one field name "Name" in it.

2. Add a static method as follows. You will note that _tblName is the name of table we want its fields name.

 using Microsoft.Dynamics.AX.Metadata.MetaModel;  
 public class YourFieldNameTemp extends common  
 {  
   public static YourFieldNameTemp populate(TableName _tblName)  
   {  
     YourFieldNameTemp yourFieldNameTemp;  
     //AxTable table = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(tableStr(SalesTable));  
     AxTable table = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(_tblName);  
     var fields = table.Fields;  
     var fieldEnumerator = fields.GetEnumerator();  
     while (fieldEnumerator.MoveNext())  
     {  
       yourFieldNameTemp.initValue();  
       AxTableField field = fieldEnumerator.Current;  
       yourFieldNameTemp.Name = field.Name;  
       yourFieldNameTemp.insert();  
     }  
     return yourFieldNameTemp;  
   }  
 }  


3. The same as ax 2012, you have a field and already override "Lookup" method.






In the Lookup method, put this below code.

   [Control("String")]  
   class YourFormStringControl  
   {  
     public void lookup()  
     {  
       YourExampleTable.lookupTableField(this);  
     }  
   }  

4. At YourExampleTable table, insert this below method. You will note that the name of table we'd like to get all lists is from ..(this.refTableName).  You can change it to fit your need.

   public void lookupTableField(FormStringControl _control)  
   {  
     SysTableLookup           lookup;  
     QueryBuildDataSource qbds;  
     Query q = new Query();  
     qbds = q.addDataSource(tableNum(YourFieldNameTemp));  
     lookup = SysTableLookup::newParameters(tableNum(YourFieldNameTemp),  
                         _control,  
                         true);  
     lookup.addLookupField(fieldnum(YourFieldNameTemp, Name), true);  
     lookup.parmQuery(q);  
     lookup.parmTmpBuffer(YourFieldNameTemp::populate(this.refTableName));  
     lookup.performFormLookup();  
   }  


That's all.

Thanks for reading and until the next post!

3 comments: