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
Rinigrace MekalaRinigrace Mekala 

Multiselect picklist field should throw error!

1) There is a multiselect picklist field 'BU_Specialty_Name__c' with values "A","B","C" & "R". The condition is that, If user selects any value along with "R", an error should occur saying that 'R should be selected alone'. For this I have implemented a Validation rule as below: 
IF(OR(AND(INCLUDES(BU_Specialty_Name__c,"R"),INCLUDES( BU_Specialty_Name__c,"A")),
      AND(INCLUDES(BU_Specialty_Name__c,"R"),INCLUDES( BU_Specialty_Name__c,"B")),
      AND(INCLUDES(BU_Specialty_Name__c,"R"),INCLUDES( BU_Specialty_Name__c,"C"))
      ),
  true, false)
It is working fine for the environment(Sandbox) which has "A","B","C"&"R" values for 'BU_Specialty_Name__c'.
2) But now, the requirment is that, For the other environment(Prod),  'BU_Specialty_Name__c' has few more values "A","B","C","D", "E" & "R". 
    Here also same condition should be given that is, If user selects any value along with "R", an error should occur.
   And we cannot overwrite Validation rule depending upon environment always.

  So,Is there any alternative to throw an error When user selects any value along with "R", instead of calling all the values as I did above?
 The idea should work for all environments independant on other multiselect picklist values.

Any kind of suggestion is accepted. Thanks in advance!
Best Answer chosen by Rinigrace Mekala
Lokeswara ReddyLokeswara Reddy
I think, this can not be done using validation rule as Multiselect picklist fields support ONLY few fuctions.
You may probably want to move this validation to a trigger as shown here

trigger AccountTrigger on Account (before insert, before update) {
  List<Account> accountList = trigger.new;
    for(Account accObj:accountList){
        Set<String> tempSet = new Set<String>(accObj.BU_Specialty_Name__c.split(';'));
        system.debug('temp string:'+tempSet);
        if(tempSet.contains('R') && tempSet.size()>1){
            accObj.BU_Specialty_Name__c.addError('R should be selected alone');
        }
    }
}

User-added image
Modify this with your object name and move the logic to an apex class if needed to keep the trigger simple.

All Answers

Lokeswara ReddyLokeswara Reddy
I think, this can not be done using validation rule as Multiselect picklist fields support ONLY few fuctions.
You may probably want to move this validation to a trigger as shown here

trigger AccountTrigger on Account (before insert, before update) {
  List<Account> accountList = trigger.new;
    for(Account accObj:accountList){
        Set<String> tempSet = new Set<String>(accObj.BU_Specialty_Name__c.split(';'));
        system.debug('temp string:'+tempSet);
        if(tempSet.contains('R') && tempSet.size()>1){
            accObj.BU_Specialty_Name__c.addError('R should be selected alone');
        }
    }
}

User-added image
Modify this with your object name and move the logic to an apex class if needed to keep the trigger simple.
This was selected as the best answer
Salvatore EspositoSalvatore Esposito
Hi Rinigrace Mekala. 

In your case, you can get out "R" from multiselect picklist (You can set R as a checkbox or anything else).
Then you can check if R is checked AND picklist is not blank.

With this technique you will be indipendent.

Bye.

 
Rinigrace MekalaRinigrace Mekala
It means a lot to me. Thank you so much Lokeswara Reddy!
Rinigrace MekalaRinigrace Mekala
Thanks for the suggestion Salvatore Esposito!