.ultrageek. Computers, Music, and Thoughts


Test Classes with Global Picklists

Global Picklists or as Salesforce.com likes to refer to them, "Picklist Value Sets" from the menu or "Global Value Sets" in change sets, are not new but my dealings with them are. Specifically in Apex and test classes with a parent/child relationship using Global Picklists.

Global picklists btw do not have the same limitations normal picklists have in terms of the 1,000 character limitation that previous you had to use record types or a custom solution to get around.

So when you get the following error message in your test class testings: "INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field". What this means is that under the user you are running the test under is not configured to use the values you have populated in the test class. Usually because the test is running under system and is using the master record type that does not have the field mappings. That is my guess at least.

Two ways to fix:

  • Set the RecordTypeId in the test data either statically or dynamically through a variable and a soql lookup using the name/developer name
  • Use the System.runAs(thisUser) method to either run as the user running the test or create a user with a specifically assigned profile that has the correct privileges and set up.

Below are examples: