You need to sign in to do that
Don't have an account?
Trigger resulting in System.LimitException: Apex CPU time limit exceeded
Hello all.
I am running into the following error: System.LimitException: Apex CPU time limit exceeded.
The error file available in dataloader.io states the following: ERROR: The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 3014A000000Xp6z. Flow error messages: This interview has been terminated as another interview in the same bulk execution request failed with the following error: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow.
The email from Salesforce subject: Error Occurred During Flow "Company_Owner_Sync": The flow tried to update these records:... After all the records update failures the message continues This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: UKCheck: System.LimitException: Apex CPU time limit exceeded. For details, see API Exceptions.---The flow tried to update these records: null.
The Process Builder referrenced "Company Owner Sync" attempts to update the owner of related children custom objects (Domain__c, Meridian_Username__c) and Standard Contacts if the Company(Account) owner is changed.
The UKCheck trigger I have created uses a custom setting to evaluate the Country picklist and stamp a UK Team checkbox if the picklist contains one of the countries specified in the Custom setting. I have included the trigger below.
Now the trigger works on single UI insert and updates. The Trigger has worked on bulk upload; however, 40 records attempted to be updated are causing the error described above. I have looked in the debub log but it is rather daunting as I am not completely sure what to look for. Hopefully someone can provide assistance based on this description and trigger. Thank you.
I am running into the following error: System.LimitException: Apex CPU time limit exceeded.
The error file available in dataloader.io states the following: ERROR: The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 3014A000000Xp6z. Flow error messages: This interview has been terminated as another interview in the same bulk execution request failed with the following error: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow.
The email from Salesforce subject: Error Occurred During Flow "Company_Owner_Sync": The flow tried to update these records:... After all the records update failures the message continues This error occurred: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: UKCheck: System.LimitException: Apex CPU time limit exceeded. For details, see API Exceptions.---The flow tried to update these records: null.
The Process Builder referrenced "Company Owner Sync" attempts to update the owner of related children custom objects (Domain__c, Meridian_Username__c) and Standard Contacts if the Company(Account) owner is changed.
The UKCheck trigger I have created uses a custom setting to evaluate the Country picklist and stamp a UK Team checkbox if the picklist contains one of the countries specified in the Custom setting. I have included the trigger below.
Now the trigger works on single UI insert and updates. The Trigger has worked on bulk upload; however, 40 records attempted to be updated are causing the error described above. I have looked in the debub log but it is rather daunting as I am not completely sure what to look for. Hopefully someone can provide assistance based on this description and trigger. Thank you.
trigger UKCheck on Domain__c (before insert, before update) { Map<String,Country_Codes__c> getCodes = Country_Codes__c.getAll(); for (Domain__c dom : Trigger.new) { if(!String.isBlank(dom.Country__c)) { for(String s : getCodes.keySet()) { if(s.containsIgnoreCase(dom.Country__c)) { dom.UK_Team__c = TRUE; } } } } }
use above code, on update your trigger should only work if the coutry is changed.
All Answers
use above code, on update your trigger should only work if the coutry is changed.
I am going to incorporate these tweeks and come back to this thread. I will have to write a test class before I can fully test this. My question regarding the test class for this check for country code change is as follows. To properly test this line of code I would create a record with a country code not listed in my custom setting and update the record to a Country__c listed in the setting. Also I would create another record making a change but NOT update the Country__c. Is this correct?
I will recommend you to remove extra for loop from code. Due to multiple for loop we used to get Apex CPU time limit exceeded error
Try to update your code like below
Please check below post for Trigger best practice http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html
For test class try like below
Let us know if this will help you
What am I missing here to get 100%?
If possible try to mearge both insert and update in trigger like trigger which i posted above