How can I troubleshoot and resolve the issue of “bad value” in the picklist?

126    Asked by BenjaminMoore in Salesforce , Asked on Apr 23, 2024

I am currently working on a Salesforce project in which I have set a restricted picklist field for the ‘Lead source’ on the lead object. One of my team members tries to enter a value that is not in the picklist option thus resulting in a “bad value” error. How can I troubleshoot and resolve this particular issue? 

Answered by David

 In the context of Salesforce, here are the appropriate approaches given of how you can troubleshoot and resolve this particular issue:-

Validation rule

Firstly, you would need to create a validation rule on the lead object to check the lead source value that is entered by the users.

ISPICKVAL(LeadSource, ‘Web’) ||
ISPICKVAL(LeadSource, ‘Phone Inquiry’) ||
ISPICKVAL(LeadSource, ‘Partner Referral’) ||
ISPICKVAL(LeadSource, ‘Purchased List’) ||
ISPICKVAL(LeadSource, ‘Other’)

Error message

You should provide a meaningful error message which would help in explaining that the entered value is not allowed for the lead source field..

Apex trigger

If you want to take additional actions or even want to provide custom handling then you can use an apex trigger.

Trigger LeadTrigger on Lead (before insert, before update) {
    List invalidLeads = new List();
    For (Lead leadRecord : Trigger.new) {
        If (!isValidLeadSource(leadRecord.LeadSource)) {
            leadRecord.addError(‘Invalid Lead Source value. Please choose from the provided options.’);
            invalidLeads.add(leadRecord); // Collect invalid leads for further processing if needed
        }
    }
    // Example: Log or notify about invalid leads
    If (!invalidLeads.isEmpty()) {
        // Example: Log the invalid leads
        System.debug(‘Invalid Leads: ‘ + invalidLeads);
        // Example: Send an email notification to the admin
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        Email.setToAddresses(new List{‘admin@example.com’});
        Email.setSubject(‘Invalid Leads Detected’);
        Email.setPlainTextBody(‘The following leads have invalid Lead Source values: ‘ + invalidLeads);
        Messaging.sendEmail(new List{email});
    }
}
Public class LeadUtils {
    Public static Boolean isValidLeadSource(String leadSource) {
        Set validLeadSources = new Set{‘Web’, ‘Phone Inquiry’, ‘Partner Referral’, ‘Purchased List’, ‘Other’};
        Return validLeadSources.contains(leadSource);
    }
}
Here is the example given in java programming language:-
Import java.util.HashSet;
Import java.util.Set;
Class Lead {
    String LeadSource;
    Public Lead(String leadSource) {
        This.LeadSource = leadSource;
    }
    Public String getLeadSource() {
        Return LeadSource;
    }
    Public void setLeadSource(String leadSource) {
        This.LeadSource = leadSource;
    }
}
Public class LeadUtils {
    Public static Set validLeadSources = new HashSet() {{
        Add(“Web”);
        Add(“Phone Inquiry”);
        Add(“Partner Referral”);
        Add(“Purchased List”);
        Add(“Other”);
    }};
    Public static boolean isValidLeadSource(String leadSource) {
        Return validLeadSources.contains(leadSource);
    }
    Public static void main(String[] args) {
        Lead leadRecord1 = new Lead(“Web”);
        Lead leadRecord2 = new Lead(“Invalid Source”);
        // Example of checking and handling “bad value” error
        If (!isValidLeadSource(leadRecord1.getLeadSource())) {
            System.out.println(“Invalid Lead Source value. Please choose from the provided options.”);
        }
        If (!isValidLeadSource(leadRecord2.getLeadSource())) {
            System.out.println(“Invalid Lead Source value. Please choose from the provided options.”);
            // Additional handling logic can be added here, such as logging or notifying users
        }
    }
}
Here is the example given in python programming language:-
Class Lead:
    Def __init__(self, lead_source):
        Self.LeadSource = lead_source
Class LeadUtils:
    Valid_lead_sources = {‘Web’, ‘Phone Inquiry’, ‘Partner Referral’, ‘Purchased List’, ‘Other’}
    @staticmethod
    Def is_valid_lead_source(lead_source):
        Return lead_source in LeadUtils.valid_lead_sources
    @staticmethod
    Def handle_bad_value(lead):
        If not LeadUtils.is_valid_lead_source(lead.LeadSource):
            Print(“Invalid Lead Source value. Please choose from the provided options.”)
            # Additional handling logic can be added here, such as logging or notifying users
# Create Lead instances
Lead_record1 = Lead(‘Web’)
Lead_record2 = Lead(‘Invalid Source’)
# Check and handle “bad value” error
LeadUtils.handle_bad_value(lead_record1)
LeadUtils.handle_bad_value(lead_record2)


Your Answer

Interviews

Parent Categories