Friday, 16 June 2017

{know-how} Editablegrid disable fields conditionally using Javascript - Dynamics CRM D365 - D365_EditableGrid#1


Editable grid is an awesome way to manage related child records effectively and efficiently within few clicks and I personally love it for the same. For this kind of requirements, building a custom grid was the story for long time and now Editable grid is a real savior.

Before deep diving into code , below are the supported JavaScript events for editable grid. 

Scenario: In account form we have contact editable grid. Whenever the account name is DisableFields(felt lazy to find another field :P), disable all the fields in the editable grid row.

Event Registration:
  1. Open form editor and then open editable grid properties
  2. Navigate to Events tab and select Event named OnRecordSelect
  3. Add a function to the event.While adding function do not forget to enable "Pass execution context as first parameter"


Code Explanation:
To access the current row(entity record) we need the editable grid's formcontext which is a part of the context passed as first parameter. From there the rest of the code is similar to our traditional js implementations. 

function setFieldsDisabled(context) {
    var name = Xrm.Page.getAttribute("name").getValue();
    if (name == "DisableFields") {
        var contact = context.getFormContext().data.entity;
        contact.attributes.forEach(function(field, i) {
            field.controls.get(0).setDisabled(true);
        });
    }
}

Hope this helps! 

4 comments:

  1. Hi Ramanathan and thanks for posting your very elegant solution. It will work for sub-grids that are dropped on the form. How about doing something similar on system views? For example, if I want to disable some columns from being edited on system views?

    Best regards,
    -TS.

    ReplyDelete
    Replies
    1. You can go to the solutions => entity => Events(will be in tabs - general, Primary field, controls, Events) and you can register Javascripts there

      Delete
  2. i want to disable particular field in the subgrid using javascript, please let me know

    ReplyDelete
    Replies
    1. I believe in the foreach loop of code you can pass your fields that needs to be disabled as an array. If the fields in grid is available in array then you can let the disable piece execute for those.

      Delete