Write-once data - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

Write-once data

Your auditors will love this trick ;-)

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 c03863039 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;
            }
        }
    }
}

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez