function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash RaviAvinash Ravi 

Need help understanding this piece of code

Can someone help me understand the flow on this codeblock? I'm still a newbie so any help on the flow would be greatly apprecieated
 
trigger AptsActivityHistoryTrigger on Task (before delete, before update) {
    String ERROR_MESSAGE = 'Update/Delete of Activity History entries are not permitted';
    String PROFILE_SYS_ADMIN = 'System Administrator';
    Id sysAdminProfileId=null;
    // Bulk processing NOT supported
    if (Trigger.size != 1) {
        return;
    } 
    //applicable for update and deletes only
    if (Trigger.isBefore) {
        if ((Trigger.isUpdate) || (Trigger.isDelete)) {
            Task taskObj = [select Id, whatId, status from Task where Id = :Trigger.old[0].Id];
            
            // parent is an agreement?
            List <Apttus__APTS_Agreement__c> agmts = [select Id from Apttus__APTS_Agreement__c where Id = :taskObj.whatId];

            // sys admin profile id
            List<Profile> profileList = [select Id, Name 
                            from Profile
                            where Name = :PROFILE_SYS_ADMIN];
                            
            if ((profileList != null) && (!profileList.isEmpty())) {    
                sysAdminProfileId = profileList[0].Id;
            }   

            if (agmts == null || agmts.size()<=0 || taskObj.Status != 'Completed' || sysAdminProfileId == UserInfo.getProfileId() ) {
                return;
            }
            
            //error to indicate action is not permitted
            String errMsg = ERROR_MESSAGE;            
            if (Trigger.isUpdate) {
                Trigger.new[0].addError(errMsg);
            } else {
                Trigger.old[0].addError(errMsg);
            }
        }       
    }
    return;

}

 
SonamSonam (Salesforce Developers) 
This trigger is built on Task aobject to screen each task that is updated or deleted.

The trigger checkes if the task is associtaed to the Aggreement object and if the user is not a system admin - it doesn't let the user delete or update the task.