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
DJP1SDJP1S 

Too Many Script Statements

I've got some code I've written that writes out all the service fields in my custom Task object for a it's parent Project object. When I insert a lot of records or delete a lot, this class gets called.

 

public class ProjectTaskGoals {
    
    public ProjectTaskGoals (List<Page1Task__c> triggerNew){
        
        	Page1Project__c[] projectsToUpdate = new Page1Project__c[]{};
        
         	Set<Id> projectIds = new Set<Id>();
            Page1Project__c[] relatedProjects = new Page1Project__c[]{};
            Integer num = 200;
           
            
            if(triggerNew != null){
            for(Page1Task__c cpc : triggerNew){
                
                projectIds.add(cpc.Project__c);
            }
                
            Page1Project__c[] projects = [SELECT Id, Task_Report_Codes__c FROM Page1Project__c
                                          WHERE Id =: projectIds];
                
            for(Page1Project__c pro : projects){
                
                relatedProjects.add(pro);
            }  
                
          	Page1Task__c[] writeCodes =   [SELECT Id, Seq__c, Project__c, Status__c, Service__r.Name FROM Page1Task__c
				                			WHERE Project__c =: relatedProjects
				                            AND Service__r.Name != null
				                            ORDER BY Seq__c
				                            LIMIT :num];
                
                for(Page1Project__c cci : projects){
                	Integer w = 0;
                	Integer x;
            	 	String taskReportCodes = '';
            		String taskMonthlyGoals = '';                           
                	for(Page1Task__c p1t: writeCodes){              
                    	if(p1t.Project__c == cci.Id)
		                    { 
		                        taskReportCodes += '' + p1t.Seq__c + ') ' + p1t.Service__r.Name + ', ';
		                        w += 1;
	                        }
		                        taskReportCodes += ''; 
                    }
		                 cci.Task_Report_Code_Count__c = w;
		                 integer l = taskReportCodes.length();
                   		 integer L2 = l - 2;
                    	 taskReportCodes = taskReportCodes.left(L2);
                    	 if (taskReportCodes.length() >= 255){
                             taskReportCodes = taskReportCodes.left(255);
                         }
		                 cci.Task_Report_Codes__c = taskReportCodes;
		                                
		                 projectsToUpdate.add(cci);
		                 //system.debug('%%%projectsToUpdate: ' + projectsToUpdate);
                  	                                       
            	}
         	}
        if(projectsToUpdate.size() > 0){
            	update projectsToUpdate;   
            }
	}
}

 This part is what's causing me grief is this little snippet here:

 

 for(Page1Project__c cci : projects){
                	//Some variables                         
     for(Page1Task__c p1t: writeCodes){              
         if(p1t.Project__c == cci.Id)
	    { 
              So some work
            }

 In the debug logs, I'm seeing a lot of this...

 

12:24:54.932 (20932493000)|SYSTEM_METHOD_EXIT|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932515000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932531000)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932572000)|SYSTEM_METHOD_ENTRY|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932588000)|SYSTEM_METHOD_EXIT|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932608000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932623000)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932664000)|SYSTEM_METHOD_ENTRY|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932679000)|SYSTEM_METHOD_EXIT|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932704000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932718000)|SYSTEM_METHOD_EXIT|[41]|system.ListIterator.hasNext()
12:24:54.932 (20932761000)|SYSTEM_METHOD_ENTRY|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932777000)|SYSTEM_METHOD_EXIT|[42]|Id.compareTo(Id, Boolean)
12:24:54.932 (20932798000)|SYSTEM_METHOD_ENTRY|[41]|system.ListIterator.hasNext()
colemabcolemab

Please see the apex best practices for tips on how to bulkify your code.

 

Here is a great article on writing bulk triggers for salesforce.

 

Hopefully these will get you going down the right path.  In general salesforce encourges use of memory (i.e. collections) over process cycles (i.e. querying in a loop).  This is different than other platforms.

 

All of that being said, I think your main issue is that you have some unnecessary loops in the start of your ProjectTaskGoals method that could be removed and/or conslidated.