In this course, we will learn to lock and unlock the fields using JavaScript in Microsoft Dynamics CRM. Before we start make sure to go through the below links.
- Understanding JavaScript in Dynamics CRM
- How to use JavaScript in Dynamics CRM?
- Using formContext in JavaScript
- Get & Set value in fields using JavaScript
- Hide / Show fields using JavaScript
- Hide / Show tabs using JavaScript
In Microsoft Dynamics 365, we can dynamically lock (enable) and unlock (disable) fields with help of JavaScript.
JavaScript snippet for locking a field
We will use the property called “setDisable” to lock a particular field dynamically as shown below. In the below example, we will lock the field “Account Address” when field “Account Name” is empty.
function LockUnlock(executionContext) { //Initiated Form Context. var formContext = executionContext.getFormContext(); //Getting Value From Field Account Name. var AccountName = formContext.getAttribute("accountname").getValue(); //Condition If Account Name Is Null. if (AccountName === null || AccountName === undefined) { //Using SetVisible propertly for locking field Account Address. formContext.getControl("accountaddress").setDisabled(true); }
JavaScript snippet for unlocking a field
We will use the property called “setDisable” to unlock a particular field dynamically as shown below. In the below example, we will unlock the field “Account Address” when field “Account Name” is having valid data.
function LockUnlock(executionContext) { //Initiated Form Context. var formContext = executionContext.getFormContext(); //Getting Value From Field Account Name. var AccountName = formContext.getAttribute("accountname").getValue(); //Condition If Account Name Is Null. if (AccountName !== null || AccountName !== undefined) { //Using SetVisible propertly for unlocking field Account Address. formContext.getControl("accountaddress").setDisabled(false); } }
Thus, we learned how to lock and unlock fields using JavaScript in Microsoft Dynamics 365 CRM.
CRM Crate
All In One Platform For Learning Microsoft CRM.