CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: Shift_OpportunityLineItemTrigger: System.LimitException: Apex CPU time limit exceeded.
I have a trigger " Shift_OpportunityLineItemTrigger" that is being referenced in the error but has not recently been changed at all and we were able to upload Opportunities yesterday so I'm unclear to what could be different or what the issue is.
Here is the code for the trigger itself -
trigger Shift_OpportunityLineItemTrigger on OpportunityLineItem (before insert, before update, after insert, after update, after delete, after undelete) {
/*
* Name : Shift_OppLineTrigger
* Author : Leonard Loo
* Description : Apex Trigger that calculates the Unit Price based
* on the RevenueScheduler Type value
*
* Maintenance History:
* Date - Name - Version - Remarks
* 2012/07/27 - Leonard Loo - Version 1.0 - No comments
* 2013/08/11 - Hermes Yan - Version 1.1 - Flat rate ignores quantity
* 2014/01/03 - Hermes Yan - Version 1.2 - Removal of update conditions.
* 2014/12/02 - Leonard Loo - Version 1.3 - Added Auto Scheduling and moved Unit Price calculation to its own class. Renamed trigger.
*/
Shift_AutoScheduling AutoScheduler = new Shift_AutoScheduling();
Map<String, Shift_Field_Trigger_List__c> theTriggerFields = Shift_Field_Trigger_List__c.getAll();
if (trigger.isAfter && (trigger.isUpdate || trigger.isInsert || trigger.isUndelete)) { // 1.3 - Leonard Loo - Trigger autoscheduling if relevant fields are updated
Map<Id, OpportunityLineItem> theLinesToScheduleMap = new Map<Id, OpportunityLineItem>();
for (OpportunityLineItem oli : Trigger.new) {
// Only schedule Line Items if Media Cost, Service Date, End Date or Total Price have changed
Boolean toSchedule = Trigger.isInsert;
if (Trigger.isUpdate ) {
for (Shift_Field_Trigger_List__c f : theTriggerFields.values()) {
if (oli.get(f.Opportunity_Line_Item_Field__c) <> trigger.oldMap.get(oli.Id).get(f.Opportunity_Line_Item_Field__c)) {
toSchedule = true;
break;
}
}
}
if (toSchedule) {
theLinesToScheduleMap.put(oli.Id, oli);
}
}
if (!theLinesToScheduleMap.isEmpty()) {
// Note that we create an empty Map of Opportunity Line Item Schedules.
// The second parameter of the CreateSchedules method is only used when calling the method from a trigger
// on the Opportunity Line Item Schedule object. Passing an empty list indicates that we are calling this method from
// the Opportunity Line Item Trigger.
AutoScheduler.CreateSchedules(theLinesToScheduleMap, new Map<Id, List<OpportunityLineItemSchedule__c>>(), trigger.isInsert, trigger.isUpdate);
}
} else if (trigger.isAfter && trigger.isDelete) {
// Since we do not have a master-detail relationship between Opportunity Line Items and Schedules
// we must simulate it by deleting the Schedules that have the same Opportunity Line Item Id as the Opportunity Line Item
// being deleted
AutoScheduler.DeleteSchedules(trigger.oldMap);
}
}
Any help would be greatly appreciated. Thanks so much in advance!
Hi Rachael, I do not know if this will help or not, but I had the exact same error (on a different Object) when I uploaded a large number of new Records last week. There were several Triggers, Proceses AND Workflow Rules for this Object. What it turned out to be was just that running all of the Triggers and Processes for all of these new Records at the same time created a "bottleneck" if you will that threw that error.