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

What does this mean: "The form datasource query object does not support changing its AllowCrossCompany property after the form has executed the query."?

I have made a form with datasources vendtable and vendtrans. Inside vendtable_ds.executequery() looks like this: QueryBuildDataSource queryBuildDatasource ,queryBDS_VendTrans_Invoice; ; queryBuildDatasource = this.query().dataSourceTable(tablenum(vendtable)); queryBDS_VendTrans_Invoice = this.query().dataSourceTable(tablenum(vendtrans)); if (curext() == "MASTERCOMP") { this.query().allowCrossCompany(true); } else { this.query().allowCrossCompany(false); } //FilterVendorName = stringedit control on form if (FilterVendorName.text()) { queryBuildDatasource.addRange(fieldNum(VendTable,Name)).value(strfmt("*%1*", FilterVendorName.text())); } else { queryBuildDatasource.clearRange(fieldNum(VendTable,Name)); } //FilterInvoiceNumber = stringedit control on form if (FilterInvoiceNumber.valueStr() == "") { queryBDS_VendTrans_Invoice.enabled(false); } else { queryBDS_VendTrans_Invoice.enabled(true); queryBDS_VendTrans_In...

Credit Note [Dynamics AX] using X++

This post will help to create credit note for a sales order based on the invent lot id. All the invoices raised for a particular sales line – Lot Id will be raised back as a credit note. Information on Credit Note: A credit note or credit memorandum (memo) is a commercial document issued by a seller to a buyer. The seller usually issues a Credit Memo for the same or lower amount than the invoice, and then repays the money to the buyer or sets it off against a balance due from other transactions Below Code will help to create credit note for all the invoices raised against the sales line -lot id. Please note: This code can be customized as per your requirements. This is just a template to help creating credit note using X++ code. Please test the code before use. static void SR_CreateCreditNote_Sales(Args _args) { // Coded by Sreenath Reddy CustInvoiceTrans custInvoiceTrans; Dialog dialog = new Dialog(“Create credit note – for sales.”); DialogField dfInv...