Untitled

As we are aware of Overlayering concept used in previous version of AX where we used to modify metadata and source code, in higher layers which may increase the cost of upgrading a solution to newer version.

In AX7 onward Microsoft introduced a concept of extensions described as below.

Extensions

You can customize an application by using extensions. An extension enables you to add functionality to existing model elements and source code. Extensions provide the following capabilities:

  • Creating new model elements.
  • Extending existing model elements.
  • Extending source code using class extensions.
  • Customizing business logic. Ways to customize business logic include:
    • Creating event handlers to respond to framework events, such as data events.
    • Creating event handlers to respond to event delegates that are defined by the application.
    • Creating new plug-ins.

So, in this post I would try to cover Extension on objects and what can be achieved using extensions.

Code Extension

There are three ways of extending the code

  • By subscribing to events (framework events and delegates)
    • Events are implemented as multi-cast delegates, which means that more than one event handler can be subscribed to any particular event.
    • Events are broadcast; there’s no sequencing of calls to event handlers.
    • Event handlers execute within the transaction scope of the base methods.
  • By writing plug-ins (extension points that are defined by the base application. By using a class-factory pattern, plug-ins enable you to replace the base functionality.)
  • By creating class extensions

Form Extension

Form functionality can be extended through its controls and data sources. Following can be achieved using extension in a form:

  • Add a new control.
  • Enable or disable a control.
  • Change the text or label property of a control.
  • Change a control’s visibility.
  • Change a form’s help text.
  • Change a form’s caption.
  • Add a new data source.
  • Add a form part.

In Microsoft Dynamics AX 2012, you could override form methods. In the current version, you use extensions to implement event handlers that are called from the base implementations of form methods.

For example:

Validatewrite has two events ValidatingWriting (Preceding event) and ValidatedWrite (Succeeding event)

Code behind extension forms

Class extensions can be used to write X++ logic associated with form extensions. This allows the definition of state variables accessible to form and control event handlers. It also allows overriding form methods without overlayering code. Refer to this blog article for an example.

[ExtensionOf(formstr(Form1))] 

final class Form1_Extension
{
[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);
}

Table Extension

Table extensions are used to extend table’s design and logic. Following can be achieved using extension in a table:

  • Add new fields
  • Add new field groups
  • Add new indexes
  • Add new mappings
  • Add new relations
  • Add new fields to existing field groups
  • Change label of table field
  • Update Created By, Created Date Time, Modified By, Modified Date Time properties
  • EDT property can also be updated on fields and set it to EDT that is derived from the current EDT

In AX 2012, you could override the virtual methods of a table’s base class to control the behavior that occurred during table operations, such as when creating, reading, updating, or deleting. In the current version, you instead use extensions to implement event handlers that are called from the base implementations of the table methods.

Table extension class example:

class LC_SalesTableEventHandler

{

/// <summary>
/// Defaulting Asset Model and Asset Serial number upon changing of Asset Id
/// </summary>
/// <param name=”sender”>Sales order table</param>
/// <param name=”e”>ModifyFieldEventArgs object</param>
[DataEventHandler(tableStr(SalesTable), DataEventType::ModifyingField)]
public static void SalesTable_onModifyingField(Common sender, DataEventArgs e)
{

SalesTable   salesTableLocal = sender as SalesTable;
ModifyFieldEventArgs eventArgs = e as ModifyFieldEventArgs;

ttsbegin;
if (fieldNum(SalesTable, LC_AssetId) == eventArgs.parmFieldId())
{
if (salesTableLocal.LC_AssetId)
{
AssetTable assetTabelLocal = AssetTable::find(salesTableLocal.LC_AssetId);
if (assetTabelLocal)
{
salesTableLocal.selectForUpdate(true);
salesTableLocal.LC_AssetModel = assetTabelLocal.Model;
salesTableLocal.LC_AssetSerialNumber = assetTabelLocal.SerialNum;
salesTableLocal.doUpdate();
}
}
}
ttscommit;

}
/// Defaulting Asset Model and Asset Serial number upon changing of Asset Id
/// </summary>
/// <param name=”sender”>Sales order table</param>
/// <param name=”e”>InsertingEventArgs object</param>
[DataEventHandler(tableStr(SalesTable), DataEventType::Inserting)]
public static void SalesTable_onInserting(Common sender, DataEventArgs e)
{

SalesTable   salesTableLocal = sender as SalesTable;
if (salesTableLocal.MatchingAgreement)
{
SalesAgreementHeader salesAgreementHeader =          SalesAgreementHeader::find(salesTableLocal.MatchingAgreement);
if (salesAgreementHeader)
{
salesTableLocal.LC_AssetId = salesAgreementHeader.LC_AssetId;
salesTableLocal.LC_AssetModel = salesAgreementHeader.LC_AssetModel;
salesTableLocal.LC_AssetSerialNumber = salesAgreementHeader.LC_AssetSerialNumber;
}
}
}

}

Enum extensions

You can extend any Enum that is marked extensible (IsExtensible=True).

Untitled

  • Using extension, you can add new Enum values to it.
  • Only issue you have using extension in Enum is that integer values that belong to the baseline enum are deterministic where integer values that are an extension are not deterministic.

EDT extensions

You can extend an EDT element in order to modify any of the following properties:

  • You can change Form help
  • You can modify Label
  • You can update String size
  • You can modify Help text

Query extension

You can extend a Query element to achieve the following:

  • Add ranges to an existing datasource
  • Add new (embedded) data sources to an existing data source
  • Add new fields to an existing data source

Menu extensions

You can extend a Menu element to achieve the following:

  • Add new menu items, submenus, menu references and tile references to an existing menu.
  • Hide an existing menu item, tile, or sub-menu in a menu using the Visible

Security role and duty extensions

  • You can extend a Role or a Duty to add new duties/privileges.

Label extensions

Label extension files allows to modify the string value of a label, add new labels to the same label file or add new languages. To create a label extension file you must name it with a _extension suffix.

Leave a Reply

Recent Comments

    Archives

    Categories