Showing posts with label Event Handling. Show all posts
Showing posts with label Event Handling. Show all posts

How to override form control Lookup using extensions. D365FO

How to override form control Lookup using extensions. D365FO



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 as FormControlCancelableSuperEventArgs;


        //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 as FormControlCancelableSuperEventArgs;

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

Delegate Result with EventHandlerResult


Delegate Result with EventHandlerResult 


In this article, we will show to get Results from delegate subscribe events.


In normal routine mostly Ax Technical use subscribe event of delegates and perform the required task and get the required result. Following are the few steps through them you can achieve the same.

Let's begin.

Step-1 Create a class with the name of SLD_DemoSample
Step-2 Add two methods Main and run in this class like below screenshot

Reference screenshot

Step-3 Create a delegate in the class with two parameters like below

delegate void showMessage(Description _msg, EventHandlerResult result)  {    }

Reference screenshot

Step-4 Subscribe to the event of newly created delegate and paste to the class 

Reference screenshot


  /// <summary>
    ///
    /// </summary>
    /// <param name="_msg"></param>
    /// <param name="result"></param>
    [SubscribesTo(classStr(SLD_DemoSample), delegateStr(SLD_DemoSample, showMessage))]
    public static void SLD_DemoSample_showMessage(Description _msg, EventHandlerResult result)
    {
        result.result(strFmt("Message received from run method is %1",_msg));
    }

Step-5 Use result object of  EventHandlerResult class (2nd parameter) to return the response to caller like result.result(strFmt("Message received from run method is %1",_msg));

Reference screenshot



Step-6 Now call the delegate from the method and pass the required parameter like below.

        EventHandlerResult result = new EventHandlerResult();
        Description message='Hello world this is an event handler';
         
         info('Before..');

         this.showMessage(Message,result);
        
         info(result.result());

         info('After...');

Reference screenshot



Please feel free to contact me if you are facing any issues during the implementation of this blog.

Dialog With Event handling

Dialog With Event handling


Create a Class with the name of Demo Dialog and extend it with RunBase class then write the following code.
dialog_1
Override the dialog method and fields in dialog in my case I added two fields customer account and customer name.
dialog_2
Override the getFromDialog method and set the value in the declared variable
dialog_3
Override the run method write the following code.
dialog_4
If you want to validate then override the validate method.
dialog_5
The dialogPostRun() method should override like below. This will allow calling to the event methods.
dialog_6
Now open dialog and personalize the customer account field and check the name of the field.
dialog_8
Then Create the modified event with a field name.
dialog_7
Now run the class and enjoy the modified events in the dialog ðŸ™‚

Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...