GuruGroup logo
Microsoft Dynamics 365 Commerce Engineering

D365 Commerce POS: Add validation before adding an item to cart

Executive Summary & Key Architectural Takeaways
  • Commerce Runtime (CRT) Extensibility: Implementing server-side business rules that execute identically across Store Commerce, CPOS, and MPOS.
  • Pre-Trigger Interception: Hooking SaveCartVersionedDataRequest to validate inventory, item restrictions, or customer status before cart persistence.
  • Asynchronous Pipeline: Utilizing IRequestTriggerAsync to ensure point-of-sale responsiveness during real-time validation checks.
  • Local & Production Deployment: Deploying the compiled DLL to Retail Server extension directories and packaging via Retail SDK.
  • Clean Error Propagation: Throwing standard Commerce exceptions that display native alert dialogs directly on the cashier terminal.

Business Requirement & Problem Statement

In retail and omnichannel commerce operations, retailers frequently require custom validation checks before an item is added to an active transaction cart in POS.

Common Retail Scenarios:

  • Restricting age-regulated items unless customer identification is verified.
  • Validating whether an item belongs to a specific seasonal catalog or promotional assortment.
  • Preventing sales of discontinued, zero-inventory, or regionally restricted SKU variants.

“Implementing pre-validation in Commerce Runtime ensures business rules are enforced across all channels—whether cashiers ring up sales on Store Commerce terminals or customers buy online.”

— Aamir Khan, Power Platform & Dynamics Lead

Development: Implementing IRequestTriggerAsync

When an item is added to the cart, the Commerce Runtime triggers the SaveCartVersionedDataRequest. By implementing the IRequestTriggerAsync interface, we can intercept this request in the OnExecuting lifecycle hook before data is committed:

public class AddItemToCartValidationTrigger : IRequestTriggerAsync
{
    public IEnumerable<Type> SupportedRequestTypes
    {
        get
        {
            return new[] { typeof(SaveCartVersionedDataRequest) };
        }
    }

    public async Task OnExecuted(IRequestContext context, Request request, Response response)
    {
        await Task.CompletedTask;
    }

    public async Task OnExecuting(IRequestContext context, Request request)
    {
        ThrowIf.Null(request, "request");

        Type reqType = request.GetType();
        if (reqType == typeof(SaveCartVersionedDataRequest))
        {
            var cartRequest = (SaveCartVersionedDataRequest)request;
            Cart cart = cartRequest.Cart;

            // Execute custom validation logic here
            // Example: Verify item assortment, inventory limits, or customer loyalty
        }
        await Task.CompletedTask;
    }
}

Deployment & Retail Server Configuration

To deploy your compiled CRT extension library into the Commerce environment:

  1. Retail Server Deployment: Build your CRT extension project and copy the output assembly DLL into the Retail Server’s bin\ext directory.
  2. Local Debugging: For local development environments, place the extension DLL in:
    C:\Program Files (x86)\Microsoft Dynamics 365\70\Retail Server\user\bin\ext
  3. CommerceRuntime Configuration: Add the assembly reference into CommerceRuntime.ext.config:
    <add source="assembly" value="YourExtensionAssemblyName" />
  4. Package Deployment: Include your CRT project in your sealed Commerce SDK build pipeline to generate deployable packages for cloud environments.
Ready to Move Forward?

Turn Your Business Goals into Measurable Results

Partner with Guru Group to design, deliver, and scale secure technology solutions for your organization.