DEV Community

Prakash Kalimuthu
Prakash Kalimuthu

Posted on Edited on

Dissecting Dataverse Plug-ins, Part 1: PreValidation Doesn't Always Run Outside the Transaction (Check IsInTransaction)

Kept thinking PreValidation always runs outside the database transaction in Dataverse.

Then I tested the same Account Create plugin through two paths.

Direct API create:

  • PreValidation, Stage 10: IsInTransaction = False
  • PreOperation, Stage 20: IsInTransaction = True
  • PostOperation (sync mode), Stage 40: IsInTransaction = True

Model-driven app form save with change set (BPF attached to the table):

  • PreValidation, Stage 10: IsInTransaction = True
  • PreOperation, Stage 20: IsInTransaction = True
  • PostOperation (sync mode), Stage 40: IsInTransaction = True

Same table. Same message. Same registered steps.

The difference is the caller path and whether the pipeline already has a transaction active when it reaches PreValidation.

That is why Stage alone is not enough. IPluginExecutionContext.IsInTransaction tells you what actually matters for that execution.

PreValidation usually runs before the main transaction, but it can run inside an existing transaction when another operation initiates it. A model-driven app form save can follow a different platform path from a plain Web API create.

This also matters for error handling.

When context.IsInTransaction is true and the plugin throws an exception, Dataverse rolls back the work in that active transaction.

So in the model-driven app save, an exception thrown at PreValidation was already inside a transaction. In the direct create, PreValidation was outside the transaction, but by PreOperation the work was inside it.

One small clarification: not every caught error causes a rollback. The exception has to reach the platform. If your code handles it and completes normally, Dataverse treats the step as successful.

The practical lesson is simple:

Don't assume. Check context.IsInTransaction.

If your plugin has logic that depends on transaction behavior, branch on the runtime value, not on the stage name.

Top comments (0)