Showing posts with label Integration. Show all posts
Showing posts with label Integration. Show all posts

Walkthrough: Adding an X++ Object to a Visual Studio Project [AX 2012]

This walkthrough illustrates the following tasks:

  • Creating the class library, adding a Microsoft Dynamics AX table to the project, and accessing that table from code.

  • Creating a console application project and testing the generated assembly.

TipTip: Although this topic shows you how to test the generated assembly from Visual Studio, you can also test it from X++ because after you add the project to the AOT, the classes in the assembly are available from Microsoft Dynamics AX.

Prerequisites

To complete this walkthrough you will need:

  • Microsoft Dynamics AX with Visual Studio Tools and sample data installed

  • Visual Studio 2010

Creating the Class Library

The first step is to create the class library in Visual Studio. After you have created the class library project, you can then add a Microsoft Dynamics AX table to the project. Then you can create class methods that access that table.

To create the class library
  1. To create a new class library project, click File > New> Project.

  2. Below the Installed Templates tree, click Visual C# > Windows, select the Class Library project type and then click OK.

  3. Save the new project.

  4. Add the project to the AOT by clicking File > Add ClassLibrary1 to AOT. Notice that the project icon changes in Solution Explorer. Alternatively, you can right-click the ClassLibrary1 project and select Add ClassLibrary1 to AOT.

To add a Microsoft Dynamics AX table to the project
  1. Open the Application Explorer by clicking View > Application Explorer. Expand the Data Dictionary > Tables node and locate the CustTable table.

  2. Click the CustTable table and drag it onto the project in Solution Explorer. Alternatively, you can right-click the table and then click Add to Project. In Solution Explorer, you can see that the table and a proxy to the CustTable table are created internally by the system. In the References node you can see a reference to the assembly Microsoft.Dynamics.Ax.ManagedInterop.

    TipTip: You can add a system table or system class to your Visual Studio project even if the  interface does not support the dragging of system objects. For example, to add the FormRun system class, first add any application class, such as the Bank class. Then rename the new proxy node from Class.Bank.axproxy to Class.FormRun.axproxy.

To create methods that use the table
  • Open the Class1.cs file and add the following code. This code contains two methods that each take two parameters and return data for the specified customer.

    C#

    using System;

    namespace ClassLibrary1
    {
    public class Class1
    {
    public string GetCustomerPaymentMode(string accountNum, string dataAreaId)
    {

    string paymentMode = String.Empty;
    CustTable custTable = new CustTable();

    // Search for the customer.
    custTable = CustTable.findByCompany(dataAreaId, accountNum);

    if (custTable.Found)
    {
    // Get the value for the customer's payment mode.
    paymentMode = custTable.PaymMode;
    }

    return paymentMode;
    }

    public bool GetCustomerCreditLimit(string accountNum, string dataAreaId)
    {

    bool hasCreditLimit = false;
    CustTable custTable = new CustTable();

    // Search for the customer.
    custTable = CustTable.findByCompany(dataAreaId, accountNum);

    if (custTable.Found)
    {
    // Get the value for whether the customer has a credit limit.
    hasCreditLimit = (custTable.MandatoryCreditLimit == NoYes.No ? false : true);
    }

    return hasCreditLimit;
    }
    }
    }

Creating a Console Application Project


To create a console application project to test the assembly



  1. In Solution Explorer, right-click the solution ClassLibrary1 and select Add > New Project.



  2. Below the Installed Templates tree, click Visual C# > Windows, select the Console Application project type and then click OK.



  3. In Solution Explorer, add a reference to the ClassLibrary1 project by right-clicking the References node under the ConsoleApplication1 project and then clicking Add Reference.



  4. Click the Projects tab, select the ClassLibrary1 project and then click OK.



  5. In Solution Explorer, add a reference to the managed interop assembly by right-clicking the References node under the ConsoleApplication1 project and then clicking Add Reference.



  6. Click the Browse tab, locate the Microsoft.Dynamics.AX.ManagedInterop assembly and select OK. This assembly is located in the Client\Bin directory. For example, C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin. This reference is necessary to use the Session object.


