Labels

Saturday, January 6, 2018

JavaScript - Lock all fields on the form

As we all frequently use to get requirements to lock the whole form to stop the end user from editing the record.
There are some out of the box options available to achieve this but these out of box options will not fulfill the actual requirement completely:
  1. Create a business rule : you can easily create a business and use lock field feature however you will have to select all the field on the form one by one   for .e.g. if you have 50 fields on the form, you will have to add action for all of them.                                                                                             Moreover another limitation is if a field is removed from the form – your business rule will stop working.
  2. We all know if record status is set to inactive – form become un-editable but in our scenarios we want to keep the record active but un-editable.
Therefore, we come down to our final easy solution to tackle all problems above, the JavaScript.


function onload()
{
var Topic= Xrm.Page.getAttribute("subject").getValue();
if(Topic!=null)
{
DisableAllFields();
}
}

function DisableAllFields()

{

       Xrm.Page.ui.controls.forEach(function (control, index) {

           var controlType = control.getControlType();

           if (controlType != "iframe" && controlType != "webresource" && controlType != "subgrid")

           {

               control.setDisabled(true);

           }

       });

}


Above code takes the control of the form directly rather than of each field & locks all of them. 

Just call the function on Onload, I have used this on my form and it looks like below:












No comments:

Post a Comment