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
DannyK89DannyK89 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

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

MiddhaMiddha

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?

This was selected as the best answer
DannyK89DannyK89

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.

DannyK89DannyK89

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?

 

trigger Task_Update_Trigger on Task (before update) {
    DateTime dt = system.now();
    Date day = system.now().date();
    Date EndofWeek = system.today().toStartOfWeek() + 5;
    String str = dt.format('MM/dd');
    List<Id> OwnerId = New List<Id>();
    List<id> ContactId = new List<Id>();
    for(Task N : Trigger.new){
        OwnerId.add(N.OwnerId);
        ContactId.add(N.WhoId);
    }
    map<Id, User> usermap = new map<Id, User>([SELECT Name, Id, Alias FROM User WHERE Id IN :OwnerId]);
    map<Id, Contact> contactmap = new map<Id, Contact>([SELECT Name, Id FROM Contact WHERE Id IN :ContactId]);
    for(Task T : Trigger.new){
         if(T.Type == 'TAP' && T.TAP_Result__c == 'No Connection'){
             if(T.TAP_Stage__c == 'Called/1st VM Left' && Trigger.oldMap.get(T.Id).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 + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and left a vm (1) for ' + contactmap.get(T.WhoId).Name;
                 }
                 else{
                     T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and left a vm (1) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Called/No VM (1)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and did not leave a vm (1) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Email (1)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Sent Email(1) to ' + contactmap.get(T.WhoId).Name  + '\n\n' + T.Description;
                 T.CallDisposition = 'Sent Email(1)';
                 T.Activity_Classification__c = 'Email';
             }
             if(T.TAP_Stage__c == 'Called/2nd VM Left' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Called/2nd VM Left'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and left a vm (2) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Called/No VM (2)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and did not leave a vm (2) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Email (2)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Sent Email(2) to ' + contactmap.get(T.WhoId).Name + '\n\n' + T.Description;
                 T.CallDisposition = 'Sent Email(2)';
                 T.Activity_Classification__c = 'Email';
             }
             if(T.TAP_Stage__c == 'Called/3rd VM Left' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Called/3rd VM Left'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and left a vm (3) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Callec/No VM (3)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Called and did not leave a vm (3) for ' + contactmap.get(T.WhoId).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)' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Email (3)'){
                 if(day == EndofWeek){
                     T.ActivityDate = system.today() + 3;
                 }
                 else
                 {
                     T.ActivityDate = system.today() + 1;
                 }
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Sent Email(3) to ' + contactmap.get(T.WhoId).Name + '\n\n' + T.Description;
                 T.CallDisposition = 'Sent Email(3)';
                 T.Activity_Classification__c = 'Email';
             }   
             if(T.TAP_Stage__c == 'Final VM/Email' && Trigger.oldMap.get(T.Id).TAP_Stage__c != 'Final VM/Email'){
                 T.Description = str + ' ' + usermap.get(T.OwnerId).Alias + ' - Final VM and Email completed\n\n' + T.Description;
                 T.CallDisposition = 'Called and left a vm / emailed';
                 T.Activity_Classification__c = 'Voice Message';
             }
        }
    }
}

 

MiddhaMiddha

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.

DannyK89DannyK89

Thanks for all the help