Thursday 9 August 2018

Override form control Lookup using extensions in D365FO / AX 7.0

In D365FO we have a bunch of events to subscribe on a form control level:





right click on lookup event of control to subscribe the event and paste in class.



[FormControlEventHandler(formControlStr(PayrollEmployerTaxRegion, Overview_StateId), FormControlEventType::Lookup)]
    public static void Overview_StateId_OnLookup(FormControl sender,FormControlEventArgs e)
    {
                  // write your logic here
    }

Now to cancel parent event, D365FO provide a class FormControlCancelableSuperEventArgs

You can cancel the parent(Original event with below code)

        FormControlCancelableSuperEventArgs ce = e asFormControlCancelableSuperEventArgs;


        //cancel super() to prevent error.
        ce.CancelSuperCall();

// Here is the complete code sample to override form lookup and cancel the original code.


[FormControlEventHandler(formControlStr(PayrollEmployerTaxRegion, Overview_StateId), FormControlEventType::Lookup)]
    public static void Overview_StateId_OnLookup(FormControl sender,FormControlEventArgs e)
    {
        SysTableLookup      sysTableLookup  =SysTableLookup::newParameters(tableNum(LogisticsAddressState), sender);
        Query               query           = new Query();

        // Filter lookup to only show US states
        query.addDataSource(tableNum(LogisticsAddressState)).addRange(fieldNum(LogisticsAddressState, CountryRegionId)).value(LogisticsAddressCountryRegion::findByISOCode(SysCountryRegionCode::countryInfo(curext())).CountryRegionId);

        // Sort the lookup by state Id
        query.dataSourceTable(tableNum(LogisticsAddressState)).addOrderByField(fieldNum(LogisticsAddressState, StateId), SortOrder::Ascending);

        // Add fields
        sysTableLookup.addLookupfield(fieldNum(LogisticsAddressState, StateId));
        sysTableLookup.addLookupfield(fieldNum(LogisticsAddressState, Name));

        // Run lookup
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
        FormControlCancelableSuperEventArgs ce = e asFormControlCancelableSuperEventArgs;

        //cancel super() to prevent error.
        ce.CancelSuperCall();
    } 

3 comments:

  1. Hi, I think you missed setting the returnItem, mostlikely the for the field StateId

    ReplyDelete
  2. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! centralina aggiuntiva mini

    ReplyDelete
  3. Hi I have done the same code but any able to get my lookup but instead no lookup is showing on field

    ReplyDelete

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...