Hide And Show a Field in Dynamics 365 CRM Using JavaScript
Introduction:
In Dynamics 365, you can hide and show fields using JavaScript. This is useful if you have business logic that determines if fields are displayed or not to the user.
Implementation:
Let’s take an example where you would like to hide the Account field if the Contact field is populated.
![](https://cdn-images-1.medium.com/max/1000/1*emLpb-zAcoaVfODnnJRZJw.png)
Next, we can check if the Contact field is empty using getAttribute(“fieldname”).getValue(). If empty, we can hide the field using setVisible. Let’s add this to the change event of the Contact field:
function showAccountOnChange(executionContext)
{
formContext = executionContext.getFormContext();
if (formContext.getAttribute("parentcontactid").getValue() == null)
{
formContext.getControl("parentaccountid").setVisible(false);
}
else
{
formContext.getControl("parentaccountid").setVisible(true);
}
}
When Contact field is populated, the Account field is displayed:
![](https://cdn-images-1.medium.com/max/1000/1*CVZoriUHZ2UTZxpLSf1d8A.png)
When empty, the Account field is hidden and the field collapses (note this is v9 Dynamics 365):
![](https://cdn-images-1.medium.com/max/1000/1*2PE_RATMGQnqKOC2pNxkJw.png)
Hope this helps …