Microsoft Dynamics AX 2012 R3 Programming - Getting Started - Sample Chapter
Microsoft Dynamics AX 2012 R3 Programming - Getting Started - Sample Chapter
$ 59.99 US
39.99 UK
P U B L I S H I N G
Sa
pl
e
P r o f e s s i o n a l
E x p e r t i s e
D i s t i l l e d
Simon Buxton
Mat Fergusson
ee
Simon Buxton
Mat Fergusson
P U B L I S H I N G
Mat Fergusson started his journey into software development in 1998 by studying
software engineering at Sheffield University. After graduating, Mat took to working in IT
infrastructure, quickly progressing from desktop support to infrastructure implementation
and management.
Armed with real-world experience of supporting users and seeing how applications and
infrastructure are actually used, Mat began moving back into full-time application
development, building classic ASP applications at the tail end of the .com boom.
Mat was among the very early adopters of Microsoft's next generation development
platform, .NET. Over the next few years, Mat would develop numerous web applications
and integration projects using VB.NET and later C#, and in 2005, he started developing
mobile business applications built on the Windows CE platform years before the iPhone
made mobile applications ubiquitous.
After completing the first release of an award-winning hosted health and safety mobile
application, Mat decided to gain some broader experience and embarked on a one and a
half year world tour, which saw him broadening both his IT and life skills by working for
the Enterprise Applications division of HP in New Zealand and becoming a certified dive
master in Thailand.
Returning to the UK, Mat teamed up with his old colleagues and between them, they
founded Binary Consultants, where he continues to work today. In the last 6 years, Mat
has continued to refine his .NET development skills in his role as .NET Lead at Binary,
and has been responsible for several highly specialist web applications for the cosmetic
industry in Russia and the rapidly growing online gaming industry in Malta.
In the last 2 years, Mat has started cross training in AX development, an experience that
has helped him directly in writing this book with Simon. Mat's current specialty however,
is the development of .NET applications for Binary's clients that extend the capabilities
of AX by interfacing with the WCF services exposed by the AIF, introducing rich
visualization and interactions that simply aren't possible with the AX client.
[1]
Report
viewer
control
Business
overview
Web Part
WCF
Development tools
Report
viewer
Web Part
Visual Studio
MorphX
.NET Business
Connector
RPC
Metadata
service
Custom
services
User session
service
Document
services
WCF
Workflow
Query
service
System services
Application services
AOS services
Application Object Server (AOS)
Business
database
Model store
database
Microsoft Dynamics AX
Microsoft SQL Server
[2]
...
.NET Framework 4.0
Chapter 1
library/dd362112.aspx.
The element named Application pages using MorphX forms in the preceding
diagram represents the vast majority of all content viewed within the Windows AX
client, for example, the Sales order details form.
All forms written within the AX client development environment under
the Forms node are MorphX forms; they may include .NET controls such
as Print preview and custom .NET controls.
The client primarily uses RPC for communication between the client and the AOS,
but certain elements within the client use WCF; for example, the Report viewer
control. Applications such as Microsoft Office, SQL Server Reporting Services
(SSRS), and custom applications also use WCF. These applications access services
that are hosted by the AOS.
Much of the complexity is hidden from us, and we do not have to remember every
nuance of the integration between the tiers. Gaining an understanding of the
structure, however, is important.
For example, we will later be writing code that will run on a specific tier (AOS
or client). In this case, it is important to pay attention to where the objects are
instantiated and how they are marshalled between the client and server.
[3]
Each element (such as a class, method, database table, field, and so on) has an
element ID. These have many uses, which are mainly internal. One such use is for
handling the link between the elements' stored table definitions and the physical
table definition within SQL Server.
The important point here is that we cannot simply assume that we
can use a model store database with a business database that was not
generated from that model store database.
CIL generation also creates physical DLLs and updates the WCF services (both those
hosted by the AX Service and those hosted by IIS).
[4]
Chapter 1
AX not only had an outstanding IDE (the MorphX IDE) but also a solution to
the problem of upgradability, change tracking, and management of solutions
written by various parties; for example, Microsoft independent software vendors
(ISVs)/Partners and the end-user/customer.
The solution to this was the technology called the layer system.
Layers
Layers segregate modifications (or the creation) of an application object's elements
(for example, a field on a table or a control on a form). This allows Microsoft
partners/ISVs and the customer to write a new functionality or change an existing
functionality in a specific layer without directly modifying the standard software or
the software supplied by the partner or ISV.
The following layers are available from the highest layer down:
Layer
Description
USR
The user layer is for end-user modifications, such as reports. It often has changes
written by the development team of the end-user organization.
CUS
VAR
ISV
When an ISV creates their own solution, their modifications are saved in the
ISV layer.
SLN
FPK
The FPK layer is an application object patch layer reserved by Microsoft for
future patching or other updates. For more information, see Patch Layers [AX
2012] at https://msdn.microsoft.com/en-us/library/aa891248.
aspx.
GLS
SYS
The standard application is implemented at the lowest level, the SYS layer. The
application objects in it can never be deleted.
Although it may seem logical to use the patch layer for updates to your
system, this actually just adds more work and detracts from its goal of
maintainability. It was sometimes useful before we had models, but with
the model technology, it is used for a different purpose.
The current layer is set in the AX configuration utility, and to use any layer other
than the USR layer, you need an access code, which Microsoft provides as part of
your license.
When a change is made to individual elements (methods, fields, form controls, and
so on), it is copied to the current layer. This copy then hides the element in lower
layers. The compiler does this by looking down at the elements from the highest
layer downas if it were looking through the panes of a glassas shown in the
following image (which is taken from Microsoft TechNet):
[6]
Chapter 1
Models
Every element in a layer belongs to one model; if a layer does not have a model, the
system will create a default model for it when a modification is made; for instance,
the USR model. These are not normally used and, in my opinion, should never be
used, as we cannot be sure whether this was intended.
A model exists in a specific layer, and elements in a layer can only belong to one
model; this is by design and should not be thought of as a limitation. Thus, a model
is a collection of elements in a specific layer.
[7]
If you import an add-on as a model, it will be imported into its original layer.
The only thing you have to do is to synchronize, compile the application, and
regenerate the CIL. This model can also be removed; again, all you need to do is
synchronize, compile the application, and regenerate CIL.
You can then, in your layer and model, make changes to the add-on for your specific
requirements; and when the ISV releases an upgrade, you simply have to import the
model and use AX's tools to highlight the changes that may conflict with yours.
Models have many benefits, and I suspect more than what were initially intended
when designed:
For ISVs, models create the following abilities:
Ship the model in a way that is easy to deploy on their customers' systems
Send patches and upgrades to customers that are relatively easy to apply
The partner can more easily use ISV solutions in their offering to their
customers, which allows the partner to focus on their specific value offering
The ability to have more control of their system, where they can choose to
install or remove add-ons with PowerShell commands. Earlier, the customer
could only remove the whole layer and not add-ons within a layer.
As a side effect of the benefits to ISVs and partners, better value and quality
of implementation are achieved, thereby reducing the risk
Don't fall for the trap of creating a model for each requirement
because even with the object model's shredded control or field
level, you will find overlaps (that is, changes required to a method
across two requirements).
[8]
Chapter 1
In our experience, partner and customer layers typically have the following models.
Typical partner (VAR) models are as follows:
The main functionality model (often named as a prefix and the customer
name, for example, axp<CustomerName>)
Labels
Security roles
Add-on models
Labels
The reason a label file is in its own model is that the label file is not shredded into
elements, and therefore, all labels in a label file exist in one model. Each party
would normally maintain one label file, which has a three-letter name, and is
therefore limited.
Ctrl + D: This opens the IDE, with the Application Object Tree (AOT) open.
Ctrl + Shift + P: This opens the development environment, with the Projects
window open.
[9]
Title bar: This tells us which server the client is connected to.
Main menu: This is a classic MDI menu that runs across the top of the
window. There are customization options for some parts of its structure.
Content area: This contains the AOT, Projects window, and code
editing windows.
Properties: This shows the properties of the selected element. It floats when
opened for the first time, but is usually docked to the right.
Status bar: This is configurable through your user options. The most
important information shown here is about the company, layer, and
current model.
AOT
The typical centerpiece of the IDE is the AOT. This contains the entire application
code of AX. The following screenshot shows a tree view, which is organized by
object type:
[ 10 ]
Chapter 1
The button ribbon at the top of the window is context sensitive to the selected node,
and in sequence, they are as follows:
Open: This will open the selected object, such as a form. It is actually an
execute action. So, for a table, it will open the table browser, and for a class,
it will try to execute if it contains a static Main method.
Properties: This will open the property sheet. This action can also be done
by pressing Alt + Enter.
Save all: This will save all unsaved elements. This action can also be done
by pressing Ctrl + Shift + S.
Import: This will open the XPO import dialog, allowing code to be
imported into the system.
To navigate around the AOT, you can use the mouse, but the most efficient method
is to use the keyboard. The AOT behaves intuitively, that is, the right arrow key will
expand a node and the left arrow key will collapse. Partially typing an object name
will move to that object. For example, to navigate to the table named CustTable
from the AOT node, perform the following steps:
1. Press the right or down arrow key. This will move to the first node,
called Data Dictionary, under the AOT.
2. Press the right arrow key again to expand this node.
3. Press the right arrow key again to move down to the first node, Tables.
[ 11 ]
4. Expand the Tables node with the right arrow key, and then press the down
arrow key to move to the first table, as shown in the following screenshot:
5. Start typing CustTable; AX will move the cursor down as you type,
displaying the text typed in the status bar at the bottom of the
application's window.
This may seem quite simple, but it is very important that we are fast at navigating
around the environment so that it is second nature.
We can now look at the property sheet for CustTable. We could use the button on
the AOT window, but this takes time. Instead, use the Alt + Enter keyboard shortcut.
Using keyboard shortcuts is much faster than using the mouse, so
this should always be the preference. You might be slower at first,
but it pays off later.
The following links contain useful shortcuts. You don't need to learn them all, but it
is useful to use the keyboard shortcuts for the commands we frequently perform:
The property sheet will show the properties relevant to the current object type, in the
case where the properties are pertinent to a table.
Although these may seem safe to adjust, any change made to any
property sheet is still a modification and will move the object into
your layer and model, even if you change the property back.
[ 12 ]
Chapter 1
We access most of the tasks we perform on an object through the right-click context
menu (or the Windows context menu key). This menu is context aware, and we will
go through this when we perform the task.
The following screenshot is of the context menu for CustTable, accessed by rightclicking on the CustTable node:
Tables and Views: This opens a table browser, a grid view form
showing all visible fields and the possibility to edit (beware!).
Table maps: This opens a table browser for fields in the map, but will
show no data. This is of limited value.
Forms: This opens the form as if it were opened from the AX client. It
is useful for testing forms as we modify them.
Classes: This will execute the class if it has the Main static method.
Many have assumed that it would open the code editor.
Queries: This will open the Query tool with the query loaded. It is of
limited use.
[ 13 ]
Menu items: This will open the object referenced as if it were opened
from the Windows client.
Jobs: This will execute the job; beware here, as many assume that this
opens the code editor!
View code: This opens the code editor with all methods visible.
Open New Window: This is a very useful feature. It will open a new AOT
window starting from the currently selected node. This is great for the
drag-and-drop functionality.
Save: This will save the changes made to the currently selected object.
Export: This exports the objects from the currently selected node to an XPO
filea file that contains the object definitionso that it can be imported to a
different system.
Delete: This deletes the element at the select level from the current layer.
So, if you change CustTable, you can delete your changes and revert to the
next layer below. This is discussed in more detail in the next section.
Restore: This will revert the selected objects to what is stored in the model
store database, losing unsaved changes. Also, this is useful for loading the
changes made by another developer working in the same system, or to
refresh a form's data source if we add fields to the table it references.
Duplicate: This will create a duplicate of the object. If the object is a table, it
does not copy the data. If the copied object contains static methods, they are
not refactored to the new object name.
Compile: This will compile the current object, or in the case of a node, the
objects within that node.
Find: This will open the Find tool, allowing you to search for an element or
the source code text.
Compare: This opens the Compare tool (discussed in detail later), which
allows you to compare the object across layers.
[ 14 ]
Chapter 1
[ 15 ]
Therefore, all projects we create will be shared. To create a shared project, we follow
these steps:
1. Open the Projects window (Ctrl + Shift + P).
2. Right-click on the Shared node and choose Project, which is under New.
3. This will create a new project named Project1 (if not, it means we have not
given a previously created project a proper name).
4. We should rename this with a suitable name. This is done by double-clicking
on the object (as you do to rename a file in Windows explorer) or choosing
Rename from the right-click context menu. The double-click method is faster.
Naming conventions are critical to getting maintainable code.
These are discussed later in this chapter.
To open the project, simply double-click on it. AX will display a window with a
single node. Elements can now be dragged from the AOT as required.
The next step is to create the project structure. The best structure to use is the same
structure as that of the AOT, as this makes items easier to find. This is done by the
following steps:
1. Right-click on the root node and choose Group, which is under New.
2. Rename this node to be the same as the AOT node we are creating, for
example, Data Dictionary.
3. Open the properties of this node and change ProjectGroupType to
DataDictionary (from the drop-down list), as shown in the following
screenshot. You would then continue this process for the nodes in the AOT.
[ 16 ]
Chapter 1
As you change ProjectGroupType, you will see that the icon in the project node
changes to the icon in the equivalent AOT node.
Creating a project and changing the project group type has an additional
benefit; AX now understands that the element contains objects of that
type, and the right-click | New menu will then offer to create an object of
that type.
Source depot
Visual SourceSafe
MorphX VCS
What Microsoft recommends here is either MorphX VCS or TFS. We prefer to avoid
shared development and use TFS internally, which is integrated with our project
management methodology.
Microsoft provides a good reference for this in MSDN at http://msdn.microsoft.
com/en-us/library/aa639568.aspx. The following table, taken from the preceding
Concurrent development
MorphX VCS
TFS
No
Yes
Isolated development
No
Yes
Change description
Yes
Yes
Change history
Yes
Yes
Yes
Yes
Branching
No
Yes
No
Yes
Labelling support
No
No
[ 17 ]
The isolated development feature means that each developer has an isolated AX
environment. The developer's machine (usually a virtual server) would need to be
reasonably powerful in order to run the environment at a suitable speed.
It also has a more involved process; that is, the developer has to check in their work
(which is file-based), and then a build has to be created.
The main advantage of TFS over MorphX VCS is the integration with work items,
which can be used alongside your sure-step agile methodology. We also have an
advantage in that the source is also held in a separate repository.
The MorphX VCS provides features similar to TFS (except branching), but it allows
the development server to be shared by multiple developers. Objects are still checked
out (and therefore locked) and checked in with comments. It also allows the use of
the built-in code compare tool to review the changes between different versions.
This is a simple, yet clever, version control solution, but it also has one drawback.
The version control information is stored in a set of tables in the business database.
This means you lose the version control data if you rebuild the business database
from the live business database.
Naming
First, all the objects you create or elements added to an object authored by another
party must be prefixed. The prefix is usually specific to your organization; in the
case of Binary, we use two prefixesBCL for all work we carry out as a VAR, and
AXP for our ISV work. The prefix cannot be of any three characters; for example, Sys
would be a bad prefix because it is also used by Microsoft for their system objects.
When adding element to your own objects, do not prefix them. All type names
(extended data types, tables, classes, and so on) must begin with a capital letter,
and all instance variables and methods must have a lowercase first letter.
[ 18 ]
Chapter 1
Although the AX compiler is not case-sensitive, the CIL is; thankfully, this compiles
from the p-code, so it isn't normally a problem. It is nonetheless good practice to
write code as if it is case-sensitive. This also helps make the code much easier to read.
The second part of naming is that you should capitalize the first letter of each
word in the type, method, or variable name; for example, CustTable and the
initFromCustTable method. This, again, makes the code much easier to read.
Commenting
A version control system cannot replace good code commenting. Any change made
to an object that was not authored by you should be tagged with the following:
Your initials
The date
A few words as to the reason for the change and what the change is
intended to do
If you add a method to an existing object, you should add a header comment.
This can easily be done by entering /// in the first line of the method. It should
contain the comment line, as shown in the preceding code snippet.
[ 19 ]
If you have made a change to a form layout, you should add a method called
DevelopmentHistory highlighting the changes. Otherwise, you may realize that it
gets lost when another developer applies an update or hotfix. This content format is
up to your organization; this would normally be a list of changes formatted in a table
in the same format as the earlier change tag.
Adding comments is often a pain and, when focused on writing an exciting piece of
code, they are easily forgotten. One way to solve this is by making the addition easy
by modifying the EditorScripts class.
The EditorScripts class has methods that create code blocks and even entire
methods for you, either through the right-click menu or by typing a keyword. The
class can be modified by adding a public method that accepts an Editor instance.
Appendix A, Code Editor Scripts, covers this topic.
Editor scripts
Editor scripts are code snippets that insert code into the current method, and can
even ask for information as part of the process. They insert either statement blocks,
such as a switch statement block, or an entire method. They are simple to enter;
simply type the script name and press the Tab key.
For example, if we have a class called BclFleetVehicleTableType and enter the
text construct in a method, the following code is inserted:
public static BclFleetVehicleTableType construct()
{
return new BclFleetVehicleTableType();
}
The available scripts can be seen by right-clicking on the method and navigating to
Scripts | template, as shown in the following screenshot:
[ 20 ]
Chapter 1
Patterns
A good example of how patterns can be used is demonstrated by the following
requirement: we require a method to edit the customer credit limit from the customer
list page. This should be a button on the action page that will open a dialog, showing
the current credit limit and allowing the user to update it. When the user clicks on
OK, it will update the customer's credit limit.
There are many ways to do this, and it may seem appropriate to create a form that
initializes from the current customer on the list page, and updates the record when
the user presses OK.
This is until we get the next series of requests:
Can we place the resulting sales orders that fail the credit limit in
a workflow?
The preceding requests are clearly additional work, but the code should require
only extending and not rewriting. What often happens is that the design is
changed to such an extent to try to fit the requirement that it is barely readable
by the original developer.
The actual design we should have adopted would be similar to the following:
The form, instead, interacts with a data contract, which is initialized from
the caller
A data contract is a class that has a set of public properties that form a structured
dataset, allowing the data to be passed to and among objects. They can also cross
tiers across the architecture and be marshalled to external services.
The additional work is to create two classes and about 10 lines of code. This will
make the solution much easier to extend, and if adopted by a team, easier to read
and maintain.
Many development teams spend some effort in adopting patterns, and sometimes
evolving them over time. We have many patters in AX, and most of the tasks we
need to perform can be done by following an existing pattern.
Microsoft has published the reference patterns that we should follow at Microsoft
Dynamics AX Design Patterns, http://msdn.microsoft.com/en-us/library/
aa851876.aspx.
Frameworks
The frameworks in Dynamics AX are designed to be either used or extended.
They are often a complicated set of tables and classes, and sometimes contain a
user interface. We take advantage of them by extending and/or implementing a
specific class.
Most of the key functionality in AX is accessed through the existing business logic;
this is true for some master data tables, and certainly true of all transactional data.
For example, in order to post a sales order confirmation, we would use
the FormLetter framework. In this case, the framework provides the
FormLetterService class, which has the postSalesOrderConfirmation method.
Other frameworks are designed to assist us in common development patterns or
provide access to features that are not directly accessible from X++. In the preceding
example, we could've used the SysOperation framework, which allows us to
interact with the batch framework, thereby allowing asynchronous code execution.
We will use and extend these frameworks as we progress through the chapters.
[ 22 ]
Chapter 1
Extendable: The solution (no matter how small) should be designed such
that it can be extended without rewriting. Using patterns can help with this.
Design around standard patterns: This helps avoid reinventing the wheel
and ensures a higher quality design that is more consistent. The solution
we write should look and feel like any other part of AX, which means less
training and better adoption by our users.
Validate the design: This should be done against the requirement before the
work on the design commences. In some cases, it is appropriate to prototype
the solution.
Data structures
The first part to design is the data structures. Even though these may evolve, a lot of
attention is required here. A simple analogy would be to compare this to building
a house; you can change the furniture, even add a room, but you cannot add a
basement garage.
[ 23 ]
There is some guidance to be found here; that is, the structure will normally fit into
an existing pattern that has already being used by AX. We normally expect to have
the following types of tables in our solution:
Parameter: This is a single record table that typically contains the table's
defaults and other behavioral properties, such as the default printer settings.
Main: These are the master data tables, such as items and customers.
Worksheet header and Worksheet line: These are the standard document
entry tables, such as the sales order table and lines where the dataset has a
header and lines.
Worksheet: This is a table used to enter data that is later processed in some
way. This not very common in AX, as most documents have both a header
and lines.
Transaction header and Transaction lines: This is the result of the worksheet
after posting, or committing, such as the sales invoice header and sales
invoice lines.
During the course of this book, we will be writing a solution based on a hypothetical
case of a fleet management system as Contoso's internal development team. Based
on the previous guidance, we can take the following simple requirement and quickly
turn it into a data structure: we require the ability to view and maintain records of
vehicles and their utilization.
[ 24 ]
Chapter 1
From this, we can see that we will need to store a list of vehicles, probably of varying
types, and then a record of their utilization will be attached to each vehicle. This
leads us to the following structure:
Table type
Parameter
Description
The main parameters
Group
ConFMSVehicleGroup
Main
ConFMSVehicleTable
A list of vehicles
Transaction
ConFMSVehicleTrans
In this case, little thought was required, as the requirement fits nicely into the
standard AX patterns.
The naming was also straightforward: the organization's prefix is Con, the module's
prefix is FMS, and the rest of the name comes from the entity name and the table
type. You will see this type of naming throughout AX. As far as possible, we
should be able to understand from the name alone what it is, what it does,
how it does it, and why.
List page: This is the main entry point to both main tables and worksheet
tables. The list page offers the ability to search and act upon the data. You can
also open a details form from list pages that allows the record to be edited
or a new entry to be created. An example of it can be found by navigating to
Forms | CustTableListPage.
Simple list: This is useful for group and transaction tables that are a simple
list of records with only a small number of fields to present. An example of
this can be found by navigating to Forms | CustClassificationGroup.
[ 25 ]
Simple list, details: This is where the group table has many fields that are
better arranged in groups of fields. There is still a list element, but there is
a pane that allows easier entry of data. An example of this can be found by
navigating to Forms | DeliveryMode.
Table of contents: This template is used for parameter forms. For example,
Forms | CustParameters.
Dialog: These forms follow a style design to be used as a popup to gather
some information and return it to the caller. A form used to create data is
likely to use this template, as we enter data and then ask AX to create the
record once complete.
Drop dialog: These appear as if they drop down from the calling button,
and are typically used to perform an action using the current record. They
are designed to display a small number of form controls, which the user will
update as required before pressing OK. The following screenshot shows this:
Based on the preceding templates, we can create the following forms for our
example requirement:
Chapter 1
We normally name forms after the table they act on, except for list pages. The last
example does not really have a true template; the simple list is the closest. The data
structures and forms are taken directly from how other parts of AX behave. This
should always be a design goal as it ensures that our users' incumbent knowledge of
AX is used, reducing the time to learn the new feature and minimizing errors.
Application logic
Classes are used to store the main business logic, and are often used to handle
user interface and table events. This allows the code to be reused and extended
more easily.
The other key point is that we should also consider that we may want our solution
available for mobile devices, and careful thought about how we write the logic can
make this much easier.
Classes in AX can be deployed as custom services that are consumed as WCF
services in mobile or other Windows applications. This means that the logic should
be simple to access, and having business logic in distinct classes allows the logic to be
reused safely.
For example, we have a table with a status field that performs some validation and
other events. We would write this in the data contract / service class patterna data
contract to store the data to be acted upon, and a service class to process the change.
This can be used on a dialog form with the AX client or exposed as a service allowing
a mobile application to set the status.
Eventing
Eventing is a new key feature in AX 2012 that, along with Models, is a key enabler
for ISVs to write add-ons for AX.
[ 28 ]
Chapter 1
Event subscription
This technology allows you to subscribe to an event in AX. So, instead of modifying
the method, we simply subscribe to it as a pre or post event. We can then perform
our logic elsewhere in an X++ or .NET assembly, and even modify method
parameters and return values.
This subscription is not a modification to the source method, and therefore,
a method can have subscriptions in several different models in the same layer.
Delegates
Delegates are empty method stubs to which we subscribe. They are created by
the original author and called from within their code. This is done so that you can
subscribe to events that happen within a method without modifying a method.
For example, the following lines of code would be a delegate written by an ISV
(Axp is the prefix used by this ISV):
delegate void weightCaptured(AxpWeighTicketContract _contract)
{
}
The ISV will then call this delegate at the appropriate time with the data contract.
By subscribing to this delegate, you can handle the event and perform the necessary
tasks with the supplier contract. This provides a hook into the supplied solution
without modifying it. For more information about the guidelines on delegates,
visit http://msdn.microsoft.com/en-us/library/gg879953.aspx.
Extending a class
Another method of modifying behavior with a minimal footprint is to extend
the class. This should be done only if you cannot perform the task through an
event subscription.
The pattern is implemented with the following steps:
1. Create a new class and use the extends keyword to extend the class you
need to change.
2. Override the method you wish to change.
3. Modify the construct method on the base class so that it constructs your
class based on the criteria you want.
[ 29 ]
A data bound field's modified event will call the data source field's
modified method
The bound data source field's modified event will call the table's
modifiedField event with the field ID of the field being modified
The table's modified event is normally a large switch statement based on the field
ID parameter with a case for each handled field. To modify this, we can simply add a
case for our new field or change the behavior of an existing case.
In this situation, we should subscribe to the modifiedField method, but another
option would be to create a new method with the same profile. This is preferable if
we are the end-user organization, but required for VARs and ISVs. This is done by
copying the method header. Let's take a look at the following example:
public void modifiedField(FieldId _fieldId)
We can then create a switch statement to handle the fields we need to in this new
method. In order to call this new method, we can then add a call to our new method
in the original modifiedField method:
public void modifiedField(FieldId _fieldId)
{
super(_fieldId);
switch (_fieldId)
{
case fieldNum(<TableName>, <FieldName>):
// code
break;
}
// Con Var modification by SB 2014/06/09 handle custom fields
this.conModifiedField(_fieldId);
}
[ 30 ]
Chapter 1
Summary
In this chapter, we covered a lot of the groundwork, from the technical architecture
of Dynamics AX to the development practices. Following this guidance should
both save time in development and allow solutions to be developed that fit in the
standard AX practices to be developed.
In the next chapter, we will go through the preparation work that we perform
each time we start a new project, putting into practice the concepts covered in
this chapter.
[ 31 ]
www.PacktPub.com
Stay Connected: