You need to sign in to do that
Don't have an account?
Trigger: Ensure picklist value is in a Map to validate it??
Hi,
I've got a requirement to ensure that Lead Status values that are "Converted" aren't chosen while the Lead is unconverted, as Salesforce.com allows.
I suppose I could write a validation rule to accomplish this, ensuring I edit the validation rule every time something changes in the Lead Status offerings, but we know how un-elegant that is.
I thought I could accomplish this in a trigger by building a map of unacceptable values for Lead.Status:
Map<Id,LeadStatus> statusList = new Map<Id,LeadStatus>([SELECT Id, MasterLabel
from LeadStatus where IsConverted=true]);
What I can't figure out is how can I compare the value being chosen for Lead.Status to ensure it's not in the map.
for (Lead newLead : Trigger.new) {
if (Trigger.IsUpdate) {
if (newLead.IsConverted != true) {
// how can I do this :
// if (newLead.Status isn't a value in statusList) {
// Trigger.old[0].addError('Invalid Choice');
// }
}
Any insight is much appreciated!!
You can use the get() method of the Map collection to see if the value is in the collection.
for (Lead newLead : Trigger.new) {
if (Trigger.IsUpdate) {
if (newLead.IsConverted != true) {
if ((LeadStatus)statusList.get(newLead.Status) == null)
Trigger.old[0].addError('Invalid Choice');
}
}
HTH
Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com