Dynamics AX 2009 – Override lookup field in dialog.

Since I’ve been working with AX for over 4 years now, I think it’s time I start writing about some of the useful tricks I’ve learnt.

I’ll start wit a useful thing I learnt today. I’s fairly straight forward to override the lookup method for the field a form. But it gets more difficult when we are dealing with a dialog box, since the dialog fields are created at run time.

Step 1, create a new class, extending from RunBase

class MyDialog extends RunBase
{   
    Dialog dlg;
    DialogField dlgEmpl, dlgLocation, dlgPrintScreen;
}

Override the dialogPostRun and dialog methods. (You will also need to implement the pack and unpack methods).

protected Object dialog
     (DialogRunbase dialog, boolean forceOnClient)
{
    ;
    dlg = super(dialog, forceOnClient);
    dlg.caption("Dialog");

    dlgEmpl 
        = dlg.addField(typeid(EmplId), "Person");
    dlgWMSLocation 
        = dlg.addField(typeid(WMSLocationId), "Location");
    dlgPrintScreen 
        = dlg.addField(typeId(NoYes), "Print to screen");    
    return dlg;
}

public void dialogPostRun(DialogRunbase dialog)
{
    ;
    super(dialog);
    dialog.dialogForm().formRun()
         .controlMethodOverload(true);
    dialog.dialogForm().formRun()
         .controlMethodOverloadObject(this);
}
public container pack()
{
   return conNull();
}
public boolean unpack(container packedClass)
{
    return true;
}

The important bit here are the controlMethodOverload methods. It allows this class to be used instead to control the lookups.

Now when dialog fields get created at runtime, they get a system named. This can be view in AX by right clicking on the field and selecting setup (in AX2009).

THe names will be along the liens of ‘fld_1_1’ , ‘fld_2_1’ etc

To create the lookup method override (or any other method override), create a new method in your class with the field system name, and the method name, like this;

 

void Fld2_1_lookup()
{
    WMSOrderTrans wmsOrderTrans;
    FormStringControl control = dlg.formRun().controlCallingMethod();
   SysTableLookup sysTableLookup =  SysTableLookup::newParameters(tablenum(WMSLocation),control);
   Query                   query = new Query();
   QueryBuildDataSource    queryBuildDataSource;
   QueryBuildRange         queryBuildRange;
   ;
   queryBuildDataSource = query.addDataSource(TableNum(WMSLocation));
   queryBuildRange = queryBuildDataSource.addRange(FieldNum(WMSLocation, inventLocationId));
  select firstonly wmsOrderTrans where wmsOrderTrans.routeId == wmsPickingRoute.pickingRouteID;
   queryBuildRange.value( wmsOrderTrans.inventDim().InventLocationId);
   sysTableLookup.addLookupfield(fieldnum(WMSLocation, wMSLocationId));
   sysTableLookup.addLookupfield(fieldnum(WMSLocation, inventLocationId));
   sysTableLookup.parmQuery(query);
   sysTableLookup.performFormLookup();
}

 

 

This entry was posted in Dynamics AX and tagged , . Bookmark the permalink.