Exception Handling in Dynamics CRM
🔧 Where Exceptions Can Occur
| Layer | Examples |
|---|---|
| Plugins | Invalid data, missing fields, service errors |
| Custom Workflow Activities | Logic failures, bad input |
| JavaScript Web Resources | Null references, failed API calls |
| Power Automate / Logic Apps | API timeouts, auth errors |
| Azure Integrations | Network failures, bad responses |
✅ Exception Handling Best Practices
1. Plugins & Custom Code
-
Always use
ITracingServicefor internal diagnostics. -
Never swallow exceptions silently.
-
Rethrow exceptions with meaningful error messages.
2. Custom Workflow Activities
Same as plugins—catch exceptions and use TracingService for diagnostics.
3. JavaScript (Client-Side)
-
Avoid hard crashes in the UI.
-
Log to browser console or use telemetry tools like Application Insights.
4. Power Automate Flows
-
Use Scope + Run After pattern for error handling.
-
Add parallel branches for “On Failure” and log to:
-
Dataverse
-
Email / Teams
-
Azure Monitor (via custom connector)
-
5. Azure Functions & Integrations
-
Use structured logging with Application Insights.
-
Implement retries and circuit breakers.
-
Send failed events to Service Bus dead-letter queue.
🔍 Tracing & Logging Options
| Method | Where it Applies | Usage |
|---|---|---|
| ITracingService | Plugins, workflows | Logs internal logic and errors |
| Trace Logs in Plugin Registration Tool | Plugins | View runtime logs for failed executions |
| Application Insights | Azure Functions, APIs | Centralized logging, telemetry, alerts |
| Power Automate Run History | Flows | Built-in step-level diagnostics |
| JavaScript Console | Client-side forms | Logs messages/errors to browser |
| Audit History | CRM entities | Shows who changed what and when |
| Custom Log Entity | Any layer | Persist errors to Dataverse for tracking |
🧠 Best Practices Summary
| Practice | Why It Matters |
|---|---|
Always use ITracingService | Helps troubleshoot plugin errors in CRM |
| Use try-catch everywhere | Prevents crash and improves resilience |
| Rethrow exceptions properly | Shows meaningful error to end user |
| Log to App Insights / custom log tables | Centralizes diagnostics |
| Build alerting & monitoring (Ops) | Detect and respond to issues quickly |
| Avoid showing technical errors to end users | Better UX and security |