Power Community

Power Community

Lock fields on Editable Grid using JavaScript in Dynamics 365 CRM

https://d365demystified.com/wp-content/uploads/2024/06/locked.png

At times, you use Editable Grid and by default – all the fields which are editable by the system are exposed and can be edited if they are present on the Editable Sub-grid. You might not want to allow all fields to be editable on the Editable Grid – so here’s how you can lock selected fields on the Editable Grid using JavaScript.

Use Case

Let’s look at the below example where by default, the fields are Editable because you are using an Editable Grid –

  1. You might want to lock the Status field since it shouldn’t be something end user should be able to change by themselves.
  2. So in such scenarios, you might want to keep most fields open but lock some of them on the Editable Grid itself. Let’s see how we can do it using JavaScript

JavaScript Code

Here’s a sample code which you can use to loop through all the fields you want to lock on the Editable Grid –

  1. Code within a class – [You can just use the lockFields function separately too.]

    oSubscriptionCustomization = {

    lockFields: function (executionContext) {
    var oFormContext = executionContext.getFormContext();
    if (oFormContext) {
    var arrFields = [“cft301_status”];
    var objEntity = oFormContext.data.entity;
    objEntity.attributes.forEach(function (attribute, i) {
    if (arrFields.indexOf(attribute.getName()) > -1) {
    let attributeToDisable = attribute.controls.get(0);
    attributeToDisable.setDisabled(true);
    }
    })
    }
    }
    };

  2. In the above code, I’m reading from an array called arrFields and all the fields listed in the array will be looped through and locked.
  3. Once my JS file is ready, I can open the Editable Grid control itself on the parent form and then loop for Events tab to register my function on the grid.
  4. In the Event tab, look for the UI Event called as OnRecordSelect
  5. Here, you can register your function. Since, I’m using class too – I’ll use classname.functionName
  6. Once done, save and publish your changes. Now, you’ll see that the field is locked when you select the record on the Editable Grid.

Hope this was useful!

Thank you!

This post was originally published on this site

- Advertisement -spot_img

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement - Advertisement

Latest News

Hands-On Challenge: Setting Up Omnichannel Voice in 30 Minutes with Azure Communication Services

I recently had the privilege of presenting at the European Power Platform Conference in Brussels, where I conducted two...

More Articles Like This

- Advertisement -spot_img