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
Allison Humphrey 7Allison Humphrey 7 

Help - I've messed up a Trigger and it's not working now - Pretty sure it has to do with the catch exception at the end, but I can't figure it out.

I was adding in an additional section & I think I messed up the ending with the Catch exception.  It was working fine before I added in the section for Creative BQA & not it's not firing for any of the items. I think I erased some part of the catch exception & didn't get it correct.  I was able to get it to where it would save, but it doesn't seem to be firing to make the changes requested.  
ANY help would be greatly appreciated.
Thanks!
Here is my code: 

trigger TaskTrigger on Task (before insert) {

    try{
    boolean active = [Select Active__c from TriggerOptions__c where Name = 'DisplayTaskTrigger' limit 1].Active__c;
    
    
   
    if(active==true){
        for(Task t:Trigger.New){
            try{
                // if Subject = "AO - Advertiser Tagging & QA" then recordtype is "Ad Tag & QA"
                ID adTagQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Ad Tag & QA').getRecordTypeId();
                if(t.Subject=='AO - Advertiser Tagging & QA'){
                    t.RecordTypeId=adTagQATypeID;
                    if(Schema.SObjectType.Task.isUpdateable()){
                         update t;
                    }
                   
                }
                
                // if Subject = "AO - Technical QA  " then recordtype is "Technical QA"
                ID technicalQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Technical QA').getRecordTypeId();
                if(t.Subject=='AO - Technical QA'){
                    t.RecordTypeId=technicalQATypeID;
                    if(Schema.SObjectType.Task.isUpdateable()){
                         update t;
                    }
                   
                }
                
                // if Subject = "AO - Setup Network Pixels" then recordtype is "Pixel Setup"
                ID pixelSetupTypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Pixel Setup').getRecordTypeId();
                if(t.Subject=='AO - Setup Network Pixels'){
                    t.RecordTypeId=pixelSetupTypeID;
                    if(Schema.SObjectType.Task.isUpdateable()){
                        update t;
                    }
                } 
                 // if Subject = "AM - Creative BQA" then recordtype is "Creative BQA"
                ID creativeBQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Creative BQA').getRecordTypeId();
                if(t.Subject=='AM - Creative BQA'){
                    t.RecordTypeId=creativeBQATypeID;
                    if(Schema.SObjectType.Task.isUpdateable()){
                        update t;
                   }                    
                }
 } catch(Exception e){
        system.debug(e);}  
    
    
    }
    }
    
} catch(Exception e){
        system.debug(e);}
}
Best Answer chosen by Allison Humphrey 7
badibadi
Hi,
Please remove all the update statements from the trigger. your trigger should work.You do not need update statements in the trigger as your trigger is a "before insert" trigger and DML statements in the loop is a bad idea.

I have changed the trigger a bit. Hope this is what you are looking for 
trigger TaskTrigger on Task (before insert) {

    try{
    boolean active = [Select Active__c from TriggerOptions__c where Name = 'DisplayTaskTrigger' limit 1].Active__c;
    
    
   
   if(active==true){
        for(Task t:Trigger.New){
            try{
              // if Subject = "AO - Advertiser Tagging & QA" then recordtype is "Ad Tag & QA"
                 if(t.Subject=='AO - Advertiser Tagging & QA'){
                    ID adTagQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Ad Tag & QA').getRecordTypeId();
                    t.RecordTypeId=adTagQATypeID;                  
                } 
                
                // if Subject = "AO - Technical QA  " then recordtype is "Technical QA"
               
                if(t.Subject=='AO - Technical QA'){
                     ID technicalQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Technical QA').getRecordTypeId();
                     System.debug('QA RecordType'+technicalQATypeID);
                     t.RecordTypeId=technicalQATypeID;                   
                }
                
               // if Subject = "AO - Setup Network Pixels" then recordtype is "Pixel Setup"
                
                if(t.Subject=='AO - Setup Network Pixels'){
                    ID pixelSetupTypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Pixel Setup').getRecordTypeId();
                    t.RecordTypeId=pixelSetupTypeID;
                } 
                 // if Subject = "AM - Creative BQA" then recordtype is "Creative BQA"
                
                if(t.Subject=='AM - Creative BQA'){
                    ID creativeBQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Creative BQA').getRecordTypeId();
                    t.RecordTypeId=creativeBQATypeID;                  
                } 
 } catch(Exception e){
        system.debug(e.getMessage());}  
    }
    }
    
} catch(Exception e){
        system.debug(e.getMessage());}
}

 

All Answers

badibadi
Hi,
Please remove all the update statements from the trigger. your trigger should work.You do not need update statements in the trigger as your trigger is a "before insert" trigger and DML statements in the loop is a bad idea.

I have changed the trigger a bit. Hope this is what you are looking for 
trigger TaskTrigger on Task (before insert) {

    try{
    boolean active = [Select Active__c from TriggerOptions__c where Name = 'DisplayTaskTrigger' limit 1].Active__c;
    
    
   
   if(active==true){
        for(Task t:Trigger.New){
            try{
              // if Subject = "AO - Advertiser Tagging & QA" then recordtype is "Ad Tag & QA"
                 if(t.Subject=='AO - Advertiser Tagging & QA'){
                    ID adTagQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Ad Tag & QA').getRecordTypeId();
                    t.RecordTypeId=adTagQATypeID;                  
                } 
                
                // if Subject = "AO - Technical QA  " then recordtype is "Technical QA"
               
                if(t.Subject=='AO - Technical QA'){
                     ID technicalQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Technical QA').getRecordTypeId();
                     System.debug('QA RecordType'+technicalQATypeID);
                     t.RecordTypeId=technicalQATypeID;                   
                }
                
               // if Subject = "AO - Setup Network Pixels" then recordtype is "Pixel Setup"
                
                if(t.Subject=='AO - Setup Network Pixels'){
                    ID pixelSetupTypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Pixel Setup').getRecordTypeId();
                    t.RecordTypeId=pixelSetupTypeID;
                } 
                 // if Subject = "AM - Creative BQA" then recordtype is "Creative BQA"
                
                if(t.Subject=='AM - Creative BQA'){
                    ID creativeBQATypeID = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Creative BQA').getRecordTypeId();
                    t.RecordTypeId=creativeBQATypeID;                  
                } 
 } catch(Exception e){
        system.debug(e.getMessage());}  
    }
    }
    
} catch(Exception e){
        system.debug(e.getMessage());}
}

 
This was selected as the best answer
Allison Humphrey 7Allison Humphrey 7
@badi - Thank you so much!!! That did the trick.  I really appreciate your help.