Export (0) Print
Expand All

How to: Call an AIF Web Service from C# to Create a Sales Order

Dynamics AX 2009

This topic describes how to call the sales order Web service that is exposed by Application Integration Framework (AIF) and create a sales order.

The following C# code calls the AIF Web service SalesOrderService from a Visual Studio console application. The code contains a Main method that calls the SalesOrderService.create method. This is similar to the create service that can be listed in the <Action> tag in the XML representation of an inbound message for AIF.  This code contains no header information, so at runtime AIF uses the current user as the source endpoint user and the default endpoint that is created when AIF is installed.

To complete this walkthrough, you will need:

  • Microsoft Dynamics AX with AIF installed

  • Visual Studio 2008

To Create the Sales Order

  1. In Microsoft Dynamics AX, configure AIF by following the steps in the section “Prepare the AIF Environment” in How to: Call an AIF Web Service from C#.

  2. In Visual Studio, create the console application and add a service reference to the sales order service by following the steps in the section “Prepare the Visual Studio Environment” in How to: Call an AIF Web Service from C#.

  3. Replace the code in the Program.cs file with the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ConsumeSalesOrderService.SalesOrder;
    // Add a using statement for the service reference.
    using System.Collections; 
    
    namespace ConsumeSalesOrderService
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Instantiate an instance of the service client class.
                SalesOrderServiceClient proxy = new SalesOrderServiceClient();
    
                // Create an instance of the document class.
                AxdSalesOrder salesOrder = new AxdSalesOrder();
    
                // Create instances of the entities that are used in the service and
                // set the needed fields on those entities.
                AxdEntity_SalesTable salesTable = new AxdEntity_SalesTable();
                salesTable.CurrencyCode = "USD";
                salesTable.CustAccount = "1101";
                salesTable.DeliveryDate = Convert.ToDateTime("2/14/2010");
                salesTable.Payment = "N060";
                salesTable.PurchOrderFormNum = "PO";
    
                AxdEntity_SalesLine salesLine = new AxdEntity_SalesLine();
                salesLine.ItemId = "1001";
                salesLine.SalesQty = 88;
                salesLine.SalesUnit = "ea";
    
                AxdEntity_InventDim inventDim = new AxdEntity_InventDim();
                inventDim.configId = "HD";
                inventDim.InventColorId = "01";
                inventDim.InventSizeId = "42";
    
                // Add the sub-entity instances to their parent entities as an array
                // of the sub-entity type.
                salesLine.InventDim = new AxdEntity_InventDim[1] { inventDim };
                salesTable.SalesLine = new AxdEntity_SalesLine[1] { salesLine };
                salesOrder.SalesTable = new AxdEntity_SalesTable[1] { salesTable };
    
                try
                {
                    // Call the create method on the service passing in the document.
                    EntityKey[] returnedSalesOrderEntityKey = proxy.create(salesOrder);
    
                    // The create method returns an EntityKey which contains the ID of the sales order.
                    EntityKey returnedSalesOrder = (EntityKey)returnedSalesOrderEntityKey.GetValue(0);
                    Console.WriteLine("The sales order created has a Sales ID of " + returnedSalesOrder.KeyData[0].Value);
                    Console.ReadLine();
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Console.ReadLine();
                }
            }
        }
    }
    
  4. In Solution Explorer, right-click the project and select Build.

Run the code and then, to view the new sales order, click Accounts Receivable > Places > Sales Orders.

Community Additions

ADD
Show:
© 2014 Microsoft