How to do it...

To add a custom lookup to a control on a standard form, please follow these steps

  1. Create a new class that could be a generic sales order utility class, or ideally a form extension class (see previous recipe for this).
The naming is key so that we can easily find it. There is no obvious link that we have done this, so naming and documentation is critical.
  1. The first example is to make the sales order create dialog using a lookup form. As standard (in the November release), the lookup only contains the account number and account. This example binds the custom lookup to the customer account's lookup event, as follows:
/// <summary> 
/// Override the customer lookup on the sales order create
/// dialog in order to use a lookup with the address
/// </summary>
/// <param name="sender">calling control</param>
/// <param name="e">form events</param>
[FormControlEventHandler(formControlStr(SalesCreateOrder,
SalesTable_CustAccount),
FormControlEventType::Lookup)]
public static void SalesTable_CustAccount_OnLookup(
FormControl sender,
FormControlEventArgs e)
{
FormStringControl custAccountCtrl = sender;
CustTable selectedCustomer;
FormRun formRun;
Form custTableLookupForm;

custTableLookupForm = new
Form(formStr(CustTableLookup));
FormControlCancelableSuperEventArgs eventArgs = e;
Args formArgs = new Args();
formArgs = new Args();
formArgs.name(formStr(CustTableLookup));
formArgs.caller(sender.formRun());

selectedCustomer =
CustTable::find(custAccountCtrl.text());
if (selectedCustomer.RecId != 0)
{
formArgs.lookupRecord(selectedCustomer);
}
formRun = FormAutoLookupFactory::buildLookupFromCustomForm(
custAccountCtrl,
custTableLookupForm,
AbsoluteFieldBinding::construct(
fieldStr(CustTable, AccountNum),
tableStr(CustTable)),
formArgs);
custAccountCtrl.performFormLookup(formRun);
eventArgs.CancelSuperCall();
}

If you wish to create your own lookup, it is also fine. Just create a new form using Lookup - basic pattern. Let's use an example where we are writing a custom product lookup:

  1. We need to bind the select control (the ID column, such as the Item Id field in our example).
  2. Add the data source as usual (or write a view and use that) and complete the design pattern. When adding fields to the grid, change the Auto Declaration property of the ID column to Yes. The control will probably be called Grid_ItemId by default, which is fine.
  3. Override the init method so we can tell the form that the Grid_ItemId control will contain the data that we want to return. This is done with the following code:
void init() 
{
DictField dictField;
FormStringControl callerControl;

if (!element.args())
{
throw error(strfmt("@SYS22862", element.name()));
}

super();

element.selectMode(Grid_ItemId);
}
  1. Next, we should handle the filtering of the control based on the calling control, which is done with the following code:
public void run() 
{
FormStringControl callerControl;
boolean filterLookup = false;

callerControl = SysTableLookup::getCallerStringControl(
element.args());

filterLookup = SysTableLookup::filterLookupPreRun(
callerControl,
Grid_ItemId,
InventTable_DS);

super();

SysTableLookup::filterLookupPostRun(filterLookup,
callerControl.text(),
Grid_ItemId,
InventTable_DS);
}
InventTable_DS is the default data source name if we added the InventTable table. Replace this as required with the data source name of the actual table used.
  1. We should now create a menu item, and add it to an appropriate security privilege.

Lookups in Operations don't always use a form and, in fact, they are usually automatic based on the autoLookup field group. We can write such a lookup programmatically:

  1. Create a class, ConGeneralHandlers, for example, and add the following method:
/// <summary> 
/// Performs a lookup on the <c>InventTable</c> table.
/// </summary>
/// <param name="_lookupCtrl">
/// The <c>FormStringControl</c> control that the
/// lookup will be attached to.
/// </param>
/// <param name="_itemId">
/// The item that is used to filter the lookup.
/// </param>
public static void lookupItemId(
FormStringControl _lookupCtrl,
ItemId _itemId)
{
SysTableLookup sysTableLookup
Query query = new Query();
QueryBuildRange queryBuildRange;
QueryBuildDataSource queryBuildDataSource;
sysTableLookup = SysTableLookup::newParameters(
tableNum(InventTable),
_lookupCtrl)
queryBuildDataSource = query.addDataSource(
tableNum(InventTable));

if (_itemId)
{
queryBuildRange = queryBuildDataSource.addRange(
fieldNum(InventTable, ItemId));
queryBuildRange.value(queryValue(_itemId));
}

sysTableLookup.addSelectionField(
fieldNum(InventTable, ItemId));
sysTableLookup.addLookupMethod(
tableMethodStr(InventTable,
itemName));
sysTableLookup.addLookupMethod(
tableMethodStr(InventTable,
modelGroupId));
sysTableLookup.addLookupMethod(
tableMethodStr(InventTable,
itemGroupId));
sysTableLookup.addLookupField(
fieldNum(InventTable,
ItemType));

sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}
  1. We may need to replace the super call on the lookup method on the field within a data source; this is done by right-clicking on the Methods node for the data source field in question and choosing Override | Lookup. Remove the super() call so you don't get two lookups!
  2. To use it as before in an event handler is done as follows:
/// <summary> 
/// Override the item lookup on the sales order form
/// </summary>
/// <param name="sender">ItemId caller control</param>
/// <param name="e">args so we can cancel super</param>
[FormControlEventHandler(formControlStr(SalesTable,
SalesLine_ItemId),
FormControlEventType::Lookup)]
public static void SalesLine_ItemId_OnLookup(
FormControl sender,
FormControlEventArgs e)
{
FormStringControl itemIdCtrl;
FormControlCancelableSuperEventArgs eventArgs;

eventArgs = e;
itemIdCtrl = sender;

ConGeneralhandlers::lookupItemId(
itemIdCtrl,
itemIdCtrl.text());
eventArgs.CancelSuperCall();
}
  1. These techniques can be used as desired to adjust or replace the lookups in Dynamics 365 for Operations without any customization to the standard code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.255