Thursday, 25 April 2013

Integrate Microsoft Dynamics Axapta with Temperature Conversion C# Application



Integrate Microsoft Dynamics Axapta with Temperature Conversion C# Application

By abhishek pareek2009, 25 Sep 2010

 
   4.93 (7 votes)


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:

  1. CvrtC2Frnt.dll
  2. 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:

 Collapse | Copy Code

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:

  1. Text Box of name txtSourceTmp
  2. Text Box of name txtDestTmp
  3. Label Box of name lblsrcTemp with blank value in Text property
  4. Label Box of name lbldesTemp with blank value in Text property
  5. 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.
image001.jpg
Step C

Now, create the following functions of class ShowTemperature:

 Collapse | Copy Code

public ShowTemperature(double Temperature, Boolean Fahrenheit)
        {
            InitializeComponent();
            txtSourceTmp.Text = Temperature.ToString();
            ConvertTemp.sourceTemp = Temperature;
            ConvertTemp.Faren = Fahrenheit;
          
        }
 
//Above code clearly specifies that we are initializing the label and text
//box with vales coming from user input.
 
       
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));
        }
 
// above code is triggered when Convert button is click to convert the temperature.

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";
            }
        }
// above  code specifies that form ShowTemperature is displayed dynamically
// by label values depending
// upon the type of conversion is selected from Axapta or any other application.

Step D

Now, create the function ShowResult in class ConverTemp as follows:

 Collapse | Copy Code

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:
image002.jpg
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:

 Collapse | Copy Code

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.
image004.jpg
Click on this to get the UI for converting Fahrenheit to Celsius.
image006.jpg
Click on Convert button to convert Fahrenheit to Celsius
image007.jpg
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

  1. 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.
  2. 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
  3. 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
  4. 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
  5. Click on Ok to add the reference.
    Click on Ok to add reference.
image016.jpg
Step B

  1. 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.
  2. 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
  3. 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
  4. Now go to Class Declaration which is under and click Edit to open it.
    Click on Edit to edit class declaration of ConvertTemp Class
  5. Write the following code in class declaration:
     Collapse | Copy Code
    class Temperature
    {
    CvrtC2Frnt.ConvertTemp ct;
    }
    The above code is creating the object for ConvertTemp class which is defined in CvrtC2Frnt DLL.
  6. 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.
  7. 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:
     Collapse | Copy Code
    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.
  8. 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

  1. We will create two new fields in SalesTable of Axapta:

  1. Temperature
  2. Farenhite

  1. 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.
  2. 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.
  3. 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.
  4. Now, we will create the Convert Temperature button on SalesTable Form.
    Go to 
    SalesTable->Designs->Design->ButtonGroup:ButtonsHeader and right click to select newCommandButton control on SalesTable form.
    Click on CommandButton to create new Command button
  5. 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.
  6. 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
  7. In the editor window, copy the following code:
     Collapse | Copy 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 callingConvertTemperature 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.
image051.jpg
First click on Sales Order
image056.jpg
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).

  • Click on Convert Temperature Command button on sales order form.
    Click on ConvertTemperature Button
  • Click on Convert Button of Convert Temperature Form.
    You got the temperature in Celsius!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

 

No comments:

Post a Comment