Introduction
This article is dedicated to Microsoft Dynamics Axapta and the C# community. Microsoft dynamics Axapta is an ERP for large scale industries. Every large organization has their own legacy system which they want to integrate with Microsoft dynamics Axapta so that without much effort they can leverage Microsoft Dynamics Axapta and their existing C# application. To understand it better, assume that the clients' C# application is having the UI with some functionality and we want to utilize that functionality in Microsoft dynamics Axapta without building that functionality from scratch but instead only passing the desire input for C# application from Microsoft Dynamics Axapta and in turn getting the output from C# application.
In order to understand the advantage of this article, please read the following scenario.
Suppose Client A is having one application B which is working perfectly fine to accomplish their small business goal. Now after some time, they decided to implement ERP Microsoft Dynamics Axapta to streamline and enhance their business process and to obtain profit. Suppose, the functionality B is not in ERP Microsoft Dynamics Axapta and client does want to implement that functionality in Axapta as already they are satisfied with that and application is having the DLL so that it can be used by other application. Hence to achieve this, we can integrate the application B of Client A to Axapta and thus by achieving this type of integration, client does not need to invest time and money in implementing the functionality to a larger extent. This article will also help any ISV partner for Microsoft Dynamics Axapta to have a POC of how to integrate the Axapta with C# application. Any ISV partner can develop their add-on and then expose their functionality as an DLL with API, which in turn Axapta will be utilized so that Axapta functionality is integrated to Add-on.
To develop a POC, I had developed a C# DLL “CvrtC2Frnt.dll” with project name as CvrtC2Frnt
. After that “CvrtC2Frnt.dll” is utilized in Axapta to integrate the Convert Temperature application with Axapta table and form.
I am providing the source code for the following items:
- “CvrtC2Frnt.dll”
- Axapta code to integrate with Convert Temperature application
In addition to the above, I am also providing a sample application to test the CvrtC2Frnt.dll without using Axapta too.
There are many ways to integrate Axapta with .NET applications. In my upcoming articles, I will be covering some other aspects of integrating Axapta with .NET applications.
Let's begin...
Step 1: Creation of “CvrtC2Frnt.dll"
Create a C# project of type Class Library with the namespace CvrtC2Frnt
. Let us assume that we have named the solution, "CvrtC2Frnt
."
Step A
First, we will create the “CovertTemp
” class. We will create the following members as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CvrtC2Frnt
{
public class ConvertTemp
{
public static double sourceTemp=0, destTemp=0;
public static bool Faren = false;
public static double ConvertCelsiusToFahrenheit(double c)
{
destTemp = 0;
destTemp = ((9.0 / 5.0) * c) + 32;
return ((9.0 / 5.0) * c) + 32;
}
public static double ConvertFahrenheitToCelsius(double f)
{
destTemp = 0;
destTemp = (5.0 / 9.0) * (f - 32);
return (5.0 / 9.0) * (f - 32);
}
}
}
The above code clearly specifies that two functions are created to convert temperature from Fahrenheit to Celsius and vice versa.
Step B
Now, we will add new windows form “ShowTemperature
”, which will contain the UI for leveraging the functionality and interface to applications like Axapta.
Add the following controls on the form:
- Text Box of name
txtSourceTmp
- Text Box of name
txtDestTmp
- Label Box of name
lblsrcTemp
with blank value in Text property
- Label Box of name
lbldesTemp
with blank value in Text property
- One Command button with Text as
Convert
and name as btnConvert
Note: Text of labels will be displayed dynamically depending upon the type of conversion like if conversion is taking place from Fahrenheit to Celsius, then label lblsrcTemp
will contain the Text as Fahrenheit and “lbldesTemp
” as Celsius.
Step C
Now, create the following functions of class ShowTemperature
:
public ShowTemperature(double Temperature, Boolean Fahrenheit)
{
InitializeComponent();
txtSourceTmp.Text = Temperature.ToString();
ConvertTemp.sourceTemp = Temperature;
ConvertTemp.Faren = Fahrenheit;
}
private void btnConvert_Click(object sender, EventArgs e)
{
if (ConvertTemp.Faren == true)
txtDestTmp.Text = Convert.ToString
(ConvertTemp.ConvertFahrenheitToCelsius(ConvertTemp.sourceTemp));
if (ConvertTemp.Faren == false)
txtDestTmp.Text = Convert.ToString
(ConvertTemp.ConvertCelsiusToFahrenheit(ConvertTemp.sourceTemp));
}
private void ShowTemperature_Load(object sender, EventArgs e)
{
if (ConvertTemp.Faren == true)
{
lblsrcTemp.Text = "Farenheit";
lbldesTemp.Text = "Celsius";
}
else
{
lblsrcTemp.Text = "Celsius";
lbldesTemp.Text = "Farenheit";
}
}
Step D
Now, create the function ShowResult
in class ConverTemp
as follows:
public void ShowResult(double Temperature, bool Farenhite)
{
ShowTemperature sT = new ShowTemperature(Temperature, Farenhite);
sT.Show();
}
The above function in created to first call the constructor of ShowTemperature
class with Temperature and type of temperature (Fahrenheit or Celsius) values. Afterwards, ShowTemperature
UI is called to display corresponding UI.
Step E
This step is optional as already our DLL is created but to test before we actually use it in Axapta, we can perform Step E.
Add a New project of type Windows Form Application to the existing solution. Put the name of project as ShowUI
. Add a reference of CvrtC2Frnt.dll
in references.
Now design the form as given below:
The above form will work as follows:
If the value in Temperature
Text box is as 50 and Fahrenheit check box is checked out, then it means that Temperature is given in Fahrenheit. When user will click on button Show Convert Temperature UI
, then UI which was created in step B will be displayed.
Now the code behind Show Convert Temperature UI
button is as follows:
private void btnShowUI_Click(object sender, EventArgs e)
{
ConvertTemp ct = new ConvertTemp();
if (chkFaren.Checked == true)
ct.ShowResult(Convert.ToInt32(txtTemperature.Text), true);
else
ct.ShowResult(Convert.ToInt32(txtTemperature.Text), false);
}
Now build the application and run it to test the result.
Click on this to get the UI for converting Fahrenheit to Celsius.
Click on Convert button to convert Fahrenheit to Celsius
Step 2: Integrate the CvrtC2Frnt.dll with Axapta
To perform step 2, you need to have Microsoft Dynamics Axapta installed in your system. Assuming that this is installed and running, follow the steps given below:
Step A
- Copy CvrtC2Frnt.dll file into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.
- Open AOT (Application object tree). AOT is a development environment in Axapta. By using AOT, we can customize and develop new functionalities in Axapta.
Click to Open AOT or Press CTRL+D
- Now under AOT, go to References and expand it. Then click over Add Reference. Browse the CvrtC2Frnt.dll file into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.
Click on Browse and select file CvrtC2Frnt.dll
- Click on Open after selecting CvrtC2Frnt.dll file from into the Bin folder under Microsoft Dynamics Axapta installable folder. i.e. C:\Program Files\Microsoft Dynamics AX\50\Client\Bin.
Click on Open
- Click on Ok to add the reference.
Click on Ok to add reference.
Step B
- Now create a New Project in Axapta name as
ConverTemp
. Press CTRL+SHIFT+P to open Project window and then right click on private
node of Project window. Select New-> Project to create New Project.
Click on Project to create New Project
Rename the new project as ConverTemp
.
- Create two groups and rename it to Form and Class.
Click on Group two times to create new Groups and rename it to Form and Class
- Now right click on Class node under
ConverTemp
project and click on New-> Class to create new Class. Rename it to Temperature
.
Click on Class to create new Class in Axapta
- Now go to Class Declaration which is under and click Edit to open it.
Click on Edit to edit class declaration of ConvertTemp Class
- Write the following code in class declaration:
class Temperature
{
CvrtC2Frnt.ConvertTemp ct;
}
The above code is creating the object for ConvertTemp
class which is defined in CvrtC2Frnt
DLL.
- Add a new method
ConvertTemperature
to Temperature
class in Axapta. Right click on temperature class and click on OverideMethod->New to add a new method. Rename the new method as ConvertTemperature
.
Click on New under OverideMethod to create new Method.
- Right click on
ConvertTemperature
method and select Edit to add code to it.
Click on Edit to edit the class
Add the following code to the editor:
void ConvertTemperature(real Temperature, Boolean Fahrenheit)
{
ct = new CvrtC2Frnt.ConvertTemp();
ct.ShowResult(Temperature, Fahrenheit);
}
The above function is taking two parameters as Temperature
and Fahrenheit
. Both parameters are passed to the ShowResult
API defined in CvrtC2Frnt
DLL.
- Right click
Temperature
class and select Save
to save and compile the class.
Click on Save to save and compile the Temperature Class
Step C
- We will create two new fields in
SalesTable
of Axapta:
- Temperature
- Farenhite
-
Open AOT and go to data dictionary. Click on Tables and search for SalesTable
.
Select SalesTable
.
Select SalesTable
Go to fields and right click over it and select new field of type real.
Rename it Temperature.
Add a new field as define of type as enum
.
Click Enum
Rename it to Farenhite.
Select the value in extended type property as Temp Type which is again a user enum
of type NoYes. Hence before creating Farenhite field, first create the User Enum TempType
.
To create the TempType Enum
, go to Extended Data Types in Data dictionary and right click select new->Enum
.
Click ExtendedData Type -> Click Enum
Rename it to TempType
and go to property. Select EnumType
property value as NoYes.
Select NoYes
Now save it and go to SalesTable
under DataDictionary
. Search the Farenhite field created earlier and then go to ExtendedData
Type property and select TempType
as value.
Select TempType
Now save it.
- Now we will add two fields created in the above step to
SalesTable
Form.
Go to SalesTable
form under Form section in AOT. Select Designs->Design->Group:Table
->Tab:TabHeader
->TabPage:TabHeaderOverview
->Grid:GridHeader
and drop Temperature, Farenhite field from Datasource
of SalesTableForm
in it.
Now Save it.
- Now Drag and drop the
SalesTable
Form from AOT to Form
group under ConverTemp
project in Axapta. Please note that Form group is created under point 2 of Step B.
- Now, we will create the
Convert Temperature
button on SalesTable
Form.
Go to SalesTable
->Designs->Design->ButtonGroup:ButtonsHeader
and right click to select new CommandButton
control on SalesTable
form.
Click on CommandButton to create new Command button
- Right click on new command button and click properties to open property window. Go to Text attribute and enter value as
ConverTemperature
.
Enter Convert Temperature -> Click on properties.
- Now we will create a new method clicked associated to
ConvertTemperature
button. This method we will create by right clicking on Methods under ConvertTemperature
button and then selecting Overide
method and then selecting Clicked.
Methods->Overide Method->Clicked
Select Clicked to override click method
- In the editor window, copy the following code:
void clicked()
{
Temperature tem = new Temperature();
;
tem.ConvertTemperature( element.salesTable().Temperature,
element.salesTable().Farenhite);
super();
}
The above code is creating the object of class Temperature
created in Axapta. Then we are calling ConvertTemperature
function defined in Temperature
class of Axapta with parameters as Temperature and Farenhite from SalesTable
in Axapta. Earlier in point 1 of Step C, we had created two new fields in SalesTable
of Axapta, i.e.:
- Temperature (Data type: real)
- Farenhite (Data Type: Enum with boolean)
- Save the Code and run the SalesTable Form. To run go to Account Receivable module of Axapta and then click Sales Order.
Step D
Click on Account receivable
Double click on Sales Orders under Account Receivable module of Axapta. Under Sales Order Grid, select any record and double click and go overview tab of Sales Order form. Scroll to find out the Temperature and Farenheit field.
First click on Sales Order
Enter Temperature as 59 and check the Farenhite Check. (If Farenhite is checked, then the temperature in Temperature field is in Fahrenheit and upon clicking Convert Temperature, command button will convert it to Celsius. If the Farenhite checkbox is not checked, then the temperature in Temperature field is in Celsius and upon clicking Convert Temperature command button will convert it to Fahrenheit).