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
guest1231231guest1231231 

Too Many Script Statements

When my test code bulk inserts 100 tasks it does not throw any exceptions, but when I increase it to 200 I get "System.Exception: Too many script statements: 50001".

 

Why would it throw an exception at 200 but not 100 insertions?

 

-Thanks

rungerrunger

You're running into the governor limit that enforces the number of script statements executed.  It doesn't relate directly to the insertions, aside from the fact that an insert statement is a single statement.  It's just that the total amount of code you're running in a single request has exceeded the limit.

guest1231231guest1231231

What am I doing wrong here?  I am still new to Apex but I followed the bulkifying code best practices to the best of my ability.  What is causing this code to hit too many script statements?

 

-Thanks

 

 

//Whenever a task record is inserted, updated, or deleted this trigger will look at all the task records 
//for first and last "contact date" of the related tasks and copy  to Contact Dates field of Account.
trigger CopyFirstContactDateToAccount on Task (after insert, after update, after delete) {
	
	//create a set of WhatIds for the tasks
	
    Task[] tasks = new Task[]{};
    Task[] tasksOld = new Task[]{};   
    if(Trigger.isInsert) {
        tasks = Trigger.new;
    } else if(Trigger.isUpdate) {
        tasks = Trigger.new;
        tasksOld = Trigger.old;               
	} else if(Trigger.isDelete) {
        tasks = Trigger.old;      
    	}
	
    Set <ID> setAccountIds = new Set <ID>();
    for(Task activity: tasks) {
        if(activity.WhatId != null) {
            setAccountIds.add(activity.WhatId);
        }
    }
    
    Set <ID>setAccountIdsToBeNull = new Set <ID>();
    for(Integer i=0;i<tasksOld.size();i++) {
        if(tasksOld[i].WhatId != null && tasks[i].WhatId == null) {
            setAccountIdsToBeNull.add(tasksOld[i].WhatId);
        } else if(tasksOld[i].WhatId != null && (tasksOld[i].WhatId != tasks[i].WhatId)) {
            setAccountIdsToBeNull.add(tasksOld[i].WhatId);
        	}
    }

	try {
    CopyContactDate(tasks,setAccountIds);

    if(Trigger.isUpdate && setAccountIdsToBeNull.size()>0)
        CopyContactDate(tasksOld,setAccountIdsToBeNull);

	}        
	catch (System.DMLException e) {
	     trigger.new[0].addError('Edit the related account in a new tab and correctly complete the required fields.');
	}
	
	/*--------------------------------------------------------------------------------------------------*/
    //this method will copy the minimum and maximum activitydates from the Task object to related Account
    private void CopyContactDate (Task[] activities,Set<ID> setAccountIdsSet)
    {
    	//create a set of OwnerIds from the related accounts
    	Set <ID> setOwnerIds = new Set <ID>();
    	for(Account acct: [select Id, OwnerId from Account where Id in:setAccountIdsSet]) {
        	if(acct.OwnerId != null) {
            	setOwnerIds.add(acct.OwnerId);
        	}
    	}
    	
    	//map the related completed tasks WhatId if the OwnerId is the same as the related account 
        Task [] taskAccts = [select Id, WhatId, OwnerId, ActivityDate 
        					from Task 
        					where Status='Completed' and WhatId in:setAccountIdsSet and OwnerId in:setOwnerIds];
        Map<Id,List<Task>> taskAcctsMap = new Map<Id,List<Task>>();		//map the task WhatId
        for (Task allTasks: taskAccts) {
            if(taskAcctsMap.containsKey(allTasks.WhatId)) {
                taskAcctsMap.get(allTasks.WhatId).add(allTasks);
            } else {
                taskAcctsMap.put(allTasks.WhatId,new List<Task>{allTasks});
            }
        }
        
        //mapthe related accounts
        Map<Id,Account> AccountsToBeUpdated = new Map<Id,Account>();
        
        //populate the contact date fields
        for (Task t: activities) {
            Date firstContactDate= null;
            Date lastContactDate = null;
            
            List<Task> taskAcctsMapTempList = new List<Task>();
            taskAcctsMapTempList = taskAcctsMap.get(t.WhatId);
            if(taskAcctsMapTempList != null) {
                for (Task bdcAccsMapTemp : taskAcctsMapTempList) {
                    if (firstContactDate== null) {
                        	firstContactDate = bdcAccsMapTemp.ActivityDate;
                    } else {
					if (bdcAccsMapTemp.ActivityDate<firstContactDate) {
                    		firstContactDate = bdcAccsMapTemp.ActivityDate;
						}
                    } 
                    if (lastContactDate == null) {
                        	lastContactDate = bdcAccsMapTemp.ActivityDate;
                    } else {
					if (bdcAccsMapTemp.ActivityDate>lastContactDate) {
                            lastContactDate = bdcAccsMapTemp.ActivityDate;
                       	}
                    }
                }
            }
            if(t.WhatId != null) {
                Account a =	new Account(id=t.WhatId);
				a.First_Contact_Date__c= firstContactDate;
       			a.Last_Contact_Date__c= lastContactDate;
				AccountsToBeUpdated.put(t.WhatId,a);	
			}
        }
        	if(AccountsToBeUpdated.size()>0) {
            	update AccountsToBeUpdated.values();
        	}
    }   
}