Extend Data Exchange - Real Time Service

How to Extend Real Time Service in D365 Commerce: Data Exchange from POS to HQ with real time extension

This blog has complete guide that how we can extend our real time service or data sync with our custom logic.
RetailTransactionServiceEx is a class that behaves like a service between POS and HQ. We have to create a class extension where we can implement our custom logic for real time data sync.

Here's an example Create a customer in POS and in real time class filling a custom field will reflect in HQ.


Create Service Class Extension in D365 FinOps

  • Create a project in D365 FinOps and write a class extension with name 'ContosoRetailTransactionServiceSample_Extension'.
  • Write a code to fill the custom field in Custtable.


[ExtensionOf(classStr(RetailTransactionServiceEx))]
final class ContosoRetailTransactionServiceSample_Extension
{
    public static container Hello(AccountNum accountNumber)
    {
        CustTable custTable;
        container result = [false, ''];
    
        if (accountNumber)
        {
            custTable = CustTable::find(accountNumber, true);
            if (custTable)
            { 
                ttsbegin;
                custTable.CustomField = 'RealTime';
                custTable.update();
                ttscommit;
                
                result = [true, 'Success!', strFmt("Hello %1 !", custTable.AccountNum)];
            }
            else
            {
                result = [false, 'Customer not found'];
            }
        }
        else
        {
            result = [false, 'AccountNumber is null.'];
        }
    
        return result;
    }

}

  • Build the project with class. The extension class has been deployed on the service.

Call the Custom Method in CRT(Commerce Runtime) Project 

  • Now we have to call the custom method that we have written in our Extension class 'ContosoRetailTransactionServiceSample_Extension'.
  • Open the CRT solution from K:\RetailSDK\SampleExtensions\CommerceRuntime/CommerceRuntimeSamples.sln
  • Find the project named 'Runtime.Extensions.NonTransactionalLoyaltyPointsSample'
  • Create a class(trigger) with name 'CustomerServiceTrigger' in the project.
  • Write the code in the created trigger.

using System;
using System.Collections.Generic;
using System.Text;

namespace Contoso.Commerce.Runtime.NonTransactionalLoyaltyPointsSample
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Threading.Tasks;
    using Microsoft.Dynamics.Commerce.Runtime.DataServices.Messages;
    using Microsoft.Dynamics.Commerce.Runtime;
    using Microsoft.Dynamics.Commerce.Runtime.Messages;
    using Microsoft.Dynamics.Commerce.Runtime.RealtimeServices.Messages;
    using Microsoft.Dynamics.Commerce.Runtime.Framework.Exceptions;
    using Microsoft.Dynamics.Commerce.Runtime.DataModel;
    using System.Runtime.Serialization;
    using Contoso.Commerce.Runtime.DocumentProvider.EpsonFP90IIISample.DocumentBuilders;


    class CustomerServiceTrigger : IRequestTriggerAsync
    {
        /// <summary>
        /// Gets the supported requests for this trigger.
        /// </summary>

        public IEnumerable<Type> SupportedRequestTypes
        {
            get
            {
                return new[] { typeof(NewCustomerRealtimeRequest) };
            }
        }

        /// <summary>
        /// Post trigger code to post loyalty reward points for non-transactional activity.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="response">The response.</param>

        public async Task OnExecuted(Request request, Response response)
        {
            try
            {
                var SearchCustomersRequest  = (NewCustomerRealtimeRequest)request;
                var customer = (Customer)SearchCustomersRequest.CustomerToSave;                

                InvokeExtensionMethodRealtimeRequest extensionRequest = new InvokeExtensionMethodRealtimeRequest("Hello", customer.AccountNumber);
                InvokeExtensionMethodRealtimeResponse response1 = await request.RequestContext.ExecuteAsync<InvokeExtensionMethodRealtimeResponse>(extensionRequest).ConfigureAwait(false);
                ReadOnlyCollection<object> results = response1.Result;


            }
            catch (HeadquarterTransactionServiceException exception)
            {
            }
        }
        /// <summary>
        /// Pre trigger code.
        /// </summary>
        /// <param name="request">The request.</param>

        public async Task OnExecuting(Request request)
        {
            // It's only stub to handle async signature 
            await Task.CompletedTask;
        }
    }
}


  • Build the project.
  • Copy the .dll of the project from K:\RetailSDK\SampleExtensions\CommerceRuntime\Extensions.NonTransactionalLoyaltyPointsSample\bin\Debug\netstandard2.0\Contoso.Commerce.Runtime.NonTransactionalLoyaltyPointsSample.dll
  • Paste the copied .dll to K:\RetailServer\WebRoot\bin\Ext
  • Add a line of code in CommerceRuntime.Ext.config file in K:\RetailServer\WebRoot\bin\Ext

Code:

<add source="type" value="Contoso.Commerce.Runtime.NonTransactionalLoyaltyPointsSample.CustomerServiceTrigger, Contoso.Commerce.Runtime.NonTransactionalLoyaltyPointsSample" />
 
  • Restart the IIS service and here you have done the task.


Test Our Work



  • Save the newly created customer.


  • Customer with 'ID = 004621' has been created.
  • Now let's check the customer in HQ.
  • Open the All Customers form or browse the Custtable.
  • Filter the Customer's AccountNumber (004621).
  • Check the Custom field of the newly created customer on HQ.

  • We can see that our custom field has been filled on real time.











Comments