Define Fields inside Editable Grid in Form of Model-Driven-App as required via JavaScript

173 Views Asked by At

I would like to define several columns in multiple editable grids in the same form of my model-driven-app as required. The sense behind this is that we have 2 different model-driven-apps for 2 different user-roles and for one specific role some fields should be required.

To reach this goal I would like to use the JavaScript approach via a webresource.

My first approach trying to avoid JavaScript unfortuantely does not work. I already tried to solve this via Power Automate flow and business rules but unfortunately this would cause a conflict because the business rule triggers too fast

In attachment my code which is unfortunately not working. I am still having problems to access the attribute in my editable grid. I have uploaded the function as webresource and embedded the webresource in the OnLoad-Event of the respective Form of the model-driven-app.

The following error is returned: repairActivityGridEntity.getEntityMetadata is not a function

Code:

function setRequiredFields(executionContext) {
    let formContext = executionContext.getFormContext();
    let repairActivityGridContext = formContext.getControl("RepairActivitys");
    let repairActivityGrid = repairActivityGridContext.getGrid();
    let repairActivityGridRows = repairActivityGrid.getRows();
    if (repairActivityGridRows.getLength() > 0) {
      let repairActivityGridData = repairActivityGridRows.get(0).getData();
      let repairActivityGridEntity = repairActivityGridData.getEntity();
      let repairActivityGridEntityMetadata = repairActivityGridEntity.getEntityMetadata();
      let repairActivityGridColumns = repairActivityGridEntityMetadata.Attributes;
      for (let i = 0; i < repairActivityGridColumns.getLength(); i++) {
        if (repairActivityGridColumns.get(i).LogicalName == "mca_overnightstaysindays") {
          repairActivityGridColumns.get(i).RequiredLevel = "required";
          break;
        }
      }
    }
    repairActivityGridContext.addOnLoad(setRequiredFields);
  }

I tried to solve it with JavaScript but it does not seem like that I could solve it to access an attribute in an editable grid on the OnLoad-event of my form. Maybe someone know how to achieve this/ what I am doing wrong.

1

There are 1 best solutions below

3
George R On

It returns that error because you are calling the wrong one. You need to call the function setRequiredFields,(and only that "setRequiredFields", nothing elese) as that is the one you created.

This should sort that error.