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
Shawn Low 24Shawn Low 24 

Good Morning, My name is Shawn Low. I am just learning to read and write code for SFDC and I would like to ask for some help.

I am posting a Trigger that was written before I started at this company. Being new to coding, I dont fully understand what is happening in this trigger. I wanted to ask if someone could please break things down for me so I can "connect" what the code says and what action is happening.
I am hoping someone can use snagit or something like that, and box off each part of the code, and then type in the explaination of what that code is telling SFDC to so.
Thank you for all your help.
Here is the trigger code...

//=================================================================================================
// Company: TRI Pointe Group
//  Author: Matt Starr (EnablePath)
// Created: 07/22/2016   
// Comment: Task Trigger
//=================================================================================================
//          Date            Purpose
// Changes: 07/22/2016    Matt Starr (EnablePath) Created
//=================================================================================================

trigger TaskTrigger on Task (after delete, after insert, after undelete, 
                                    after update, before delete, before insert, before update) {

    //Trigger Handler
    TaskTriggerHandler handler = new TaskTriggerHandler();

    //Before Insert
    if (Trigger.isBefore && Trigger.isInsert && !OpportunityTriggerHandler.onInsert) {
        handler.OnBeforeInsert(Trigger.new);
    }

    //After Insert
    if (Trigger.isAfter && Trigger.isInsert && !OpportunityTriggerHandler.onInsert) {
        handler.OnAfterInsert(Trigger.new);
    }

    //Before Update
    if (Trigger.isBefore && Trigger.isUpdate && !OpportunityTriggerHandler.onInsert) {
        handler.OnBeforeUpdate(Trigger.newMap,Trigger.oldMap);
    }
    
    //After Update
    if (Trigger.isAfter && Trigger.isUpdate && !OpportunityTriggerHandler.onInsert) {
        handler.OnAfterUpdate(Trigger.newMap,Trigger.oldMap);
    }

    

}
Kai TribbleKai Tribble

Not familiar with Snagit, but I'll take bits and pieces of your code and help you step through them:

trigger TaskTrigger on Task (after delete, after insert, after undelete, 
                                    after update, before delete, before insert, before update) {


This is the initial declaration of the Trigger. Any time a Task object is deleted, created, undeleted, or updated, the code underneath it runs.

//Trigger Handler
    TaskTriggerHandler handler = new TaskTriggerHandler();

    //Before Insert
    if (Trigger.isBefore && Trigger.isInsert && !OpportunityTriggerHandler.onInsert) {
        handler.OnBeforeInsert(Trigger.new);
    }

    //After Insert
    if (Trigger.isAfter && Trigger.isInsert && !OpportunityTriggerHandler.onInsert) {
        handler.OnAfterInsert(Trigger.new);
    }

    //Before Update
    if (Trigger.isBefore && Trigger.isUpdate && !OpportunityTriggerHandler.onInsert) {
        handler.OnBeforeUpdate(Trigger.newMap,Trigger.oldMap);
    }
    
    //After Update
    if (Trigger.isAfter && Trigger.isUpdate && !OpportunityTriggerHandler.onInsert) {
        handler.OnAfterUpdate(Trigger.newMap,Trigger.oldMap);
    }
The comments state when each section is run. Before that, an instance of the TaskTriggerHandler class is created. That's the class you'd probably be interested in the most, looking for the OnBeforeInsert, OnAfterInsert, OnBeforeUpdate, and OnAfterUpdate methods within that class. You're passing in the records that are impacted each time those methods run.
Shawn Low 24Shawn Low 24
Hi Kal,
Thank you so much for that. So basically, all this trigger is doing is telling the "TaskTrigerHandler" Class to run, if any of the mentioned things happen (basically, a Task is edited or created). Is that correct?