Enforce limitations on Standard Picklist Values To Adhere To The Specified Set in Salesforce By Utilizing Apex Triggers.

  • In Salesforce, picklists serve as a valuable tool for standardizing data and maintaining data quality by offering a predefined set of values for users to choose from during record creation or updating.
  • While Salesforce allows the restriction of picklist values in custom picklists, this option is not available for standard picklists. It becomes crucial to verify that values entered for specific standard picklist fields align with the corresponding picklists. Failing to do so can result in data inconsistencies and pose security challenges.
  • To address such scenarios, Apex triggers come into play by enabling the validation of picklist values. They ensure that only valid values are stored in the database. This blog post will explore the process of restricting Standard Picklist values on an Sobject using an Apex Trigger, focusing on an example involving the validation of Opportunity Type and Lead Source fields within the Opportunity object.
Sample Code:

Here is the sample code for the trigger – Restrict Standard Picklist values such as Opportunity Type and Lead Source fields in the Opportunity object, using an Apex Trigger – “ValidatePicklistValues”.

/**
 * This trigger validates that the values entered for the Opportunity Type and Lead Source
 * fields are present in the corresponding picklists. If a value is not present in the picklist,
 * an error message is added to the field and the record cannot be saved.
 */
trigger ValidatePicklistValues on Opportunity(before insert, before update) {
    // Set to store valid values for Opportunity Type picklist
    Set<String> validOpportunityTypeValues = new Set<String>();
    // Set to store valid values for Lead Source picklist
    Set<String> validLeadSourceValues = new Set<String>();
 
    // Get the description of Opportunity Type field
    Schema.DescribeFieldResult fieldType = Opportunity.Type.getDescribe();
    // Get the picklist values of Opportunity Type field
    List<Schema.PicklistEntry> types = fieldType.getPicklistValues();
    // Get the description of Lead Source field
    Schema.DescribeFieldResult fieldLeadSource = Opportunity.LeadSource.getDescribe();
    // Get the picklist values of Lead Source field
    List<Schema.PicklistEntry> leadsources = fieldLeadSource.getPicklistValues();
 
    // Add valid Opportunity Type values to the set
    for (Schema.PicklistEntry entry : types) {
        validOpportunityTypeValues.add(entry.getValue());
    }
    // Add valid Lead Source values to the set
    for (Schema.PicklistEntry entry : leadsources) {
        validLeadSourceValues.add(entry.getValue());
    }
 
    // Iterate through each Opportunity record in the trigger
    for (Opportunity record : Trigger.new) {
        // Validate Opportunity Type value
        if (!validOpportunityTypeValues.contains(record.Type)) {
            record.Type.addError('Value not in Opportunity Type picklist');
        }
        // Validate Lead Source value
        if (!validLeadSourceValues.contains(record.LeadSource)) {
            record.LeadSource.addError('Value not in Lead Source picklist');
        }
    }
}