When working with Dynamics 365 Customer Engagement (CE), ensuring data integrity before records are saved is critical. Pre-validations help achieve this, but the choice between JavaScript (client-side) and Plugins (server-side) can be tricky.
So, when should you use JavaScript validations on forms versus server-side plugins in the Pre-Operation stage? Letβs break it down.
JavaScript Validations (Client-Side)
When to Use
- Immediate feedback for end users.
- Validations based on form-level data.
- Dynamic field visibility, error notifications, or mandatory field checks.
- Business rules that change frequently and are UI-driven.
Pros
- Instant user experience (no server round-trip).
- Easy to implement using form events (
onChange
,onSave
). - Interactive guidance (e.g., show/hide fields, notifications).
Cons
- Bypassed if records are updated via API, integrations, or mobile apps.
- Browser-dependent; not foolproof for critical validations.
- Complex logic in JavaScript can be hard to maintain.
Example
if (formContext.getAttribute("revenue").getValue() < 0) {
formContext.ui.setFormNotification("Revenue cannot be negative", "ERROR", "revenueValidation");
executionContext.getEventArgs().preventDefault();
}
Plugins for Pre-Validation (Server-Side)
When to Use
- Enforcing business rules universally, regardless of the interface.
- Critical validations where data integrity is non-negotiable.
- Scenarios involving cross-entity validation or data lookups.
Pros
- Cannot be bypassed (applies to UI, API, integrations, and imports).
- Centralized, reusable, and version-controlled.
- More powerful (can query Dataverse, handle complex logic).
Cons
- No immediate UI feedback; errors appear only after a save attempt.
- Slightly longer round-trip time.
- Requires deployment via solutions.
Example
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var target = (Entity)context.InputParameters["Target"];
if (target.Contains("revenue") && (decimal)target["revenue"] < 0)
{
throw new InvalidPluginExecutionException("Revenue cannot be negative.");
}
}
When to Use Both?
For the best user experience and data integrity, combine them:
- JavaScript: Provide user-friendly, instant feedback.
- Plugin Pre-Validation: Ensure integrity across all channels.
Quick Decision Flow
Scenario | Use JavaScript | Use Plugin |
---|---|---|
Form field validations | Yes | Maybe |
API or integration updates | No | Yes |
Cross-entity checks | No | Yes |
User experience improvements | Yes | No |
Critical business rules | Maybe | Yes |
Conclusion
- JavaScript: UX-friendly but limited in scope.
- Plugins: Robust and backend-safe but slower for feedback.
- Best Practice: Use JavaScript for soft validations and Plugins for enforcement.
Top comments (0)