Skip to main content

Code behind extension forms: How to add state variables and override methods without overlayering

Code behind extension forms: How to add state variables and override methods without overlayering


The August 2016 release of the new Dynamics AX platform (aka platform update 2 or platform 7.2) enables developers to define form extension classes. Why is this feature useful? In previous releases of the new Dynamics AX, developers could extend form metadata and author form event handlers, but there was no simple way to define a new form variable and store a form state to use across different event handlers.
This articles gives an example on how to add a state to a form extension and how to use a form extension class to override a form method without overlayering the source code of the form. I hope you find it useful.

Override a method using a form extension class

I will start with an example showing how to override a method in the form extension class.
Consider the following scenario:
  • A form named Form1.
  • A data source in Form1 named Table1 and referencing the table Table1.
  • A field in the data source named Field1.
A developer who needs to override the JumpRef method for field1 can user a form extension class as follows.

[ExtensionOf(formstr(Form1))] 
final class Form1_Extension // This name can be anything, the only condition is that it ends with the _Extension suffix, no need to name it Form1
{
[FormEventHandler( formstr( Form1 ) FormEventType::Initialized)]
public void initializedFormHandler(xFormRun formRun, FormEventArgs e)
{
FormDataObject field1DataObject = this.table1_ds.object( fieldNum(Table1, field1 ), 1); 

field1DataObject.registerOverrideMethod( methodStr(FormDataObject, jumpRef), formMethodStr( Form1, jumpRefImplementation), this);
}

public void jumpRefImplementation(FormDataObject dataSourceField1)
{
// Your implementation of the override logic
}
}

Add a state to a form extension

In the scenario above, what if the developer needs to add a state to the existing Form1, but does not want to edit or overlayer the form's source code? In the Form1_Extension class, the developer can add member variables and these variables will be associated with the Form1 instance. In the following example, var1 and var2 integers are added as state variables.

[ExtensionOf( formstr( Form1 ) ] 
final class Form1_Extension
{
public int var1;
public int var2;

/*
These variables can now be referenced from any instance method in the extension class. In order to complete this picture, imagine Form1 has one button, named Button1. You can implement in the same Form1_Extension extension class an event handler as follows.
*/

[FormControlEventHandler(formControlStr(Form1, Button1), FormControlEventType::Clicked)]
public void onMatchNo_Clicked(FormControl sender, FormControlEventArgs e)
{
var1 = 10;
var2 =20;
}
}

As you can see, the event handler has not been defined as a static method (it is an instance method). This makes it possible for the handler to reference any state (variable) defined in the extension class. Another important aspect is that an instance method in a form extension class can make a reference to any of the public member variables, controls and data source objects defined on the form directly using the reference to “this”. 

Comments

Popular posts from this blog

Number sequence with existing module and new module

Number sequence customization in Dynamics 365 Create a number sequence in a standard module Step 1: Create new EDT Step 2: Add a new code block to the loadModule in the NumberSeqModuleXXX class Step 3: Add a new static method to the parameters table of the module to get the number sequence reference Using Extension [ExtensionOf(classStr(NumberSeqModuleCustomer))] final class NumberSeqModuleCustTest_Extension { protected void loadModule() { NumberSeqDatatype datatype = NumberSeqDatatype::construct(); next loadModule(); datatype.parmDatatypeId(extendedTypeNum(TestId)); datatype.parmReferenceHelp(literalStr(‘Test ID’)); datatype.parmWizardIsContinuous(false); datatype.parmWizardIsManual(NoYes::No); datatype.parmWizardIsChangeDownAllowed(NoYes::No); datatype.parmWizardIsChangeUpAllowed(NoYes::No); datatype.parmSortField(1); datatype.addParameterType(NumberSeqParameterType::DataArea, true, false); this.create(datatype); } } [ExtensionOf(tableStr(CustParameters...

[D365/AX7] How to: Create and Use number sequence

[D365/AX7] How to: Create number sequence Posted on January 7, 2018 Updated on January 11, 2018 Number sequences are used to generate readable, unique identifiers for master data records and transaction records that require identifiers. A master data record or transaction record that requires an identifier is referred to as a reference. On this example, we are going to create a new number sequence for customer group. Create a new class that extends the NumberSeqModuleCustomer class and then create the method loadModule_Extension and add the following code: class NumberSeqModuleCustomer_CFS extends NumberSeqModuleCustomer {         public void loadModule_Extension()         {                 NumberSeqDatatype datatype = NumberSeqDatatype::construct();          ...

Batch Multi threading D365FO AX7

Batch Multi threading D365FO AX7 Normally, when we schedule our RunBaseBatch class as a batch job, it will create single task. Which will responsible to execute complete process in single thread. But, As you know like AX-2009 and AX-2012. In D365FO We can add multiple AOS server in single batch group and they can process the multiple task simultaneously.  In this demo I will show, How to create multi threading Batch job. following are the steps Step-1 Create a table for demo purpose in our demo we keep table name is  SLD_DemoTable . Step-2 create a class with name  SLD_DemoBusinessLogic  and write your business logic in this class. In demo I write only insert records in table and show you guys how it works. Reference Code class   SLD_DemoBusinessLogic {      public   void  processInit( VendTable  _vend)     {          SLD_DemoTable  ...