Monday, April 3, 2017

Lookup method on form using eventing Dynamics AX 7

How to create a lookup method on a form without writing any code on form and using event.

AX 7 provides a very nice feature called Events. Using that concept lookup can be build on form level without touching any code on that form. See the procedure below.
I added a new string field on InventBatch form using extension. I have requirement to create lookup on this field. Lets start the steps of doing


  1. Go to that control on form,expand the events node and click on copy event handler method.











   
 2. Create a class InventBatchEventHandler and paste.

  1. class InventBatchEventHandler
  2. {
  3.     /// <summary>
  4.     ///
  5.     /// </summary>
  6.     /// <param name="sender"></param>
  7.     /// <param name="e"></param>
  8.     [FormControlEventHandler(formControlStr(InventBatch, VesselName), FormControlEventType::Lookup)]
  9.     public static void VesselName_OnLookup(FormControl sender, FormControlEventArgs e)
  10.     {
  11.         SysTableLookup sysTableLookup =              SysTableLookup::newParameters(tablenum(VesselTable), sender);
  12.  
  13.         Query query = new Query();
  14.         QueryBuildDataSource qbds;
  15.  
  16.         sysTableLookup.addLookupField(fieldnum(VesselTable, VesselId), true);
  17.         sysTableLookup.addLookupField(fieldnum(VesselTable, Name));
  18.  
  19.         qbds = query.addDataSource(tablenum(VesselTable));
  20.  
  21.         sysTableLookupCreate.parmQuery(query);
  22.  
  23.         sysTableLookup.performFormLookup();
  24.   FormControlCancelableSuperEventArgs ce = _e as FormControlCancelableSuperEventArgs;
  25.     //cancel super() to prevent error.
  26.     ce.CancelSuperCall();
  27.     }
  28. }
Build the project and you will have the new lookup on control.

Please enjoy learning and comment below if you have any query or suggestion.

No comments:

Post a Comment