I have a hierarchy custom setting with 2 custom checkbox fields & 3 profiles. I'm trying to use this profile based value in Validation rule. I have created the OWD on custom setting to be false & gave individual checkbox values at profile entires. However, the setup doesn't work as expected when i ran a test class with running user as sales (userrole.Name = Sales). The Validation rule fires irrespective of the BypassValidationRule__c values corresponding to the user's profile.

TEXT(StageName) = "Stage 3", 
TEXT(StageName) = "Stage 4"), 
($UserRole.Name = "Sales"),
($Setup.BypassRules__c.BypassValidationRule__c = false)

enter image description here

I know how to get specific values in apex but not sure about the validation or formula with respect to above. Any help on this would really be appreciated.

Thanks in advance!

  • 2
    Hierarchical custom settings are based on Org Wide, Profile and User. Roles does not come in picture here. Also you have to create the custom settings in test class as well Jul 16, 2019 at 21:23
  • Thanks for the response @PranayJaiswal, yes you can ignore the role statement that i mentioned above, what i meant was test class running user is a Sales Profile user, do i still need to add the custom settings in test class. Im little confused on this as when i tried this with List Type custom setting with just a single checkbox, it worked as expected. But when i tried it with hierarchy setting, i see this issue. Not sure why the contrast occurred.
    – SMorse
    Jul 16, 2019 at 21:40
  • 1
    You have to insert custom setting values for that profile even in test context. If am not wrong you might have created Custom metaData instead of list custom settings. Custom metaData is metaData so you don't create them in test context Jul 16, 2019 at 22:07
  • Ok, will do that. Is this the right way or is there any better way to bypass validation, workflows or processbuilder. Pls recommend the best or widely used one
    – SMorse
    Jul 16, 2019 at 22:11
  • I havent used custom metadata, i only used the list type custom setting
    – SMorse
    Jul 16, 2019 at 22:15


You must log in to answer this question.

Browse other questions tagged .