2

I have a bunch of existing values for picklist and now we want to make the picklist as restricted picklist. Some of the records may contain values which are not in the restricted picklist.

I initially thought that by making a picklist restricted it wouldnt allow save of the record if it had a bad value. But it did allow edit and save even if there value in picklist is bad.

What could be the best way to make sure the users change the value before they save the record again?

1, Validation Rule : Its ok if the picklist is small but if picklist is in hundreds then its gonna be big VR.

  1. Trigger

Any other way anyone can think of?

2 Answers 2

4

In addition to validation rules and triggers you also have the option of using a process builder flow or even a full flow to address this.

On the assumption that you can't automate the replacement of one bad value with one good value (this could be done as Pranay suggested I believe, or via a simple bit of anonymous apex with a bit of logic an SOQL query and a DML update), personally I would do this in a trigger, something like:

// First get the set of permitted values from the picklist, keeping just active ones
List<Schema.PicklistEntry> entries = Schema.SObjectType.SomeObject.fields.SomeField.getPicklistValues();

Set<String> permittedValues = new Set<String>();

for (Schema.PicklistEntry entry : entries) {
    if (entry.isActive()) {
        permittedValues.add(entry.getValue());
    }
}

// Now check each object given to the trigger
for (SomeObject someObject : Trigger.new) {
    if (!permittedValues.contains(someObject.SomeField)) {
        someObject.SomeField.addError('SomeField value must be changed as it is not valid (' + someObject.SomeField + ')');
    }
}
2
  • i was thinking of taking the trigger route. But what do you mean by full flow or process builder path? did you mean to perform the validation on the process builder or flow?
    – Prady
    Jul 4, 2019 at 7:33
  • Process Builder: help.salesforce.com/… and Flow: help.salesforce.com/articleView?id=flow.htm&type=5 In each case you can have automation that fixes the bad value selection automatically, if you can do that and don't want to do a simple data migration up-front.
    – Phil W
    Jul 4, 2019 at 9:20
4

I can think of another way, not sure if this will fill your use case. In Restricted picklist add that bad value in metadata, and then click on delete,

Restircted Picklist

SF will show a screen asking you to replace the old value with new value, which would be clean and from the restricted list.

3
  • This is a good approach if you know that each bad value can be mapped, in every object, to the same good result. If the user needs to do this on an object-by-object basis, consider what I have suggested in my answer.
    – Phil W
    Jul 4, 2019 at 7:24
  • Thanks Pranay. This looks promising but some values could not be there in the picklist. i am probably moving towards the trigger path. But its always good to learn
    – Prady
    Jul 4, 2019 at 7:28
  • @Prady - By defining the picklist as restricted, you are implicitly saying that all values for the field must conform to the new restricted list. Hence all values in the database that are not in the restricted set should be mappable, even if to Other (or at least all records in the database that the user can potentially edit). The nice thing about Pranay's solution is that no triggers will execute and hence no side effects from a Data Loader patch job
    – cropredy
    Jul 4, 2019 at 18:58

You must log in to answer this question.

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