Lock and Unlock Fields by Using JavaScript in Dynamics 365

```html id="lock-unlock-fields-dynamics365"

Lock and Unlock Fields by Using JavaScript in Dynamics 365

Microsoft Dynamics 365 allows developers to control user interactions through JavaScript. One common requirement is to lock or unlock fields dynamically based on business rules, record status, user roles, or field values.

Using JavaScript, fields can be made read-only or editable at runtime without changing field-level security settings. The most commonly used method is setDisabled(), which enables developers to control whether a field can be edited.


Why Lock Fields?

Many organizations require fields to become read-only after specific actions.

Examples:

Example Workflow:

Draft Record
↓
Editable Fields
↓
Approval Completed
↓
Fields Locked

This prevents accidental changes.


Common Business Scenarios

Approval Process

Lock fields after manager approval.

Opportunity Management

Lock revenue fields when opportunity is closed.

HR Applications

Prevent editing after employee onboarding.

Service Requests

Lock completed tickets.

Project Management

Freeze project information after completion.


What is setDisabled()?

Dynamics 365 provides the setDisabled() method to control whether a field is editable.

When set to true, the field becomes read-only. When set to false, the field becomes editable again.

Syntax:


formContext.getControl("fieldname").setDisabled(true);

Lock Field:


formContext.getControl("name").setDisabled(true);

Unlock Field:


formContext.getControl("name").setDisabled(false);

Step 1: Create JavaScript Web Resource

Create a JavaScript web resource.

Example:

new_LockUnlockFields.js

Upload it into Dynamics 365.


Step 2: Create Function


function LockField(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    formContext
    .getControl("name")
    .setDisabled(true);
}

Result:

Name Field = Read Only

Step 3: Unlock Field


function UnlockField(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    formContext
    .getControl("name")
    .setDisabled(false);
}

Result:

Name Field = Editable

Lock Field Based on Status

Requirement:

Status = Approved
↓
Lock Fields

JavaScript:


function LockOnApproval(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    var status =
    formContext.getAttribute("statuscode")
    .getValue();

    if(status == 100000001)
    {
        formContext
        .getControl("name")
        .setDisabled(true);

        formContext
        .getControl("telephone1")
        .setDisabled(true);
    }
}

Lock Multiple Fields

Instead of writing multiple statements, use an array and loop through the fields.


function LockFields(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    var fields =
    [
        "name",
        "telephone1",
        "emailaddress1"
    ];

    fields.forEach(function(field)
    {
        formContext
        .getControl(field)
        .setDisabled(true);
    });
}

Output:

Name = Locked
Phone = Locked
Email = Locked

Unlock Multiple Fields


function UnlockFields(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    var fields =
    [
        "name",
        "telephone1",
        "emailaddress1"
    ];

    fields.forEach(function(field)
    {
        formContext
        .getControl(field)
        .setDisabled(false);
    });
}

Lock Based on Another Field Value

Business Requirement:

Priority = High
↓
Lock Budget Field

JavaScript:


function LockBudget(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    var priority =
    formContext.getAttribute("prioritycode")
    .getValue();

    if(priority == 2)
    {
        formContext
        .getControl("budgetamount")
        .setDisabled(true);
    }
}

Lock All Fields on Form

Sometimes all fields must become read-only.

Example:


function LockAllFields(executionContext)
{
    var formContext =
    executionContext.getFormContext();

    formContext.data.entity.attributes
    .forEach(function(attribute)
    {
        var control =
        formContext.getControl(
        attribute.getName());

        if(control)
        {
            control.setDisabled(true);
        }
    });
}

A common approach is looping through all attributes and disabling their controls.


Lock Business Process Flow Fields

For Business Process Flow fields:


formContext
.getControl(
"header_process_fieldname")
.setDisabled(true);

Similarly:


formContext
.getControl(
"header_process_fieldname")
.setDisabled(false);

Business Process Flow controls can also be locked or unlocked using the same method.


Register JavaScript

Navigate:

Solution
↓
Entity
↓
Forms
↓
Form Properties

Add:

JavaScript Web Resource

Register Function:

On Load

or

On Change

Pass:

Execution Context

Save and Publish.


Example Workflow

Record Opened
↓
Check Status
↓
Approved?
↓
Yes
↓
Lock Fields

Real-World Example

Project Approval System

Fields:

Project Name
Budget
Start Date
End Date

When:

Status = Approved

Workflow:

Approved
↓
Lock Fields
↓
Prevent Changes

Result:

Data integrity is maintained.


Best Practices


Benefits


Common Use Cases


Conclusion

Locking and unlocking fields using JavaScript is one of the most useful customization techniques in Dynamics 365.

By using the setDisabled() method, organizations can control user access to fields dynamically based on record status, business rules, approvals, and user interactions.

This improves data quality, reduces errors, and provides a better user experience across Dynamics 365 and Dataverse applications.

```