function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Trigger: Ensure picklist value is in a Map to validate it??



I've got a requirement to ensure that Lead Status values that are "Converted" aren't chosen while the Lead is unconverted, as 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 : {

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!!

Best Answer chosen by Admin (Salesforce Developers) 

You can use the get() method of the Map collection to see if the value is in the collection.



for (Lead newLead : {
if (Trigger.IsUpdate) {
if (newLead.IsConverted != true) {

if ((LeadStatus)statusList.get(newLead.Status) == null)
Trigger.old[0].addError('Invalid Choice');





Jeff Douglas
Appirio, Inc.