Hide and Show Field Based on Field Value in Dynamics 365
Hide and Show Field Based on Field Value in Dynamics 365
Dynamics 365 allows developers to create dynamic and user-friendly forms using JavaScript. One of the most common customization requirements is showing or hiding fields based on the value selected in another field.
This functionality helps simplify forms, improves user experience, and ensures users only see fields that are relevant to their current business process.
Business Scenario
Suppose a Customer Type field contains the following values:
- Individual
- Business
When Business is selected, additional fields such as Company Name and GST Number should become visible. Otherwise, those fields should remain hidden.
Benefits of Dynamic Field Visibility
- Cleaner forms
- Better user experience
- Reduced confusion
- Improved data quality
- Faster record creation
Create JavaScript Function
function hideShowFields(executionContext)
{
var formContext =
executionContext.getFormContext();
}
Get Field Value
var customerType = formContext.getAttribute( "new_customertype") .getValue();
Show or Hide Fields
if(customerType == 100000001)
{
formContext.getControl(
"new_companyname")
.setVisible(true);
formContext.getControl(
"new_gstnumber")
.setVisible(true);
}
else
{
formContext.getControl(
"new_companyname")
.setVisible(false);
formContext.getControl(
"new_gstnumber")
.setVisible(false);
}
Register the Function
- Create a JavaScript Web Resource.
- Upload the script.
- Add the library to the form.
- Register the function on Form OnLoad.
- Register the function on Field OnChange.
- Save and Publish customizations.
Common Use Cases
- Customer type selection
- Employee onboarding forms
- Vendor management forms
- Approval workflows
- Project management forms
- Service request forms
Best Practices
- Keep form logic simple.
- Use meaningful field names.
- Handle null values properly.
- Test all visibility scenarios.
- Document custom JavaScript logic.
Key Takeaways
- JavaScript can dynamically control field visibility.
- Forms become cleaner and easier to use.
- Users see only relevant information.
- Improves data quality and productivity.
- Commonly used in Dynamics 365 form customizations.