You need to sign in to do that
Don't have an account?
Trif Cristian 7
This trigger it's not firing.. I'm confused why not?
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?
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.
If you want to this code to work in case of update , then you have to use before update.
All Answers
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
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.
Hope this helps.
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?
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:
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
Please try with this one.
I can understand that you are not using validation rules as you want it dynamic :)
I have tested this works in my org . This should work fine.
Thanks,
Hi, trigger still not working :(
the trigger:
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.
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.
If you want to this code to work in case of update , then you have to use before update.