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

Mark All for Open Cust Trans and Open Vend Trans

We have situations where there are lots of open transactions that need to be settled against each other. This can be the case if auto settlement is turned off. One solution is to add a "Mark All" button to the custOpenTrans or vendOpenTrans forms. This button "checks" the mark checkbox on every line. The user can then uncheck several lines if needed and Update to settle the lines. The code below is an example of what we used on the open vendor transaction screen. The code is very similar on the AR side. One note: I used vendTable.AccountNum in the code below. That should be generalized to work with any buffer that is passed into the open trans form. void customMarkAll() { VendTransOpen localVendTransOpen; VendTrans localVendTrans; container conSum; int linesProcessed; ; //show wait cursor startLengthyOperation(); element.lock(); //remove all prior markings specOffsetVoucher.deleteSpe...

Print Report in Microsoft Dynamics AX 2009 through X++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following code: void clicked() {     Args                args;     ReportRun           reportRun;     SalesFormLetter     salesFormLetter;     PrintJobSettings    printJobSettings;     CustConfirmJour     custConfirmJour;     RecordSortedList    list                = new RecordSortedList(55);     SalesTable          salesTableUpdate;     ;     SELEC...

Creating Free Text Invoice through X++ code

Job: Calling class from job to run the class public void freeTextInvoicePostTestJob() { Dialog dialog; DialogField dlgCustAcc; DialogGroup dialogPeriodLengthGroup, dialogPeriodLengthGroup1; DialogField dlgLedgerAcc; ; dialog = new Dialog("Free-Text Invoice"); dialogPeriodLengthGroup1 = dialog.addGroup('Cust Table'); dlgCustAcc = dialog.addField(typeid(CustAccount)); dialogPeriodLengthGroup = dialog.addGroup('Ledger Table'); dlgLedgerAcc = dialog.addField(typeid(LedgerAccount)); if(dialog.run()) { if(dlgCustAcc.value() && dlgLedgerAcc.value() != '') FreeTxtInvoiceCreatePost::main(dlgCustAcc.value(), dlgLedgerAcc.value()); else throw error(strfmt("Either CustAccount or LedgerAccount info is missing.")); } } Class: Which creates the free text invoice class FreeTxtInvoiceCreatePost { } static void main(CustAccount _custAccount, LedgerAccount _ledgerAccount) { CustInvoiceTable custInvoiceTable; ...