Skip to main content

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\\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\\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.

Popular posts from this blog

Dynamics Axapta: Sales Orders & Business Connector

Well, again folllowing my same idea of writting close to nothing and pasting code, I'll paste in some code to create a sales order from some basic data and the invoice it. I'll try to explain more in the future. AxaptaObject axSalesTable = ax.CreateAxaptaObject("AxSalesTable"); AxaptaRecord rcInventDim = ax.CreateAxaptaRecord("InventDim"); AxaptaRecord rcCustTable = ax.CreateAxaptaRecord("CustTable"); rcCustTable.ExecuteStmt("select * from %1 where %1.AccountNum == '" + MySalesOrderObject.CustAccount + "'"); if (MySalesOrderObject.CurrencyCode.Trim().Length == 0) MySalesOrderObject.CurrencyCode = rcCustTable.get_Field("Currency").ToString().Trim(); string sTaxGroup = rcCustTable.get_Field("taxgroup").ToString().Trim(); //set header level fields axSalesTable.Call("parmSalesName", MySalesOrderObject.SalesName.Trim()); axSalesTable.Call("parmCustAccount", M

Passing values between form and class

Class name is EmplDuplication and Form is EmplTable . void clicked() {    MenuFunction mf;    args args = new Args();    ;     args.record(EmplTable);     mf = new menufunction(identifierstr(EmplDuplication), MenuItemType::Action); mf.run(args); } Meanwhile, in the main() method of the EmplDuplication class, we need to put this Axapta x++ code to get the datasource: static void main(Args args) {     EmplDuplication EmplDuplication; EmplTable localEmplTable; ;     if(args.record().TableId == tablenum(EmplTable)) localEmplTable = args.record();     ... }