How To Add Extension Methods To Commerce Data Exchange Real-Time Service
How To Add Extension Methods To Commerce Data Exchange Real-Time Service
AX Object Server
Client
The Real-time Service by default allows the client to perform the following operations by default (not limited to the list): Issue/query/use gift card Query/update/use loyalty points Issue/query/use credit memo Create/query/update customer information Create/query/retrieve customer orders, sales orders, transfer orders etc. Query/retrieve existing transactions Return transactions/items Remote staff log on/ log off, clock in/ clock out
Extension Overview
In addition to the default functionality that ships with the product, the Real-time Service also allows customers and partners to extend its functionality by adding extension methods. These methods can be invoked from the client side to perform any custom business logic.
Note that in order for the extension methods to work, the Real-time Service must be installed and configured correctly. Please see the documentation on Install and configure Commerce Data Exchange: Real-time Service <Add link> for details on how to do that. The rest of the document will describe how to add extension methods in AX, and how to consume the extension methods from a CRT client.
How to add extension methods in AX: Adding extension methods in AX is very straightforward. You just need to add a new method in the RetailTransactionServiceEx class in the AOT. There are a few requirements for this method so that it can be invoked through the Real-time Service: The method must be a public static method. The return type must be a container of length 2 or longer. The first two elements are reserved for a Boolean indicating success/failure of the method call, and a string that can be used for comment or error message. The other items in the returned container can be of any type, including nested containers. The parameters for the method must be of primitive types in AX. Only the following types are supported due to BC.NET limitations:
CLR type
System.Boolean System.DateTime System.Int32 System.Int64 System.String System.Guid System.Single or System.Double
X++ type
boolean date int int64 str guid Real
Example: We would like to add a method that accepts a customer account number, and returns a greeting message with the customer name. For example, say we have customer Sandy whose account number is 1001, the method would return Hello Sandy! The AX side implementation is to add the following method in RetailTransactionServiceEx class:
public static container Hello(AccountNum accountNumber) { CustTable custTable; DirPartyTable dirPartyTable; container result = [false, '']; if (accountNumber) { select firstOnly Name from dirPartyTable exists join custTable where custTable.accountNum == accountNumber && dirPartyTable.RecId == CustTable.Party; if (dirPartyTable) { result = [true, 'Success!', strFmt("Hello %1 !", dirPartyTable.Name)]; } else { result = [false, 'Customer not found']; } } else { result = [false, 'accountNumber is null.']; } return result; }
} }
Note that the response successful/comments have been abstracted away. If the request failed (false on the first container results), the service client will throw a communication exception. Only the additional response data is returned.