Thursday, July 10, 2014

Setting Profile Field Level Security (FLS) using JavaScript + Salesforce

Have you ever had to set FLS settings on Account for over 500 fields for a particular profile ?
I'm sure you had encountered this situation and we all know what a pain it can become. Manually checking each and every checkbox.
Well, here I'm with an easy solution. With a little bit of JavaScript magic we can get this task done in seconds. Here I've mentioned two JavaScript code snippets which can be run in the console of and get this task done in no time.
Run the below Code Snippet in Console of Firefox or Chrome to check all the check boxes of Visible type.
var elem = document.getElementsByTagName("tr");
for (var i = 0; i < elem.length; i++) {
    for (var j = 0; j < elem[i].childNodes.length; j++) {
        if (elem[i].childNodes[j].nodeName == "TD" && elem[i].childNodes[j].firstChild.nodeName == 'INPUT'
            && elem[i].childNodes[j].firstChild.type == 'checkbox' && elem[i].childNodes[j].firstChild.title == 'Visible') {
            var checkTD = elem[i].childNodes[j].firstChild.checked = true;
        }
    }
}
Run the below Code Snipped in Console of Firefox or Chrome to check all the check boxes of Read-Only type.
var elem = document.getElementsByTagName("tr");
for (var i = 0; i < elem.length; i++) {
    for (var j = 0; j < elem[i].childNodes.length; j++) {
        if (elem[i].childNodes[j].nodeName == "TD" && elem[i].childNodes[j].firstChild.nodeName == 'INPUT' && elem[i].childNodes[j].firstChild.type == 'checkbox'
            && elem[i].childNodes[j].firstChild.title == 'Read-Only') {
            var checkTD = elem[i].childNodes[j].firstChild.checked = true;
        }
    }

}
To Uncheck the Checkboxes just change the last line in above snippets to
var checkTD = elem[i].childNodes[j].firstChild.checked = false;

I hope this will get rid of a major pain point that us Developers and Administrators had to encounter on a daily basis.

Thanks
Enjoy Coding....