how to add extension methods to commerce data exchange real-time service
DESCRIPTION
How to Add Extension Methods to Commerce Data Exchange Real-Time ServiceTRANSCRIPT
![Page 1: How to Add Extension Methods to Commerce Data Exchange Real-Time Service](https://reader035.vdocument.in/reader035/viewer/2022073120/55cf9a2e550346d033a0c17e/html5/thumbnails/1.jpg)
AX Object ServerCommerce Data
Exchange: Real-time Service
Client
AX Database
Client
Client
Client
How to Add Extension Methods to Commerce Data Exchange: Real-time Service
Commerce Data Exchange: Real-time Service OverviewCommerce Data Exchange: Real-time Service enables the retail clients, from POS to SharePoint StoreFront, to interact with AX retail functionalities in real-time.
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 OverviewIn 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.
![Page 2: How to Add Extension Methods to Commerce Data Exchange Real-Time Service](https://reader035.vdocument.in/reader035/viewer/2022073120/55cf9a2e550346d033a0c17e/html5/thumbnails/2.jpg)
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 AXPre-requisites:
Dynamics AX 2012 R2 installed. Developer license for Dynamics AX 2012 R2.
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:
X++ type CLR type
boolean System.Boolean
date System.DateTime
int System.Int32
int64 System.Int64
str System.String
guid System.Guid
Real System.Single or System.Double
Example:
![Page 3: How to Add Extension Methods to Commerce Data Exchange Real-Time Service](https://reader035.vdocument.in/reader035/viewer/2022073120/55cf9a2e550346d033a0c17e/html5/thumbnails/3.jpg)
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;}
Consuming the Extension Methods from CRT ClientConsuming the Real-time Service extension methods from CRT client is also very straightforward. It is very similar to consuming the normal Real-time Service methods. The only difference is that instead of calling InvokeMethod, we need to call InvokeExtensionMethod.
Here is an example of calling the Hello methods we showed as example in the previous section:
public void HelloCustomer(string accountNumber) { try { var response = this.CommerceRuntime.TransactionService.InvokeExtensionMethod("Hello", "2014"); if (response.Count == 1) { Console.WriteLine(response[0] as string); } } catch (CommunicationException ex) { Console.WriteLine("Request failed: {0}", ex.Message); } }
![Page 4: How to Add Extension Methods to Commerce Data Exchange Real-Time Service](https://reader035.vdocument.in/reader035/viewer/2022073120/55cf9a2e550346d033a0c17e/html5/thumbnails/4.jpg)
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.