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

Override Lookup AX7 & D365FO


Override Lookup AX7 & D365FO



Here is a small example to override lookup.


copy the lookup event handler of the field and  paste the event into any class




/// <summary>
    ///
    /// </summary>
    /// <param name="sender">receiving value in parameter</param>
    /// <param name="e">receiving value in parameter</param>
    [FormControlEventHandler(formControlStr(PayrollEmployerTaxRegion, Overview_StateId), FormControlEventType::Lookup)]
    public static void Overview_StateId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
           /// write your lookup code here

if the control or field already has a lookup to we need to cancel parent lookup execution otherwise we will get an exception.

below code you can use to cancel parent lookup ---
        FormControlCancelableSuperEventArgs ce = e as FormControlCancelableSuperEventArgs;
        //cancel super() to prevent error.
        ce.CancelSuperCall();
      }


Here is the complete code sample 


/// <summary>
    ///
    /// </summary>
    /// <param name="sender">receiving value in parameter</param>
    /// <param name="e">receiving value in parameter</param>
    [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();
    }


Grid/List row color change using method override D365



Grid/List row color change using method override D365

Today, I received the requirement from my functional to change the row color Grid/List page on any specific condition... 

As we all knows in custom form its really easy to achieve but in my case I need to do this on exists form using method override/Event handler of Data source..

So I start searching over internet and community and found that lots of people were facing the same issue because this feature is not available in extension...

After the resolution of this task i decide to share with everyone..


There is an event available in data source OnDisplayOptionInitilized. I copied the event handler and paste on my class.



Following are the code 

[FormDataSourceEventHandler(formDataSourceStr(JmgRegistrationTouchAssignedJobs, JmgJobTable), FormDataSourceEventType::DisplayOptionInitialize)]
public static void JmgJobTable_OnDisplayOptionInitialize(FormDataSource sender, FormDataSourceEventArgs e)
{

}

Now another challenge is ready for me to find the display Option and set the back-ground color..
After one hour of efforts found a class FormDataSourceDisplayOptionInitializeEventArgs which we can use to set the color




Following is the complete code 
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[FormDataSourceEventHandler(formDataSourceStr(JmgRegistrationTouchAssignedJobs, JmgJobTable), FormDataSourceEventType::DisplayOptionInitialize)]
public static void JmgJobTable_OnDisplayOptionInitialize(FormDataSource sender, FormDataSourceEventArgs e)
{
FormDataSourceDisplayOptionInitializeEventArgs fdsoption;
fdsoption= e as FormDataSourceDisplayOptionInitializeEventArgs;
fdsoption.displayOption().backColor(WinAPI::RGB2int(161,161,255));
}


Here is the result of my all efforts.



Event handler for QueryExecuting to filter data

Event handler for QueryExecuting to filter data


You can use the below code sample to filter data in query execution event

1. Copy Form ->Data source  >Event > onQueryExecuting.
2. Paste this code in the New class.



  [FormDataSourceEventHandler(formDataSourceStr(PayrollEmployerTaxRegion, PayrollEmployerTaxRegion), FormDataSourceEventType::QueryExecuting)]
    public static void PayrollEmployerTaxRegion_OnQueryExecuting(FormDataSource sender, FormDataSourceEventArgs e)

    {

        sender.query().dataSourceName(sender.name()).addRange(fieldnum(PayrollEmployerTaxRegion,     CountryRegionId)).value(queryValue(LogisticsAddressCountryRegion::findByISOCode(SysCountryRegionCode::countryInfo(curext())).CountryRegionId));

    }

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...