Restrict Standard Picklist Values within the defined set in Salesforce using Apex Triggers

Restrict Standard Picklist values within the defined set in Salesforce using apex triggers

, ,
  • In Salesforce, picklists are a great way to standardize data and ensure data quality. Picklists provide a set of predefined values that users can select from when creating or updating a record.
  • In custom picklist, salesforce gives an option to restrict picklist values ,but in standard picklist this option is not available.
  • It’s important to validate that the values entered for specific standard picklist fields are present in the corresponding picklists. If a value is not present in the picklist, it can lead to data inconsistencies and create security challenges.
  • In such cases, Apex triggers can be employed to validate picklist values. This ensures that only valid values are saved to the database.
  • In this blog post, we’ll see How to Restrict Standard Picklist values on an Sobject with Apex Trigger.
  • Let’s examine an example. We will validate the Opportunity Type and Lead Source fields in the Opportunity object using an Apex trigger.
Sample Code:

Here is the sample code for the trigger. It restricts Standard Picklist values, such as Opportunity Type and Lead Source fields, in the Opportunity object. This is done using an Apex Trigger called “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');
        }
    }
}

For more helpful articles please visit – https://thesalesforcedev.in/

Leave a comment