Exception Handling & Logging in Dynamics CRM

 

Exception Handling in Dynamics CRM

🔧 Where Exceptions Can Occur

LayerExamples
PluginsInvalid data, missing fields, service errors
Custom Workflow ActivitiesLogic failures, bad input
JavaScript Web ResourcesNull references, failed API calls
Power Automate / Logic AppsAPI timeouts, auth errors
Azure IntegrationsNetwork failures, bad responses

✅ Exception Handling Best Practices

1. Plugins & Custom Code


try { // Business logic } catch (InvalidPluginExecutionException ex) { tracingService.Trace("Plugin Exception: {0}", ex.ToString()); throw; // Rethrow so CRM handles it as business error } catch (Exception ex) { tracingService.Trace("Unexpected Error: {0}", ex.ToString()); throw new InvalidPluginExecutionException("An error occurred in plugin", ex); }
  • Always use ITracingService for 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)

try { // Logic } catch (e) { console.error("Form error: ", e.message); Xrm.Navigation.openAlertDialog({ text: "An error occurred." }); }
  • 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

MethodWhere it AppliesUsage
ITracingServicePlugins, workflowsLogs internal logic and errors
Trace Logs in Plugin Registration ToolPluginsView runtime logs for failed executions
Application InsightsAzure Functions, APIsCentralized logging, telemetry, alerts
Power Automate Run HistoryFlowsBuilt-in step-level diagnostics
JavaScript ConsoleClient-side formsLogs messages/errors to browser
Audit HistoryCRM entitiesShows who changed what and when
Custom Log EntityAny layerPersist errors to Dataverse for tracking

🧠 Best Practices Summary

PracticeWhy It Matters
Always use ITracingServiceHelps troubleshoot plugin errors in CRM
Use try-catch everywherePrevents crash and improves resilience
Rethrow exceptions properlyShows meaningful error to end user
Log to App Insights / custom log tablesCentralizes diagnostics
Build alerting & monitoring (Ops)Detect and respond to issues quickly
Avoid showing technical errors to end usersBetter UX and security

🚫 Common Mistakes

  • Catching exceptions but not tracing or logging them

  • Throwing vague exceptions (throw new Exception("error"))

  • Relying only on CRM UI error messages for diagnostics

  • Hardcoding sensitive details in error messages


🎯 Interview Readiness

Be ready to:

  • Walk through how you’ve debugged a production error (e.g., plugin exception).

  • Explain your tracing and logging setup.

  • Describe how you'd monitor failures across Power Platform + Azure stack.