Tuesday, July 21, 2020

How to Create Multi Select Lookup in Microsoft Dynamics AX using X++

Hi guys, Today we see how to create multi select lookup on form level in Dynamics Ax using X++.

It will work for both 2012 and D365FO.

Unbound field :
Here we are adding Dynamic query to lookup.
Set Auto Declaration property "Yes" to control field(VendAccountNum).


Write below code on Class declaration on form level:

public class FormRun extends ObjectRun
{
    SysLookupMultiSelectCtrl msCtrl;

Way 1  : adding fields from one table only :

Write Below code in Form init method:

public void init()
{
    Query query = new Query();
    QueryBuildDataSource qbds;

    super();

    qbds= query.adddatasource(tablenum(VendTable));

    qbds.fields().addField(fieldNum(VendTable,AccountNum));
    qbds.fields().addField(fieldNum(VendTable,VendGroup));

    msCtrl = SysLookupMultiSelectCtrl::constructWithQuery(element, VendAccountNum , query);//dynamic Query

   msCtrl = SysLookupMultiSelectCtrl::construct(element, VendAccountNum , querystr(query));//static query
}

You can check your from it show the multiple lookup.
Output:


For testing purpose try below code

void clicked()
{
    container         con;
    int               i;

    con=msCtrl.getSelectedFieldValues();
    //con=msCtrl.get();//RecId values we get

    for (i=1; i<=conLen(con); i++)
    {
            Info(strfmt('%1',conPeek(con,i));
    }
}

Way 2 : adding fields from multiple tables :

    SysLookupMultiSelectCtrl     itemNumberControl;// Declare in global

    // This method we are calling from in it method.
    private void initItemMultiSelectCtrl()
    {
        QueryBuildDataSource    ecoResProductQbds, productTranslationQbds;
        Query                   ecoResProductQuery = new Query();

        ecoResProductqbds = ecoResProductQuery.addDataSource(tableNum(EcoResProduct));
        ecoResProductqbds.fields().addField(fieldNum(EcoResProduct, DisplayProductNumber));

        productTranslationQbds  = ecoResProductqbds.addDataSource(tableNum(EcoResProductTranslation));
        productTranslationQbds.relations(true);
        productTranslationQbds.fields().addField(fieldNum(EcoResProductTranslation, Name));

        itemNumberControl   = SysLookupMultiSelectCtrl::constructWithQuery(element, ItemNumber , ecoResProductQuery);

//ItemNumber  : control name. (need to put auto declaration is yes);
    }

// To get the data from that control : 
 any2str(conPeek(itemNumberControl.getSelectedFieldValues(), 1)) == ""  // check user is selected any value or not.

itemNumberControl.getSelectedFieldValues() // get the user selected values. It will return container.



For Bound field:

Some times wee need to store the user select value in table.

  • For bound field also we need to create unbound field and add the above code.
  • Need to add few more methods.
Data source active method.

 public int active()
   {
        int ret;
        
        ret = super();
ItemNumber.text(Table.field);

return ret;
   }

Unbound Field modified method.

        public boolean modified()
        {
            boolean ret;
    
            ret = super();
    // use any one line
            table.field = ItemNumber.valueStr();
            table.field = this.valueStr();
        table.field = Con2str(itemNumberControl.getSelectedFieldValues());

            return ret;
        }

case 1 : If your showcasing field in details form need to add unbound control.
case 2 : If your showcasing field in grid drag that field from table.


Keep Daxing !!


No comments:

Post a Comment