Tuesday, May 24, 2011

JavaScript Hack to Set Visibility for Field-Level Security

Having to click numerous (sometimes over 100) checkboxes just to grant or revoke visibility to certain fields via Field-Level Security settings can be a pain. Here's a JavaScript hack that can mark all of the "Visible" checkboxes on the Field-Level Security edit page.

The following code was tested in Firefox 4 and Chrome 11 on Windows 7. It can probably be adapted easily to work for marking the read-only checkbox and for clearing checkboxes as well.

javascript:

/* Locate the form. */

var flsForm =
    document.getElementById("editPage");

/* Find the form inputs. */

var inputs =
    document.getElementsByTagName("input");

var visibleInputs = new Array();
for(var i = 0; i < inputs.length; i++) {
  var titleAttr = inputs[i].attributes["title"];
  if(titleAttr != null) {
    if(titleAttr.value == "Visible") {
      var visibleInput = inputs[i];
      visibleInputs[visibleInputs.length] =
          visibleInput;
    }
  }
}

/* Mark all checkboxes. */

for(var i = 0; i < visibleInputs.length; i++) {
  var eName =
      visibleInputs[i].attributes["name"].value;
  
  var e = flsForm.elements[eName];
  
  if(!e.checked)
    e.click();
}