Validation Callback Class
Validation Callback provides a mechanism to implement custom validations on the line items in the cart. Validations are any constraints you define on the line item to restrict the selection of that line item based on any condition or criteria. When you define validations, CPQ checks if the condition or the criteria is satisfied and displays an error message accordingly. For example, you can validate if the Quantity field for any product is negative and prevent the sales rep from finalizing the cart.
IsEnabled property for a custom action, the UI now updates the button's visibility in real time as you change attribute values, without requiring you to navigate away and return to the page.Validation Callback applies validation on the following aspects in the configuration:
- Entire cart
- Line items in the cart
- Price Ramps
To use the Validation Callback you must create a custom C# that implements the IValidationCallback interface and register the custom C# with Validation Callback Class. You must write your custom logic in the custom C# language.
The following methods are available in the IValidationCallback interface:
Method | Signature | Description |
|---|---|---|
BeforePricingValidationAsync | public Task BeforePricingValidationAsync(IActionRequest actionRequest, CancellationToken cancellationToken); | This extension point is utilized for creating pre-price validation callbacks. |
FinalizeValidationAsync | public Task FinalizeValidationAsync(IActionRequest actionRequest, CancellationToken cancellationToken); | This hook point is used to write validation callback code for finalize event. |
OnCartValidationAsync | public Task OnCartValidationAsync(IActionRequest actionRequest, CancellationToken cancellationToken);OnCartValidationAsync(IActionRequest actionRequest) | This is generic method for validation callback. |
Usecases
Following are some examples of Validation Callback methods: BeforePricingValidationAsync, OnCartValidationAsync, and FinalizeValidationAsync.
CancellationToken has been introduced in custom code developed within the Conga Advantage Platform. If you are still using the older approach, you must mark those legacy methods as obsolete. This means applying the [Obsolete] attribute to methods that do not support tokens. For example: [Obsolete("Use version with CancellationToken")]. For more information, see CancellationToken Implementation Guidelines.
BeforePricingValidationAsync
This method is used to write pre-pricing validation callback logic. In the below example the code sets the validation error when all cart line status are marked cancelled, then sets validation error as All lines are cancelled. Cannot proceed with pricing so that cart is not processed further for pricing.
public async Task BeforePricingValidationAsync(IActionRequest request, CancellationToken cancellationToken)
{
ThrowIfCancellationRequested(cancellationToken);
var logHelper = GetLogHelper();
try
{
logHelper?.LogInformation("ValidationCallback.BeforePricingValidationAsync Started");
var cartLines = request.GetCartContext().GetLineItems().SelectMany(x => x.GetChargeLines()).ToList();
if (cartLines.Where(x => x.IsCancelledLine()).Count() == cartLines.Count)
{
cartLines.First().SetValidationError("All lines are cancelled. Cannot proceed with pricing.");
}
logHelper?.LogInformation("ValidationCallback.BeforePricingValidationAsync Ended");
}
catch (Exception ex)
{
logHelper?.LogError("Exception Message=" + ex.Message + "stacktrace =" + ex.StackTrace);
}
await Task.CompletedTask;
}OnCartValidationAsync
This method is used to write validation callback logic that will be triggered on clicking "Validate" button. In below example the code sets validation error when any line item found in cart which is not in cancelled status but start date is not 1st of the month, shows the validation error as Please adjust end dates to reflect a full calendar month.
public async Task OnCartValidationAsync(IActionRequest actionRequest, CancellationToken cancellationToken)
{
ThrowIfCancellationRequested(cancellationToken);
var logHelper = GetLogHelper();
try
{
logHelper?.LogInformation("ValidationCallback.OnCartValidationAsync Started");
var cartLines = actionRequest.GetCartContext().GetLineItems().SelectMany(x => x.GetChargeLines()).ToList();
foreach (ILineItemModel lineItem in cartLines)
{
if (lineItem.LineStatus != "Cancelled" && !(lineItem.SellingTerm == Math.Floor(lineItem.SellingTerm ?? 0m)))
{
lineItem.SetValidationError("Please adjust end dates to reflect a full calendar month.");
}
}
logHelper?.LogInformation("ValidationCallback.OnCartValidationAsync Ended");
}
catch (Exception ex)
{
logHelper?.LogError("Exception Message=" + ex.Message + "stacktrace =" + ex.StackTrace);
}
await Task.CompletedTask;
}FinalizeValidationAsync
This method is used to write validation callback code for cart finalize action. In the below example the code sets validation error when cart is getting finalized and a line item with quantity as null or zero is identify, shows the validation error as Quantity cannot be zero or null.
public async Task FinalizeValidationAsync(IActionRequest request, CancellationToken cancellationToken)
{
ThrowIfCancellationRequested(cancellationToken);
var logHelper = GetLogHelper();
try
{
logHelper?.LogInformation("ValidationCallback.FinalizeValidationAsync Started");
var cartLines = request.GetCartContext().GetLineItems().SelectMany(x => x.GetChargeLines()).ToList();
foreach (ILineItemModel lineItem in cartLines)
{
if (lineItem.Quantity == null || lineItem.Quantity == 0)
{
lineItem.SetValidationError("Quantity cannot be zero or null.");
}
}
logHelper?.LogInformation("ValidationCallback.FinalizeValidationAsync Ended");
}
catch (Exception ex)
{
logHelper?.LogError("Exception Message=" + ex.Message + "stacktrace =" + ex.StackTrace);
}
await Task.CompletedTask;
}