You need to sign in to do that
Don't have an account?
Jessica Puckett
Apex Trigger & DataLoader: System.LimitException: Apex CPU time limit exceeded
I am attempting to learn Apex and last week successfully deployed my first (and admittedly rudimentary) Apex trigger to production. Everything was going great until I attempted to update some of my Contact records via DataLoader. I received a System.LimitException: Apex CPU time limit exceeded error. I was able to successfully break my .csv file into 200-part pieces and upload, but I know there must be a better way. As I understand it, the loops in my trigger are the cause, but I'm not sure how to improve the code to solve this. My code is below and suggestions are greatly appreciated!
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
for(contact currCon: contactList ){
currcon.duplicate_email__c=false;
for(contact exiscon : listExistingContact ){
if(currcon.email==exiscon.email){
currcon.duplicate_email__c=true;
} else {
currcon.duplicate_email__c=false;
}
if(currcon.email==null || currcon.email==''){
currcon.duplicate_email__c=false;
}
}
}
}
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
for(contact currCon: contactList ){
currcon.duplicate_email__c=false;
for(contact exiscon : listExistingContact ){
if(currcon.email==exiscon.email){
currcon.duplicate_email__c=true;
} else {
currcon.duplicate_email__c=false;
}
if(currcon.email==null || currcon.email==''){
currcon.duplicate_email__c=false;
}
}
}
}
CPU Time limit mainly ocurs due to excexx processing time.
For this we need to avoid nested for loops, minimize If conditions, use SOQL For loops, etc.
I have somewhat updated your above code to minimize the CPU processing time as follows,
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
Map<String, Contact> emailVsContactMap = new Map<String, Contact>();
for(Contact con : listExistingContact){
if(c.email!=null || c.email!='')
emailVsContactMap.put(con.Email, con);
}
for(contact currCon: contactList ){
currcon.duplicate_email__c = false;
if(emailVsContactMap.containsKey(currCon.Email))
currcon.duplicate_email__c=true;
}
}
OR
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
for(contact currCon: contactList ){
currcon.duplicate_email__c = false;
for(contact exiscon : listExistingContact ){
if(currcon.email == exiscon.email)
currcon.duplicate_email__c=true;
}
}
}
========================================================================================================
Note: Just keep in mind, avoid writting the whole logic in Trigger, create an handler class, write your logic their and call that class from the trigger.
Reason being, this makes your code reusable and testable.
- Hope this helps
All Answers
CPU Time limit mainly ocurs due to excexx processing time.
For this we need to avoid nested for loops, minimize If conditions, use SOQL For loops, etc.
I have somewhat updated your above code to minimize the CPU processing time as follows,
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
Map<String, Contact> emailVsContactMap = new Map<String, Contact>();
for(Contact con : listExistingContact){
if(c.email!=null || c.email!='')
emailVsContactMap.put(con.Email, con);
}
for(contact currCon: contactList ){
currcon.duplicate_email__c = false;
if(emailVsContactMap.containsKey(currCon.Email))
currcon.duplicate_email__c=true;
}
}
OR
trigger contactDupCheck2 on Contact (before insert, before update, before delete) {
set<string> emailSet = new set<string>();
List<contact> contactList = new list<contact>();
for(contact c : trigger.new){
if(c.email!=null || c.email!=''){
emailSet.add(c.email);
contactList.add(c);
}
}
List<Contact> listExistingContact = [select Id, email, LastName from Contact where email in: emailSet and id not in : trigger.new];
for(contact currCon: contactList ){
currcon.duplicate_email__c = false;
for(contact exiscon : listExistingContact ){
if(currcon.email == exiscon.email)
currcon.duplicate_email__c=true;
}
}
}
========================================================================================================
Note: Just keep in mind, avoid writting the whole logic in Trigger, create an handler class, write your logic their and call that class from the trigger.
Reason being, this makes your code reusable and testable.
- Hope this helps
Hi Jessica,
Hope it will helps :) Please let me know, in case of issue
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com