Search This Blog

Friday, October 28, 2016

Use AX Form As lookup in Dynamics Ax 2012

AX-2012 - Use AX Form As lookup in Dynamics Ax

In this article we will cover and learn how to create a lookup form and link this lookup form with EDT Field by following the step-by-step walk-through.

Scenario:-
As part of this tutorial, Consider that we need to overwrite the system lookup and use AX form as a lookup for the StringEditField.





StringEditField Lookup

public void lookup()
{
    Query Query ;
    QueryBuildDataSource QBDS;
       
    SysTableLookup systbl=SysTableLookup::newParameters(tableNum(CustTable),this);
    systbl.addLookupfield(fieldNum(CustTable,AccountNum));
    systbl.addLookupfield(fieldNum(CustTable,CustGroup));
    Query=new Query();
    QBDS=Query.addDataSource(tableNum(CustTable));
    
    systbl.parmQuery(Query);
    systbl.performFormLookup();
}


Reference Group Lookup

public Common lookupReference()
{
    Query Query ;
    QueryBuildDataSource QBDS;
    SysReferenceTableLookup sysRefTablelookup;

    sysRefTablelookup =SysReferenceTableLookup::newParameters(tableNum(PayrollEarningCode),this);
    sysRefTablelookup.addLookupfield(fieldNum(PayrollEarningCode,EarningCode));
    sysRefTablelookup.addLookupfield(fieldNum(PayrollEarningCode,QuantityUnit));
    Query=new Query();
    QBDS=Query.addDataSource(tableNum(PayrollEarningCode));
    sysRefTablelookup.parmQuery(Query);
    return sysRefTablelookup.performFormLookup();
}


Lookup from Lookup From

public void lookup()
{
    Args args;
    FormRun custlookup;
           
    args=new Args();
    args.name(formStr(CustlookupForm));
    args.caller(this);
    custlookup=classFactory.formRunClass(args);
    custlookup.init();
    this.performFormLookup(custlookup);        
}


How to Create a Lookup Form

1-Create a new Form Named "CustLookupForm"

2-Add "custTable"  in the form data source and in the reference data source of "custTable"add "DirPartyTable".
3-Set the property of custTable as below:-

Name : CustTable               Table : CustTable
Index  : AccountIdx             AllowCheck : No
AllowEdit : No                     AllowCreate : No
AllowDelete : No                 OnlyFetchActive : Yes

4-Set the property of DirPartyTable as below:-
Name : DirPartyTable         Table : DirPartyTable
JoinSource : CustTable      JoinRelation : DirPartyTable_FK

5-Set propertys on Design node as below:-
Frame : Border                         WindowType : Popup
Style : Lookup

6-create a new Grid and set its property as below:-
ShowRowLabels : No           DataSource : CustTable

7-Add new stringEdit control on the grid and set its property as below :-
Name : CustTable_AccountNum          AutoDeclaration : Yes
DataSource : CustTable                         DataField : AccountNum

8-Add new stringEdit control on the grid and set its property as below :-
Name : DirPartyTable_Name         
DataSource : DirPartyTable                   DataField : Name

9-Add new stringEdit control on the grid and set its property as below :-
Name : CustTable_CustGroup        
DataSource : CustTable                         DataField : CustGroup


10-Overwrite form init method

public void init()
{
    super();
    element.selectMode(CustTable_AccountNum);
}

11-Overwrite form run method
public void run()
{
    FormStringControl CustAccount;
    boolean Filterlookup;
    ;

    CustAccount=SysTableLookup::getCallerStringControl(element.args());
    Filterlookup=SysTableLookup::filterLookupPreRun(CustAccount,CustTable_AccountNum,CustTable_DS);
    super();
    SysTableLookup::filterLookupPostRun(Filterlookup,CustAccount.text(),CustTable_AccountNum,CustTable_DS);

}