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
Trif Cristian 7Trif Cristian 7 

multiselect picklist validation rule with trigger

Hi all,

I'm trying to create a validation rule via trigger when two multiselect picklist values are the same then the validation to fire.

trigger differentValues on User (before insert) {

    for(User u : Trigger.New) {
        if(u.Restrict_access_by_Country__c =='DE' && u.TestMultiSelectCountry__c== 'DE' ) {
        u.addError('Values cannot be the same');
    }
    }
}

This trigger it's not firing.. I'm confused why not?
Best Answer chosen by Trif Cristian 7
Dushyant SonwarDushyant Sonwar
Hi Trif ,

Are you creating a new user or updating the fields of same user?
It will work when creating a new user and trying to put same values in multi select.
User-added image

If you want to this code to work in case of update , then you have to use before update.

All Answers

Anthony McDougaldAnthony McDougald
Good Morning Trif Cristian 7,
Hope that your day is off to an amazing start. May we inquire as to why you're not using a validation rule rather than a trigger? Looking forward to assisting you and may God bless you abundantly.
Best Regards,
Anthony McDougald
Dushyant SonwarDushyant Sonwar
Hi Triff ,

I would strongly agree with Anthony on this one , as it can be done using point and click (validation rules) using Includes method  

I believe you are trying to learn apex and want to implement this using apex code in your dev org.

Now coming back to the question why it is not working?
As you might not know that , multipicklist values are stored in semicolon format in SF database    DE;FL;CA
so this below condition will not
Restrict_access_by_Country__c =='DE'


containsAll will check if all the picklistvalues of fiest mulltiselect matches with all the picklist values of second multiselect then it will throw error.

 
trigger differentValues on User (before insert) {

    for(User u : Trigger.New) {
		if(u.Restrict_access_by_Country__c != null && TestMultiSelectCountry__c != null){
			Set<String> setOfRestrictAccessCtry =  new set<String>(u.Restrict_access_by_Country__c.split(';'));
			Set<String> testMultiSelectCountry =  new set<String>(u.TestMultiSelectCountry__c.split(';'));
			if(setOfRestrictAccessCtry.containsAll(testMultiSelectCountry)) {
				u.addError('Values cannot be the same');
			}
		}
    }
}


Hope this helps.
Trif Cristian 7Trif Cristian 7

Hi Dushyant,

The trigger you wrote does not trigger when I select two exact values on these 2 fields. I tried to select DE on these 2 fields and it does not work :(

Any idea why?

Trif Cristian 7Trif Cristian 7

I agree that you can make this with validation rules but then I have to add 250 values for each country and that will not be efficient for me..

This validation rule works but as I told you I needed to hardcode this:

 

OR

   (
INCLUDES(  TestMultiSelectCountry__c   , "TH") && INCLUDES(   Restrict_access_by_Country__c  , "TH"),
INCLUDES(  TestMultiSelectCountry__c   , "SA") && INCLUDES(   Restrict_access_by_Country__c  , "SA"),
INCLUDES(  TestMultiSelectCountry__c   , "DE") && INCLUDES(   Restrict_access_by_Country__c  , "DE"),
INCLUDES(  TestMultiSelectCountry__c   , "BR") && INCLUDES(   Restrict_access_by_Country__c  , "BR"),
INCLUDES(  TestMultiSelectCountry__c   , "DZ") && INCLUDES(   Restrict_access_by_Country__c  , "DZ"),
INCLUDES(  TestMultiSelectCountry__c   , "EG") && INCLUDES(   Restrict_access_by_Country__c  , "EG"),
INCLUDES(  TestMultiSelectCountry__c   , "ES") && INCLUDES(   Restrict_access_by_Country__c  , "ES"),
INCLUDES(  TestMultiSelectCountry__c   , "IT") && INCLUDES(   Restrict_access_by_Country__c  , "IT"),
INCLUDES(  TestMultiSelectCountry__c   , "PL") && INCLUDES(   Restrict_access_by_Country__c  , "PL"),
INCLUDES(  TestMultiSelectCountry__c   , "PT") && INCLUDES(   Restrict_access_by_Country__c  , "PT")
   )
Dushyant SonwarDushyant Sonwar
Hi Trif,

Could you make sure the values in both the multi picklist field are of same case ? It can be due to  one is lowecase 'de' and other is in 'DE or extra whitespaces.

Could you post the screenshot of the detail page where these fields are added ? It would be great
trigger differentValues on User (before insert) {

    for(User u : Trigger.New) {
		if(u.Restrict_access_by_Country__c != null && u.TestMultiSelectCountry__c != null){
			
			Set<String> setOfRestrictAccessCtry =  new set<String>(u.Restrict_access_by_Country__c.split(';'));
			Set<String> testMultiSelectCountry =  new set<String>(u.TestMultiSelectCountry__c.split(';'));
			
			System.debug(setOfRestrictAccessCtry + ' @@@@ '+ setOfRestrictAccessCtry.size()) ;
			System.debug(testMultiSelectCountry + ' @@@@ '+ testMultiSelectCountry.size()) ;
			
			if(setOfRestrictAccessCtry.containsAllIgnoreCase(testMultiSelectCountry)) {
				u.addError('Values cannot be the same');
			}
		}
    }
}

 
Dushyant SonwarDushyant Sonwar
Hi Trif,

Please try with this one.


I can understand that you are not using validation rules as you want it dynamic :)

 
trigger differentValues on User (before insert) {

    for(User u : Trigger.New) {
		if(u.Restrict_access_by_Country__c != null && u.TestMultiSelectCountry__c != null){
			
			Set<String> setOfRestrictAccessCtry =  new set<String>(u.Restrict_access_by_Country__c.split(';'));
			Set<String> testMultiSelectCountry =  new set<String>(u.TestMultiSelectCountry__c.split(';'));
			
			System.debug(setOfRestrictAccessCtry + ' @@@@ '+ setOfRestrictAccessCtry.size()) ;
			System.debug(testMultiSelectCountry + ' @@@@ '+ testMultiSelectCountry.size()) ;
			
			if(setOfRestrictAccessCtry.equals(testMultiSelectCountry)) {
				u.addError('Values cannot be the same');
			}
		}
    }
}

I have tested this works in my org . This should work fine.

Thanks,
Trif Cristian 7Trif Cristian 7

Hi, trigger still not working :(

User-added image

Trif Cristian 7Trif Cristian 7

the trigger:

User-added image

Trif Cristian 7Trif Cristian 7

I'm wondering how its working in your dev environment and in my environment not.. I'm selecting same value for these 2 fields and nothing happens.. What I'm doing wrong here?

I double checked the fields api names and its correct..no misspell or something like that.

Dushyant SonwarDushyant Sonwar
Hi Trif ,

Are you creating a new user or updating the fields of same user?
It will work when creating a new user and trying to put same values in multi select.
User-added image

If you want to this code to work in case of update , then you have to use before update.
This was selected as the best answer
Trif Cristian 7Trif Cristian 7
Thanks Dushyant! It works. God bless you my man.