+ Start a Discussion
wcwill978wcwill978 

Bypass validation rule via trigger or exclude trigger on validation

Can anyone tell me if there is an easy way withipout having to use a visualforce page to bypass a validation rule on the account standdard object. I have an opportunity trigger that updates the Type field on an account. I also have a validation rule on that field that only allows users to select two values from the picklist. All the other values in the picklist are to be selected by the trigger. If the validtion rule is active then the trigger will not work because the trigger runs as the user and the validation rule kicks in before the trigger and does not allow the update. The only way i can get it to work is by deactivating the validation rule.

 

Is there a way that i can say on the validation rule to only fire when its a user and not via a trigger, or on the trigger can i say to bypass that validation rule?

 

Please help I have tried support and they suggested the visualforce page, but I do not want to use a custom page for a standard object just to do this.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

We've done something along these lines using fields that aren't present on the page but are referenced by the validation rule.  When the change is made by a trigger, the field value is set so that the validation rule doesn't error.  Then we usually have some workflow or a further trigger that resets the field to a value where the validation rule won't ignore the change.

All Answers

bob_buzzardbob_buzzard

We've done something along these lines using fields that aren't present on the page but are referenced by the validation rule.  When the change is made by a trigger, the field value is set so that the validation rule doesn't error.  Then we usually have some workflow or a further trigger that resets the field to a value where the validation rule won't ignore the change.

This was selected as the best answer
wcwill978wcwill978

Hi Bob,

 

Do you some examples of this, I'm trying to understand it. So you are saying the trigger will update the correct (visible) field and the user is also able to update this field but have a secondary field in the backend with workflow that is referenced on the validation rule?

 

Im going to try this out now and see if it works. Thanks for the idea!!!

bob_buzzardbob_buzzard

That's the concept - I'm having a look to see if I've got any examples of this in my current workspace.  No joy as yet I'm afraid.

wcwill978wcwill978

Thanks Bob,

 

This worked I ended up creating a new picklist field with three values, one for the validation rule, another one for a second validation rule, and a third for the trigger. In the trigger i first set the field to "Trigger Edit", this allows me to bypass the validation rule via the trigger, then i have workflow and field updates that set the value on that field again based on the type of the account when the trigger has executed, so far working great. Thanks.