Monday, March 24, 2008

Create Custom Lookups Dynamics AX

Question:
How can we create customized lookups in Dynamics AX
Answer:
One way of creating a custom lookup is by using the power of SysQuery. Lets solve this using a scenario, we want to create a customized vendor lookup that will be used on the Item form. We will define a vendor type and an item type and the vendor that matches the item type on the purchase order form shall be displayed in the lookup. So in order to get this output we will create a method lookupVendor() in the Vendor table and we will call this method from the overridden lookup() method of the Vendor.VendorId field of Vendor datasource passing in the item type from the form.
New method created in the Vendor table
public client static void lookupVendor(FormStringControl _ctrl,
VehicleVendorType _vehicleVendorType)
{

SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(VehicleVendor), _ctrl);
Query query = new Query();
QueryBuildDataSource qbds;
QueryBuildRange vehicleVendorFuelFilter;
;
sysTableLookup.addLookupfield(fieldnum(VehicleVendor, VendorId), true);
sysTableLookup.addLookupfield(fieldnum(VehicleVendor, VendorName));
qbds = query.addDataSource(tablenum(VehicleVendor));
switch (_vehicleVendorType)
{
case VehicleVendorType::Fuel:
qbds.addRange(fieldnum(VehicleVendor, IsFuelVendor)).value(queryValue(NoYes::Yes));
break;
case VehicleVendorType::Mantienance:
qbds.addRange(fieldnum(VehicleVendor, IsMantenanceVendor)).value(queryValue(NoYes::Yes));
break;
case VehicleVendorType::Manufacturer:
qbds.addRange(fieldnum(VehicleVendor, IsFuelVendor)).value(queryValue(NoYes::Yes));
break;
case VehicleVendorType::Tyre:
qbds.addRange(fieldnum(VehicleVendor, IsManufacturer)).value(queryValue(NoYes::Yes));
break;
case VehicleVendorType::Vehicle:
qbds.addRange(fieldnum(VehicleVendor, IsVehicleVendor)).value(queryValue(NoYes::Yes));
break;
}
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}

Overridden method of Vendor.VendorId on Vendor Datasource

public void lookup(FormControl _formControl, str _filterStr)
{
;
VehicleVendor::lookupVendor(_formControl, VehicleVendorType::Fuel);
}