5

Suppose i have a multi select picklist which has 30 items. Now i have to put a validation rule that "user can't choose all the items from picklist. Atleast one item should be available in available list."

How can i implement this validation.

I know i can do like " AND(INCLUDES(item_c,'1'),INCLUDES(item_c,'2'),INCLUDES(item__c,'3'),.... etc) for all values but i don't want to follow this approach as i will have to hard code all the values.

5

What you're asking can't be accomplished through a validation rule, without hand coding every value like you suggested.

On the other hand this could very easily be accomplished with a before update trigger that looks something like this:

trigger MyTrigger on MyObject__c (before update)
{
    //this variable could also be gotten through use of the system describe methods
    Integer totalPicklistItems = 30;

    for(MyObject__c obj : Trigger.new)
    {
        if(obj.MultiSelectField__c != null && obj.MultiSelectField__c != '')
        {
            //split picklist into list
            String[] selectedValues = obj.MultiSelectField__c.split(';', 0);

            //check if the selected selected values are less than 1 or equal to the
            //total picklist items to add an error
            if(selectedValues.size() < 1 || selectedValues.size() == totalPicklistItems)
                obj.MultiSelectField__c.addError('You must select at least 1 value but not all values.');
        }
        else
        {
            obj.MultiSelectField__c.addError('You must select at least 1 value.');
        }
    }
}
  • 6
    Plus, if the picklist values are controlled by record types, even that needs to be considered while implementing this. – Shailesh Deshpande Mar 6 '13 at 7:14
0

I've seen similar issues solved by doing an addition of If statement outputs.

Essentially, one if() statement per picklist value

If(includes(picklist, value1), 1, 0) +
If(includes(picklist, value1), 1, 0) +
... value n

Then wrap it in an if statement saying if total = n, fail to validate, else, record is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.