Pre-Validation vs Pre-Operation Stages in Dynamics 365 Plugins Complete Understanding
Pre-Validation vs Pre-Operation Stages in Dynamics 365 Plugins
When writing plugins in Dynamics 365, choosing the right execution stage is critical. Two commonly used stages are Pre-Validation and Pre-Operation. Although both execute before the main operation is committed to the database, their behavior is different.
⚡ Pre-Validation Stage
-
Execution Context: Runs outside the database transaction.
-
Rollback: Since it’s outside the transaction, any runtime error will not rollback the entire operation.
-
Use Case: Ideal for implementing business logic that checks conditions before the database transaction starts. You can cancel the operation if needed.
-
Security: Runs before security checks, so user permissions aren’t validated yet.
-
Typical Usage:
-
Perform validations (e.g., duplicate detection).
-
Restrict operations before security checks.
-
-
Example: Checking if a duplicate record exists in the system before allowing creation.
⚡ Pre-Operation Stage
-
Execution Context: Runs inside the database transaction.
-
Rollback: Any runtime error here will rollback the entire transaction, including other plugin steps.
-
Use Case: Best when you need to modify target entity attributes or perform calculations within the transaction.
-
Security: Executes after security checks, meaning the user must already have the right permissions.
-
Typical Usage:
-
Update values in the entity being processed.
-
Perform operations that must be consistent with the transaction.
-
-
Example: Debiting an amount from one record and crediting it to another within the same transaction.
π Key Differences at a Glance
| Aspect | Pre-Validation | Pre-Operation |
|---|---|---|
| Transaction Scope | Outside transaction | Inside transaction |
| Rollback | ❌ No rollback on failure | ✅ Full rollback on failure |
| Security Checks | Before security check | After security check |
| When to Use | Validation, restricting operation | Modifying values, transactional logic |
| Example | Duplicate check | Debit/Credit logic |
✅ Rule of Thumb:
-
Use Pre-Validation for early validation & business rules.
-
Use Pre-Operation for modifying data safely within the transaction.
Comments
Post a Comment