Best for simple enforcement without coding.
C# Plugin Code Sample:
---
public class ValidateOpportunityStage : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.MessageName != "Update" || !context.InputParameters.Contains("Target"))
return;
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "opportunity")
return;
// Retrieve field values
bool field1 = entity.Contains("new_field1") && (bool)entity["new_field1"];
bool field2 = entity.Contains("new_field2") && (bool)entity["new_field2"];
bool field3 = entity.Contains("new_field3") && (bool)entity["new_field3"];
bool field4 = entity.Contains("new_field4") && (bool)entity["new_field4"];
// Prevent stage change if any field is "No"
if (!field1 || !field2 || !field3 || !field4)
{
throw new InvalidPluginExecutionException("All fields must be 'Yes' before moving to the next stage.");
}
}
}
---
Steps to Implement:
- Register the plugin using the Plugin Registration Tool.
- Attach it to the On Stage Change event of the Opportunity entity.
🔸 Limitations:
- Requires development effort.
- Enforces validation only when the record is saved.
Approach 3: JavaScript (OnSave Event – Client-Side Enforcement)
Best for real-time validation before saving.
How It Works:
- Checks the
Sales Stage field before saving.
- If the stage is not "Qualify", verifies that all four fields are "Yes".
- If any field is "No", prevents saving and displays an alert.
JavaScript Code Sample (OnSave Event):
---
function validateFieldsOnSave(executionContext) {
var formContext = executionContext.getFormContext();
// Get the Sales Stage field (Update with your actual field name)
var salesStage = formContext.getAttribute("sales_stage").getValue();
// Define the first stage value (update based on your system's values)
var firstStageValue = "Qualify";
// Only run validation if the stage is NOT the first stage
if (salesStage !== firstStageValue) {
var field1 = formContext.getAttribute("new_field1").getValue();
var field2 = formContext.getAttribute("new_field2").getValue();
var field3 = formContext.getAttribute("new_field3").getValue();
var field4 = formContext.getAttribute("new_field4").getValue();
// Check if all fields are "Yes" (true)
if (field1 !== true || field2 !== true || field3 !== true || field4 !== true) {
// Prevent form save
executionContext.getEventArgs().preventDefault();
alert("All fields must be 'Yes' before moving to the next stage.");
}
}
}
---
🔸 Steps to Implement:
- Upload the script as a JavaScript Web Resource in CRM.
- Open the Opportunity Form, go to Form Properties.
- Attach the script to the OnSave event.
- Save & Publish the form.
🔸 Limitations:
Users can still bypass by modifying fields after stage transition.
Approach 4: Business Rules (No Code – UI Enforcement)
Best for making fields mandatory dynamically.
How It Works:
- Tracks the Sales Stage field.
- If the stage is not "Qualify", makes the four fields mandatory.
- This ensures users must update these fields before saving.
🔸 Steps to Implement Business Rule:
- Go to PowerApps > Opportunity Entity > Business Rules.
- Click New Business Rule and add these conditions:
- If Sales Stage ≠ "Qualify", then:
Set Field 1 → Mandatory
Set Field 2 → Mandatory
Set Field 3 → Mandatory
Set Field 4 → Mandatory
- Save & Activate the rule.
🔸 Limitations:
- Works only in UI forms, not in back-end/API updates.
I hope you will find your answer from these approaches.
If this helps you, please consider approving this answer.
Regards,
Nitesh Raj