[SalesForce] How to bypass validation rule for trigger in salesforce

I have a picklist field on opportunity, which contains some values for example: A, B, C, D

The user must be able to update A or C during record edit if he tries to edit values other than A and C validation rule fires.
But when trigger fires it should be able to update any of the values validation rule should not be fired.

I tried creating one flag on opportunity but this method will allow both system and user to edit the field.

So please provide any suggestions on this??

Best Answer

You can use a custom setting in the validation rule and change its value in the trigger, so that the validation rule does only fire during a user interaction. The idea is described here: Make Validation Rule bypass if TRIGGER is run?

Be aware that in a Salesforce org with a lot of traffic, this pattern could lead to sporadic locks on the custom setting.

Related Topic