2

I have a custom object (Expense__c) which has the following picklists (Type__c & Sub_Type__c).

Sub_Type__c is a dependent picklist based on Type__c.

When I use the following anonymous apex insert I am able to insert Expense__c records with values in "Type__c" & "Sub_Type__c" that are not defined.

For example the following snippet works(even though type__c is a picklist and does not have "first" as one of the pick elements) fine and inserts records into db.

List<Expense__c> e = new List<Expense__c>();
e.add(new Expense__c(type__c='first',sub_type__c='second',amount__c = 100));
insert e;

Can someone explain how this can happen ?

3
  • Do you want to avoid this behaviour of getting inserted the value which is not defined?
    – snehakem
    Jan 9, 2015 at 5:04
  • Yes...thats what my intent is..and also I would like to know how can the picklist's sanctity be bypassed ?
    – Varun
    Jan 9, 2015 at 5:08
  • 2
    SF doesnt have any validation for that. Least you can do is check the value needed to be assigned in apex itself like myPickValuesSet.contains(myValueToBeAssined), Where myPickValuesSet is the set of all predefined picklist values in your Type__c field
    – snehakem
    Jan 9, 2015 at 5:12

2 Answers 2

4

Unfortunately that is the expected behavior, records with invalid picklist items can be imported or on a visualforce pages that are set as inputText vs inputField or via Apex. There is an idea that appears to be actively worked on out on the idea exchange so hopefully at least as far as custom picklist fields and importing are concerned, it sounds like there will be something coming soon. You could create validation rules to validate the fields or through code as mentioned in a comment above. An example validation rule was posted by Steve Molis here:

CASE(Industry,
"Agriculture", 1,
"Apparel", 1,
"Banking", 1,
"Biotechnology", 1,
"Chemicals", 1,
"Communications", 1,
"Construction", 1,
"Consulting", 1,
"Education", 1,
"Electronics", 1,
"Energy", 1,
"Engineering", 1,
"Entertainment", 1,
"Environmental", 1,
"Finance", 1,
"Food & Beverage", 1,
"Government", 1,
"Healthcare", 1,
"Hospitality", 1,
"Insurance", 1,
"Machinery", 1,
"Manufacturing", 1,
"Media", 1,
"Not For Profit", 1,
"Other", 1,
"Recreation", 1,
"Retail", 1,
"Shipping", 1,
"Technology", 1,
"Telecommunications", 1,
"Transportation", 1,
"Utilities", 1, 
"", 1,
0) <> 1

Update: Winter 16 release has a restricted picklist option in pilot:
Eliminate Picklist Clutter with Restricted Picklists (Pilot)

Enforce the integrity of your picklist data with restricted picklists.
Values in a restricted picklist field can include only the values you’ve defined. This keeps your picklist data clean by preventing users from adding erroneous or redundant values. This feature is available in both Lightning Experience and Salesforce Classic.
Only a new custom picklist field can be a restricted picklist. You can’t convert an existing non-restricted picklist to a restricted picklist.

1
  • 1
    Thanks J...your explanation helps and I better build a validation rule or modify my VF page property :)
    – Varun
    Jan 10, 2015 at 3:21
1

Salesforce treats picklist fields as text fields. The only thing you are doing when assigning picklist values or dependent picklists is modifying the ways to input that information. The picklist just acts as an easy way to fill out a text field. Obviously there are a few other features that come along with picklists, but validation is not one of them.

Jenny B's answer will help you add that validation if you want it. Unfortunately there isn't much of a better way to do it without writing code.

I hope this helps your understanding!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .