Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Last active August 12, 2021 07:25
Show Gist options
  • Save olgaloza/37cb19ae45de3b14fa76c8f0d5d552cc to your computer and use it in GitHub Desktop.
Save olgaloza/37cb19ae45de3b14fa76c8f0d5d552cc to your computer and use it in GitHub Desktop.
RAD 2 Homework 4 - Write a new trigger and handler class from scratch
public class CaseTriggerHandler {
//Handler Methods
public static void onBeforeInsert(List<Case> newCases) {
//Set default Reason if not provided at case creation
for ( Case c : newCases) {
if (c.Reason == NULL) {
System.debug('---->Case reason is '+c.Reason);
c.Reason = 'Other';
}
else {
System.debug('---->Case reason is '+c.Reason);
}
}
}
public static void onAfterInsert(List<Case> newCases) {
}
public static void onBeforeUpdate(List<Case> oldCases, List<Case> updatedCases, Map<Id, Case> oldCaseMap, Map<Id, Case> updatedCaseMap) {
}
public static void onAfterUpdate(List<Case> oldCases, List<Case> updatedCases, Map<Id, Case> oldCaseMap, Map<Id, Case> updatedCaseMap) {
//Before closing the case, check if it has potential liability issues and create a Task for the Case Owner
//will require newMap because we'll need IDs to work on the related records
//need to bulkify in case records are updated in bulk
//create a list to collect all tasks we will create
List<Task> TasksList = new List<Task>();
//Loop through the Oppties we just updated and create a Task for each
for ( Case c : updatedCases) {
Case oldCase = oldCaseMap.get(c.Id);
System.debug('---->Old Case Status is '+oldCase.Status+' and IsClosed='+oldCase.IsClosed);
System.debug('---->New Case Status is '+c.Status+' and IsClosed='+c.IsClosed);
if(c.PotentialLiability__c == 'Yes' && c.IsClosed == TRUE && oldCase.IsClosed == FALSE) {
System.debug('---->Closing a Case with Potential Liability issues');
Task t = new Task();
t.OwnerId = c.OwnerId;//UserInfo.getUserId();
t.Description ='Case \"' +c.CaseNumber+ '\" was closed on ' + c.ClosedDate +'. Please follow up with the customer.';
t.Subject = 'Case with Potential Liability was closed';
t.WhatId = c.Id;
t.Status = 'Not Started';
t.ActivityDate = Date.Today();
System.debug('------> Added a Task');
TasksList.add(t);
}
}
//bulk insert the Tasks list
insert(TasksList);
System.debug('------> Created ' + TasksList.size() + ' Tasks.');
}
public static void onBeforeDelete(List<Case> casesToDelete, Map<Id, Case> deletedCaseMap) {
}
public static void onAfterDelete(List<Case> deletedCases, Map<Id, Case> deletedCaseMap) {
}
public static void onUndelete(List<Case> undeletedCases, Map<Id, Case> recoveredCaseMap) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment