You need to sign in to do that
Don't have an account?
Bryan Leaman 6
I need valid picklist value for record type in a test class
We have a picklist used by marketing to designate special pricing programs. This object in question also has several record types. Ideally, the same values would always be defined as available for all record types, but when they aren't my test class can't tell which ones are valid for the record I'm creating for the test. Also, I can't call out in a test to the metadata api to obtain valid values by record type. This picklist is a global picklist, so it must be restricted to the defined values.
How can I ensure my test class will always use valid values for the picklist?
How can I ensure my test class will always use valid values for the picklist?
Another way to do this is by fetching the valid picklist values in test class constructor and use it in test methods.
You can also create a custom setting or custom metadata type with the picklist values and record type and use it in test class.
It's important to note that it is generally considered a best practice to use real data in test classes, so if it's possible to obtain valid picklist values for the record type you are testing, it's generally better to use those values than to hard-code them in the test class.
Here, in this example, the test class is testing a custom object named Example__c which has a picklist field named PicklistField__c and has a record type named RecordTypeName. The setUp method queries the RecordType object to retrieve the valid picklist values for the RecordTypeName record type and stores them in the validPicklistValues set. Then the test methods testMethod1 and testMethod2 insert the Example__c object with picklist value 'ValidValue' and 'InvalidValue' respectively and asserts whether the picklist value used is valid or not by checking in validPicklistValues set.
Did you try any of that code at all (like simply running it in anonymous apex)? The SOQL statement getting "ValidFor" doesn't work. There's no "PicklistValues" relationship to the RecordType object.
--Bryan