Pre-Entry Field Options Filterer
  • 27 Jun 2023
  • 2 Minutes to read
  • Dark
    Light

Pre-Entry Field Options Filterer

  • Dark
    Light

Article summary

This Apex hook controls the list of options shown in the picklist of a Pre-entry field. If the hook is not applied, then all Picklist values appear in the dropdown.
The input for this hook includes the list of all pre-entry fields configured for this component, along with any current entered user value. This allows the hook to filter the Picklist of the current pre-entry field based on the values of previously entered pre-entry fields.

For example, assume you have two pre-entry fields: State and City. The State pre-entry field is a controlling field for the City pre-entry field. Each time the user changes the state pre-entry field, new options for the city pre-entry field are presented.

This Apex hook occurs before a Pre-entry field of type Picklist is displayed to a user on the Add Lines component.

Apex hook class

  • It must implement the zpl.PreEntryFieldOptionsFilterer interface.
  • It must have a global access modifier.
  • You must specify the Apex hook class name in the Pre-entry Field Options Filterer field of the Product Selector Configuration.

Interface definition

/** 
 * Interface for the hook class for filtering picklist options in configured pre-entry fields. 
 * 
 * @param currentField the pre-entry field for which the list of available options is configured 
 * @param defaultFieldOptions default list of options for the current pre-entry field 
 * @param allFields list of the pre-entry fields configured in the system 
 * @param valuesBySalesforceFieldName map of the user-entered values for the each pre-entry field defined. 
 *                  KEY: API name of the pre-entry field (SalesforceField__c), VALUE: user-entered field value 
 * @param headerRecordId the ID of current Header object record (ID of Contract or Price Lookup record) 
 * @returns a new list of options that will be displayed to user for selection for the current pre-entry field. 
 */ 
global interface PreEntryFieldOptionsFilterer 
{ 
    List<zpl__PicklistValue> getFilteredOptions( 
            zpl__PreEntryField__c currentField, 
            List<zpl__PicklistValue> defaultFieldOptions, 
            zpl__PreEntryField__c[] allFields, 
            Map<String, Object> valuesBySalesforceFieldName, 
            Id headerRecordId); 
} 

Example of the class implementation

global with sharing class SimplePreEntryFieldOptionsFilterer implements zpl.PreEntryFieldOptionsFilterer 
{ 
    private static final String STATUS_CONTRACT_LINE_ITEM_FIELD = Schema.SObjectType.zpl__ContractLineItem__c.fields.zpl__Status__c.getName(); 
    private static final String EFFECTIVE_DATE_CONTRACT_LINE_ITEM_FIELD = Schema.SObjectType.zpl__ContractLineItem__c.fields.zpl__EffectiveDate__c.getName(); 
    private static final String WAREHOUSE_PREFERENCE_CONTRACT_LINE_ITEM_FIELD = Schema.SObjectType.zpl__ContractLineItem__c.fields.WarehousePreference__c.getName(); 

    global List<zpl.PicklistValue> getFilteredOptions(zpl__PreEntryField__c currentField, List<zpl.PicklistValue> defaultFieldOptions, 
            zpl__PreEntryField__c[] allFields, Map<String, Object> valuesBySalesforceFieldName, Id headerRecordId) 
    { 
        if (isStatusField(currentField) && isFutureEffectiveDate(valuesBySalesforceFieldName.get(EFFECTIVE_DATE_CONTRACT_LINE_ITEM_FIELD))) 
        { 
            List<zpl.PicklistValue> filteredPicklistOptions = new List<zpl.PicklistValue>(); 
            for (zpl.PicklistValue picklistValue : defaultFieldOptions) 
            { 
                if (picklistValue.value != 'Active') 
                { 
                    filteredPicklistOptions.add(picklistValue); 
                } 
            } 
            return filteredPicklistOptions; 
        } 
        if (isWarehousePreferenceField(currentField) && !isUserSystemAdministrator()) 
        { 
            List<zpl.PicklistValue> filteredPicklistOptions = new List<zpl.PicklistValue>(); 
            for (zpl.PicklistValue picklistValue : defaultFieldOptions) 
            { 
                if (picklistValue.label.startsWith('N')) 
                { 
                    filteredPicklistOptions.add(picklistValue); 
                } 
            } 
            return filteredPicklistOptions; 
        } 
        return defaultFieldOptions; 
    } 
  
    private static Boolean isStatusField(zpl__PreEntryField__c currentField) 
    { 
        return currentField.zpl__SalesforceField__c == STATUS_CONTRACT_LINE_ITEM_FIELD; 
    } 

    private static Boolean isFutureEffectiveDate(Object effectiveDate) 
    { 
        return ((Date) effectiveDate) > Date.today(); 
    } 

    private static Boolean isWarehousePreferenceField(zpl__PreEntryField__c currentField) 
    { 
        return currentField.zpl__SalesforceField__c == WAREHOUSE_PREFERENCE_CONTRACT_LINE_ITEM_FIELD; 
    } 

    private static Boolean isUserSystemAdministrator() 
    { 
        String userProfileName = 
        [ 
                SELECT Name 
                FROM profile 
                WHERE id =: UserInfo.getProfileId() 
        ].Name; 

        return userProfileName == 'System Administrator'; 
    } 
} 

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.