Wednesday, November 12, 2014

Salesforce + Finding out "How deleted out the records"

If you need to find out who is deleting your records, you can write this code on the Execute Anonymous block (either on the force.com IDE or the Force.com Console):

for (List<Account> Accs : [SELECT Id, LastModifiedByID, LastModifiedBy.userName, LastModifiedBy.name, LastModifiedDate, Name
                           FROM Account
                           WHERE IsDeleted = true
                           ALL ROWS]) {
    for (Account acc : Accs) {
        String str = '';
        str += 'Account [' + Acc.Name + '] ';
        str += 'was deleted by [' + acc.LastModifiedBy.name + ' ';
        str += '(' + acc.LastModifiedBy.userName + ')] ';
        str += 'on [' + acc.LastModifiedDate + ']';
        System.debug(str);
    }
}

You could also perform a similar operation using Data Loader (v20 or later) by clicking on the [Export All] button and using this query:

SELECT Id, LastModifiedByID, LastModifiedBy.userName, LastModifiedBy.name, LastModifiedDate, Name
FROM Account
WHERE IsDeleted = true

This works in Data Loader because it uses the QueryAll() method when connecting to Salesforce using the Webservices API.

Trigger + Write Once Data for Field + Salesforce

This trigger will prevent records from being deleted or fields from being changed once they have been set, so they are write-once. Note, the other fields can be written many times but the specific field's value can only be set when the record is created.

trigger Trigger_Account on Account (before delete, before update) {

    if (trigger.isDelete) {
        for (Account a : trigger.old) {
            a.addError('You can’t delete accounts');
        }
    } else {
        for (Account aNew : trigger.new) {
            Account aOld = Trigger.oldMap.get(aNew.ID);
            if (aNew.Phone != aOld.Phone) {
                aNew.Phone = aOld.Phone;
            }
        }
    }
}

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....