To call methods to display output to the console application



  1. Open the Program.cs file and add the following code. This code calls the GetCustomerPaymentMode and the GetCustomerCreditLimit methods from the class you created and displays the values in the console window.

    C#

    using System;
    using ClassLibrary1;
    using Microsoft.Dynamics.AX.ManagedInterop;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    // Create a session
    using (Session session = new Session())
    {
    session.Logon(null, null, null, null);

    Class1 class1 = new Class1();
    string accountNum = "3003";
    string dataAreaId = "ceu";
    string paymentMode = String.Empty;
    string hasCreditLimitText = String.Empty;
    bool hasCreditLimit;

    // Get the customer payment mode and credit limit.
    paymentMode = class1.GetCustomerPaymentMode(accountNum, dataAreaId);
    hasCreditLimit = class1.GetCustomerCreditLimit(accountNum, dataAreaId);
    hasCreditLimitText = (hasCreditLimit == false ? " and does not have" : " and has");

    // Write the data to the console
    Console.WriteLine("Customer " + accountNum + " in company " + dataAreaId +
    " has a payment mode of " + paymentMode + hasCreditLimitText +
    " a mandatory credit limit." );
    Console.ReadLine();
    }

    }
    }
    }


  2. In Solution Explorer, set the console application to be the startup project by right-clicking ConsoleApplication1 and then clicking Set as StartUp Project.


To test the generated assembly



  • Run the application. If you have the sample data installed, you will see the following output in the console window:

    Customer 3003 in company ceu has a payment mode of CHCK and does not have a mandatory credit limit.

    TipTip: You may receive an error about the .NET target framework because both projects target the .NET Framework 4 by default but the Microsoft.Dynamics.AX.ManagedInterop assembly is a mixed-mode assembly that targets the 2.0 runtime.

    To resolve this error you have two options: you can change the target framework for both projects to .NET Framework 3.5 or you can add an attribute to the app.config file of the console application project. To change the target framework, right-click each project in Solution Explorer and then click Properties. In the Target framework field, select .NET Framework 3.5.

    If you want the console application project to target the .NET Framework 4, add the useLegacyV2RuntimeActivationPolicy attribute to the startup element in the app.config file and set it to true. For example, <startup useLegacyV2RuntimeActivationPolicy="true">.

Common Keys Steps for The Planning integration process Between AX and Others Application

1. In a typical integration scenario, users who have business expertise first determine the document exchange needs. These are requirements from a business perspective. The business users work with the Implementation team to specify:
a. What data is to be exchanged.
b. Any business logic related to that data.
c. The external systems with which data is to be exchanged.
d. The conditions under which data is sent from or received by Microsoft Dynamics AX.


2. The partner or system implementer works with the customer and their IT staff to determine the hardware and software requirements for AIF. They analyze the existing environment and recommend any new hardware or software that must be installed.


3. The customer's IT staff installs and configures any required hardware and software to support AIF.

4. The partner or customer developer programs the document exchange. They can make customizations to the AIF documents or create new documents to meet the requirements of the business users. How AIF is configured depends in part on the network environment. Therefore, the developer may work with IT staff when implementing an integration scenario.

5. IT staff monitors the document exchanges and troubleshoot any errors that are generated.

How AX 2012 Integrate with Other Applications

You can use the following methods to integrate Microsoft Dynamics AX with other applications:

1- Services and the Application Integration Framework

Services are the preferred option for integration with Microsoft Dynamics AX. Services in Microsoft Dynamics AX are used to expose its functionality through WCF-based services. Microsoft Dynamics AX code and external applications can consume Microsoft Dynamics AX services. AIF supports the processing of inbound and outbound messages such as message transforms and value lookups. Together, services and AIF provide the programming model, tools, and infrastructure support for XML-based integration with external applications and data

 2- .NET interop to X++

You can use the .NET interop to X++ feature to call X++ code using C# or another managed language. A proxy is an automatically generated .NET class, in C# or another managed language, that mimics an X++ class of Microsoft Dynamics AX.

3- .NET interop from X++

The .NET interop from X++ (also known as the CLR Interop in the previous release) provides interoperability with external .NET components and you can execute managed-code components from within X++ code. .NET interop from X++ is useful when you want your X++ code to access functionality provided by a CLR-managed assembly. This includes assemblies that are installed with the .NET Framework and any assemblies that you create with a language such as C# or Visual Basic.NET.

4- Consume external web services

You can use the Microsoft Dynamics AX programming model to consume external web services from within X++ code. To consume an external web service from X++, you must first create a reference to the web service. After creating a reference to the web service, you can invoke it from X++
and view the available methods with IntelliSense. Calling and managing external web services is done completely within Microsoft Dynamics AX.

The following figure shows how the AIF integration components interact with Microsoft Dynamics AX through the AOS.

image