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.

Validation Callback applies validation on the following aspects in the configuration: ​

  • Line items in the cart
  • Price Ramps​

To use the Validation Callback you must create a custom Apex class that implements the Apttus_Config2.CustomClass.IValidationCallback3 interface and register the custom apex class with Validation Callback Class. You must write your custom logic in the custom apex class.

The following methods are available in the Apttus_Config2.CustomClass.IValidationCallback3 interface:

MethodSignatureDescription
validateCart()Apttus_Config2.CustomClass.ValidationResult validateCart (Apttus_Config2.CustomClass.ActionParams, Apttus_Config2.ProductConfiguration)

You can use this method to apply validation on line items in the cart. This method is invoked when the Sales rep performs the following actions:

  • View Cart 

    To invoke Validation Callback when click View Cart on the Mini Cart, you must enable Run Validation Callback On Add in Config System Properties custom setting. For more information, see Config System Properties.

  • Reprice
  • Save
  • Finalize
  • Analyze Deal
  • Validate on the Configuration page.
  • Any action that is available on the Cart and Catalog page.
  • Custom actions
validateRampLineItems()Apttus_Config2.CustomClass.ValidationResult validateRampLineItems (Apttus_Config2.CustomClass.ActionParams, Apttus_Config2.ProductConfiguration, List)

You can use this method to apply validation on the ramp line items in a Price Ramp. This method is invoked when the Sales rep performs the following actions:

  • Click Save on the Price Ramp pop-up
  • Any action that is available on the Cart and Catalog page when price ramps exist for any line item.

The following are examples of the custom logic you can implement using Validation example

Example

You can use validateCart()  apply the following validations:

  • Check if the Duration for any product is null.
  • Check if the value in the Adjustment Amount field is negative.

The given code example implements two methods of IValidationCallback3 interface. The code sample implements the abovementioned validation using the method validateCart()

global with sharing class Test_ValidationCallBack_UC1 implements Apttus_Config2.CustomClass.IValidationCallback3 {
    
    global Apttus_Config2.CustomClass.ValidationResult validateCart(Apttus_Config2.CustomClass.ActionParams params, Apttus_Config2.ProductConfiguration cart) {
        system.debug('***ValidationCallBack > validateCart() > ' + cart);
        
        Apttus_Config2.CustomClass.ValidationResult result = new Apttus_Config2.CustomClass.ValidationResult(true); 
       
        list <Apttus_Config2.LineItem> allLines = cart.getLineItems();
        list<Apttus_Config2__LineItem__c> allLineItems = getLineItems(allLines); 

        
        Id configurationId;
        if(allLineItems != null) {
            configurationId = allLineItems[0].Apttus_Config2__ConfigurationId__c;            
        }

        Integer i = 0;
        System.debug('***ValidationCallBack > allLineItems: '+allLineItems.size() + ' <> ' + allLineItems);
        for(Apttus_Config2__LineItem__c lineItem : allLineItems) {
            system.debug('***ValidationCallBack > ' + (++i) + ' > lineItem : ' + lineItem);

            // Check if the Duration for any product is null.
            if(lineItem.Apttus_Config2__SellingTerm__c == null) {
                result.Messages.add(new ApexPages.Message(ApexPages.Severity.ERROR, 'Value of Duration for product : \'' + lineItem.Apttus_Config2__Description__c + '\' is None.'));
                result.isSuccess =  false;
            }
            // Check if the value in the Adjustment Amount field is negative.
            if(lineItem.apttus_config2__adjustmentamount__c < 0) {
                result.Messages.add(new ApexPages.Message(ApexPages.Severity.ERROR, 'Adjustment Amount for product : \'' + lineItem.Apttus_Config2__Description__c + '\' is Negative. It should be >= 0'));
                result.isSuccess =  false;
            }     
        }
        return result;
    }
    
  
    global Apttus_Config2.CustomClass.ValidationResult validateRampLineItems(Apttus_Config2.CustomClass.ActionParams params, Apttus_Config2.ProductConfiguration cart,List<Apttus_Config2.LineItem> lstLI) {
        system.debug('***ValidationCallBack > validateRampLineItems() > Cart : ' + cart + ' <> lstTemp : ' + lstLI);
        Apttus_Config2.CustomClass.ValidationResult result;
        return result;
    }
    
      
    /* Gets the list of product line items associated with the Battery line
     * @param cart the cart object
     * @return the list of line item objects
    */
    private static List<Apttus_Config2__LineItem__c> getLineItems(List<Apttus_Config2.LineItem> allLines) {
        list<Apttus_Config2__LineItem__c> lineItems = new list<Apttus_Config2__LineItem__c>();
        // iterate through the cart and get the line items matching the battery code1 
        for (Apttus_Config2.LineItem lineItemMO : allLines) {
            lineItems.add(lineItemMO.getLineItemSO());
        }
        
        return lineItems;
    }  
  }
CODE