Microsoft Dynamics AX Support

This blog contains posts by the Microsoft Dynamics AX Support Teams Worldwide

Calling a web service from X++ code

This post describes how to call the AIF sales order service from X++ code.  The same approach can be used for calling any web service.

In the AOT under the References node add a service reference specifying the wsdl location of the service, a .NET code namespace and a reference namespace.  An example of a wsdl url is:

http://myMachine.myDomain/MicrosoftDynamicsAXAif50/salesorderservice.svc?wsdl

Once the reference has been created, in X++ create a new class that runs on the server.  In a method in the class write the code that calls the service.  An example of how to call the sales order service is below.  Once the code in the class compiles close the AX client.  Go to the Appl\Standard\ServiceReferences directory and copy the app.config and the generated assembly to the Server\bin directory.

    SalesOrder.SalesOrderServiceClient proxyClient;
    SalesOrder.AxdSalesOrder salesOrder;
    SalesOrder.AxdEntity_SalesTable salesTable;
    SalesOrder.AxdEntity_SalesLine salesLine;
    SalesOrder.AxdEntity_InventDim inventDim;
    SalesOrder.AxdEntity_InventDim[] inventDimArray;
    SalesOrder.AxdEntity_SalesLine[] salesLineArray;
    SalesOrder.AxdEntity_SalesTable[] salesTableArray;
    Exception ex;
    ;

    try
    {
       new InteropPermission(InteropKind::ClrInterop).assert();

        proxyClient = new SalesOrder.SalesOrderServiceClient();
        salesOrder = new SalesOrder.AxdSalesOrder();

        salesTable = new SalesOrder.AxdEntity_SalesTable();
        salesTable.set_CurrencyCode(“USD”);
        salesTable.set_CustAccount(“1101”);
        salesTable.set_DeliveryDate(str2date(“2/14/2010”, 0));
        salesTable.set_Payment(“N060”);
        salesTable.set_PurchOrderFormNum(“PO”);

        salesLine = new SalesOrder.AxdEntity_SalesLine();
        salesLine.set_ItemId(“1001”);
        salesLine.set_SalesQty(new System.Decimal(99));
        salesLine.set_SalesUnit(“ea”);

        inventDim = new SalesOrder.AxdEntity_InventDim();
        inventDim.set_configId(“HD”);
        inventDim.set_InventColorId(“01”);
        inventDim.set_InventSizeId(“42”);

        inventDimArray = new SalesOrder.AxdEntity_InventDim[1]();
        inventDimArray.SetValue(inventDim, 0);
        salesLine.set_InventDim(inventDimArray);

        salesLineArray = new SalesOrder.AxdEntity_SalesLine[1]();
        salesLineArray.SetValue(salesLine, 0);
        salesTable.set_SalesLine(salesLineArray);

        salesTableArray = new SalesOrder.AxdEntity_SalesTable[1]();
        salesTableArray.SetValue(salesTable, 0);
        salesOrder.set_SalesTable(salesTableArray);

        proxyClient.create(salesOrder);

        CodeAccessPermission::revertAssert();
       
        info(“Sales order successfully created.”);

    }

    catch (Exception::CLRError)
    {
        throw error(AifUtil::getClrErrorMessage());
    }

 

More Information

  • If you need to use a type that is not an AX type like System.Decimal you will have to call the assert method on the InteropPermission class before instantiating the type. 
  • In the Intellisense for the code editor arrays are not listed, only the singular instances of the types are displayed.  However, you can still add the arrays to your code and they will compile.  When you populate the arrays do it using syntax similar to the code example in this article where the array is first declared and the SetValue method is called to set the value and index for the entry in the array. 
  • If the assembly in the Application\Appl\<InstanceName>\ServiceReferences directory is not copied to the Server\bin directory you will get missing reference errors thrown at runtime when instantiating the arrays. 
  • If the app.config in the Application\Appl\<InstanceName>\ServiceReferences directory is not copied to the Server\bin directory CLRObject could not be created errors are thrown when trying to instantiate the first non-service client class so in the example in this article the line salesOrder = new SalesOrder.AxdSalesOrder() would throw a CLRObject exception.