You need to sign in to do that
Don't have an account?
DannyK89
Optimizing a Trigger
I created this trigger to update and populate some fields in a task record. I was wondering if anyone could see a way that I could optimize the code so that it runs more efficiently. Thanks.
Trigger:
rigger Task_Update_Trigger on Task (before update) { List<Task> newTask = trigger.new; List<Task> oldTask = trigger.old; List<User> userList = new List<User>(); List<Contact> contactList = new List<Contact>(); DateTime dt = system.now(); Date day = system.now().date(); Date EndofWeek = system.today().toStartOfWeek() + 5; String str = dt.format('MM/dd'); userList = [SELECT Name, Id, Alias FROM User WHERE Id =: newTask[0].OwnerId]; contactList = [SELECT Name, Id FROM Contact WHERE Id =: newTask[0].WhoId]; for(Task T : newTask){ for(Task O : oldTask){ for(User U : userList){ for(Contact C : contactList){ if(T.OwnerId == U.Id && T.Type == 'TAP' && T.TAP_Result__c == 'No Connection' && T.WhoId == C.Id){ if(T.TAP_Stage__c == 'Called/1st VM Left' && O.TAP_Stage__c != 'Called/1st VM Left'){ if(day == EndofWeek){ T.ActivityDate = system.today() + 3; } else { T.ActivityDate = system.today() + 1; } if(T.Description == '' || T.Description == null){ T.Description = str + ' ' + U.Alias + ' - Called and left a vm (1) for ' + C.Name; } else{ T.Description = str + ' ' + U.Alias + ' - Called and left a vm (1) for ' + C.Name + '\n\n' + T.Description; } T.CallDisposition = 'Called and left a vm'; T.Activity_Classification__c = 'Voice Message'; } if(T.TAP_Stage__c == 'Called/No VM (1)' && O.TAP_Stage__c != 'Called/No VM (1)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Called and did not leave a vm (1) for ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Called and did not leave a vm'; T.Activity_Classification__c = 'Call Attempt'; } if(T.TAP_Stage__c == 'Email (1)' && O.TAP_Stage__c != 'Email (1)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Sent Email(1) to ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Sent Email(1)'; T.Activity_Classification__c = 'Email'; } if(T.TAP_Stage__c == 'Called/2nd VM Left' && O.TAP_Stage__c != 'Called/2nd VM Left'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Called and left a vm (2) for ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Called and left a vm'; T.Activity_Classification__c = 'Voice Message'; } if(T.TAP_Stage__c == 'Called/No VM (2)' && O.TAP_Stage__c != 'Called/No VM (2)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Called and did not leave a vm (2) for ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Called and did not leave a vm'; T.Activity_Classification__c = 'Call Attempt'; } if(T.TAP_Stage__c == 'Email (2)' && O.TAP_Stage__c != 'Email (2)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Sent Email(2) to ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Sent Email(2)'; T.Activity_Classification__c = 'Email'; } if(T.TAP_Stage__c == 'Called/3rd VM Left' && O.TAP_Stage__c != 'Called/3rd VM Left'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Called and left a vm (3) for ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Called and left a vm'; T.Activity_Classification__c = 'Voice Message'; } if(T.TAP_Stage__c == 'Called/No VM (3)' && O.TAP_Stage__c != 'Callec/No VM (3)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Called and did not leave a vm (3) for ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Called and did not leave a vm'; T.Activity_Classification__c = 'Call Attempt'; } if(T.TAP_Stage__c == 'Email (3)' && O.TAP_Stage__c != 'Email (3)'){ if(day == EndofWeek){ T.ActivityDate += 3; } else { T.ActivityDate += 1; } T.Description = str + ' ' + U.Alias + ' - Sent Email(3) to ' + C.Name + '\n\n' + T.Description; T.CallDisposition = 'Sent Email(3)'; T.Activity_Classification__c = 'Email'; } if(T.TAP_Stage__c == 'Final VM/Email' && O.TAP_Stage__c != 'Final VM/Email'){ T.Description = str + ' ' + U.Alias + ' - Final VM and Email completed\n\n' + T.Description; T.CallDisposition = 'Called and left a vm / emailed'; T.Activity_Classification__c = 'Voice Message'; } } } } } } }
Lots of scope for optimization in this. This trigger is not bulkified. You have used "newTask[0].". If somebody updates the tasks from Data loader or in bulk, this trigger wont work as it only processes the first record in the list.
4 FOR loops is definately not a good idea. The same logic can be done using Maps. Also what exactly are you trying to accomplish here?
All Answers
Lots of scope for optimization in this. This trigger is not bulkified. You have used "newTask[0].". If somebody updates the tasks from Data loader or in bulk, this trigger wont work as it only processes the first record in the list.
4 FOR loops is definately not a good idea. The same logic can be done using Maps. Also what exactly are you trying to accomplish here?
Ok, I would like to update a group of fields after a user updates just one field. I know my code isn't the best mostly because I'm kind of new to this stuff.
Ok so if you want can you check my code again. I know its a pain but I just want to know whay you think. Did i make it better?
Great job in optimizating the trigger from what it was earlier.
One more thing, if you can use ELSE IF for all your IFs where you are checking for the stage. This will avoid execution of all the IF statements everytime you run the code.
Thanks for all the help