Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
The Retail Interest Group by Dynamics 365 Commerce has moved from Yammer to Viva Engage. If you don't have access to the new Viva Engage community, fill out this form (https://aka.ms/JoinD365commerceVivaEngageCommunity) to be added and stay engaged in the latest discussions.
This article explains how you can use a trigger to block an invoice or credit transaction in Microsoft Dynamics 365 Commerce.
To block an invoice or credit transaction, follow these steps:
Open Visual Studio as an administrator. Create a new Visual C# Class Library (Portable) project and name it CRTTriggerExtension. If you get a message that the selection makes this project incompatible with Visual Studio 2010, select OK.
In Solution Explorer, rename default class1.cs to GetCustomersServiceRequestTrigger.cs.
Right-click the Reference node in the project and add the following references. The location of the references depends on the deployment topology.
- Microsoft.Dynamics.Commerce.Runtime.Entities.dll
- Microsoft.Dynamics.Commerce.Runtime.Framework.dll
- Microsoft.Dynamics.Commerce.Runtime.Services.Messages.dll
Add the following using statement to the GetCustomersServiceRequestTrigger.cs file.
using Microsoft.Dynamics.Commerce.Runtime.Messages; using Microsoft.Dynamics.Commerce.Runtime.Services.Messages; using Microsoft.Dynamics.Commerce.Runtime;Rename class1.cs in the code to GetCustomersServiceRequestTrigger and then add the IRequestTrigger interface declaration.
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Dynamics.Commerce.Runtime; using Microsoft.Dynamics.Commerce.Runtime.Services.Messages; using Microsoft.Dynamics.Commerce.Runtime.Messages; namespace CRTTriggerExtension public class GetCustomersServiceRequestTrigger : IRequestTriggerImplement the IRequestTrigger interface trigger. Right-click the IRequestTrigger class, select Quick Actions, and then select Implement Interface. Visual Studio implements the interface. You can also place the cursor on IRequestTrigger, press Ctrl+, and select Implement Interface.
The empty interface members SupportedRequestTypes, OnExecuted, and OnExecuting methods are shown in the following code example.
public class GetCustomersServiceRequestTrigger : IRequestTrigger { public IEnumerable<Type> SupportedRequestTypes { get { throw new NotImplementedException(); } } public void OnExecuted(Request request, Response response) { throw new NotImplementedException(); } public void OnExecuting(Request request) { throw new NotImplementedException(); } }Commerce Scale Unit uses the GetCustomersServiceRequest object to get the customer details from Commerce Runtime (CRT) and uses the GetCustomersServiceRequest object to add the customer to the transaction. Before adding the customer to the transaction, you need to check whether the customer is blocked. To check whether the customer is blocked, implement a post trigger for this request and check whether the customer is blocked. If the customer is blocked, throw the exception to the Store Commerce app.
In the SupportedRequestTypes method, tell the CRT that you're going to add the trigger for GetCustomersServiceRequest. The following code example shows how to add GetCustomersServiceRequest as a supported type.
public IEnumerable<Type> SupportedRequestTypes { get { return new[] { typeof(GetCustomersServiceRequest) }; } }Check if the customer is blocked in the OnExecuted (post trigger) method with the following code.
public void OnExecuted(Request request, Response response) { if (response == null) { throw new ArgumentNullException("request"); } var getCustomersServiceResponse = (GetCustomersServiceResponse)response; if(getCustomersServiceResponse.Customers.FirstOrDefault().Blocked == true) { string message = string.Format("Failed to add customer '{0}' to cart. Blocked customers are not allowed for transactions.", getCustomersServiceResponse.Customers.FirstOrDefault().AccountNumber); throw new CartValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_CustomerAccountIsBlocked, message); } }Finally, update the OnExecuting method with the following code.
public void OnExecuting(Request request) { if (request == null) { throw new ArgumentNullException("request"); } }Select Save.