0

I have one validation rule which is checking account. Type == 'other' then throw error.

I have before update trigger which is updating account. Type ="technical"

when i deactivated the trigger then validation is firing, but not when my before trigger is active. How to run it.please suggest

1
  • As you said there is a custom validation rule by order of execution the custom validations run after running the before triggers and this is something that make sure the process is maintained to learn more you can try checking this link: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Anutej
    Oct 22, 2020 at 12:10

1 Answer 1

2

Validation rules are run after the before triggers have finished1, and there's no way to change that. It's part of the trigger order of execution.

If your before trigger is indiscriminately changing the Account Type, then of course your validation rule won't catch anything (the trigger has changed the field value away from the problematic value before the validation is executed). Your options here are to:

  • check to see if the Account Type is "Other", and only change it if it is not (though this will cause the entire DML operation to fail, in normal circumstances, when you reach validation if any record is "Other")
  • Rely on the validation rule to do its job when the Account Type is first being changed to "Other" (i.e. detect this change when it happens, not sometime after it happens)

The job of validation rules is to prevent bad data from being saved. If you already have bad data, you'll need to use some other tool (data loader, anonymous apex, etc...) to fix it.

1: For most objects at least, OpportunityLineItem and QuoteLineItem appear to be exceptions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .