Subline Validator Class
  • 27 Jun 2023
  • 2 Minutes to read
  • Dark
    Light

Subline Validator Class

  • Dark
    Light

Article summary

Use this hook to identify data combinations between parent fields and their subline fields that are not valid.

This hook is invoked in the onChange event in any field on any subline in the Edit Lines modal. The results of the hook identify validation errors. If errors exist, the hook prevents the user from saving the current state of sublines in the Edit Lines modal.

Apex hook class

  • It must implement the zpl.SublineValidatorConfigurer interface.
  • It must have a global access modifier.
  • You must specify the Apex hook class name in the Subline Validator Class field of the Product Selector Configuration that is mapped to the Line Items component to be affected by the hook.

Interface definition

/**
 * Interface for a hook class used to validate sublines.
 *
 * @param sublines the sublines that have been edited.
 * @return a Map were key is subline Id and value is Map where key is field api name and value
 * is error that will be displayed.
 */
global interface SublineValidator
{
    Map<String, Map<String, String>> validateSublines(List<SObject> sublines);
}

Example of the class implementation

global class TestSublineValidator implements zpl.SublineValidator
{
    global Map<String, Map<String, String>> validateSublines(List<SObject> contractSublines) {
        Map<String,Map<String,String>> results = new Map<String,Map<String,String>>();
        String errorMessage = 'Value isnt acceptable';
        String errorMessage2 = 'Value should be <= 100';
        for (SObject subline : contractSublines)
        {          
            if ((Boolean)subline.get('zplpack1k__IsActive__c') ) 
            {
                if ((Double)((zplpack1k__ContractSubline__c) subline).get('Number__c') == 25.0)
                {
                    results.put(subline.Id, new Map<String, String>{'Number__c' => errorMessage});
                }
                else if ((Double)((zplpack1k__ContractSubline__c) subline).get('Number__c') == 30.0)
                {
                    results.put(subline.Id, new Map<String, String>{'Number__c' => errorMessage});
                }
                
                if ((Double)((zplpack1k__ContractSubline__c) subline).get('Currency__c') > 100.0)
                {
                    results.put(subline.Id, new Map<String, String>{'Currency__c' => errorMessage2});
                }
            }
        }
        return results;
}
}

Additional sample code

global with sharing class ValidationHook implements SublineValidator
{
    global Map<String, Map<String, String>> validateSublines(List<SObject> sublines)
    {
        Map<String, Map<String, String>> errors = new Map<String, Map<String, String>>();

        for (SObject subline : sublines)
        {
            //here you can write any statement for each subline
            Map<String, String> errorByField = new Map<String, String>();


            if (subline.get('zpl__ups_Guidance__c') < 10) //in this case if guidance is less than 10 then error message will appear for it
            {
                errorByField.put('zpl__ups_Guidance__c', 'error on Guidance');
                errorByField.put('zpl__relatedGuidanceField__c', 'error for related field');//also here you can add validation for another field if this condition not suitable for it.
            } // so, after execution of this block, two error will appear for zpl__ups_Guidance__c - 'error on Guidance' and
            //   for zpl__relatedGuidanceField__c - 'error for related field'

            if (subline.get('zpl__someTextField__c') == '')
            {
                errorByField.put('zpl__someTextField__c', 'this field cannot be empty');
            }//this block add validation for zpl__someTextField__c that it cannot be empty

            errors.put(String.valueOf(subline.Id), errorByField);
        }

        return errors;
    }
}

Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.