You need to sign in to do that
Don't have an account?
Chuck M Rohllf
System Limit Exception - Apex CPU Timeout
My code below is functional when updating accounts manually.....however, when I update several accounts with Data Loader I'm getting nothing but timeout errors - this is even with a small batch selected. I'm sure my code could use some clean-up to run more efficiently and avoid the errors. I'm far from a developer - can anyone assist? Thank you!
trigger AccountGetTerritoryReference on Account (before insert, before update) { Map<String,Territory_Reference__c> Terr2Terr_SA = new Map<String,Territory_Reference__c>(); Map<String,Territory_Reference__c> Terr2Terr_DTT = new Map<String,Territory_Reference__c>(); Set<String> SA = new Set<String>(); Map<String,Contact> Contact_Estab_Id = new Map<String, Contact>(); Set<String> CID = new Set<String>(); if(checkRecursive.runOnce()) for(Account a:Trigger.new) { if (a.RecordTypeId == '012E0000000PR3u') { if (a.Agency__c<>null){ //something new SA.add(a.Agency__c); a.Agency_Name__c = a.Agency__c; // the default agency name will be the SA } else if (a.Agency__c==null ) { a.Agency_Name__c = ''; } } a.NAIS_VOSS_Mgr__c = null; a.NAIS_VSEN_Mgr__c = null; a.MSO__c = null; a.PSE_Representative__c = null; a.PSM__c = null; a.RMOS__c = null; a.VOSS__c = null; a.VPSO__c = null; a.MTSS__c = null; a.Xerox_Entity__c = null; a.Area_Title_t2t__c = null; a.ASD__c = null; a.CBM__c = null; a.VSPM__c = null; a.MPS_Manager__c = null; } for (Territory_Reference__c t : [SELECT MTSS__c,NAIS_VOSS_Mgr__c,VSEN__c,NAIS_VSEN_Mgr__c,AGENT_DTT__c,AGENCY__c,AGENCY_ID__c,SA_DTT_KEY__c,VOSS__c,PSE__c,PSM__c,MSO__c,RVP__c,Area_Title__c,REGION__c,RMOS__c,ASD__c,CBM__c,VPSE__c,VSPM__c FROM Territory_Reference__c WHERE AGENCY_ID__c IN : SA]){ Terr2Terr_SA.put(t.AGENCY_ID__c,t); Terr2Terr_DTT.put(t.AGENT_DTT__c,t); } if (!Terr2Terr_SA.isEmpty()) { for(Account a:Trigger.new) { if (Terr2Terr_DTT.get(a.Dtt__c)<>null){ Territory_Reference__c t2t = Terr2Terr_DTT.get(a.Dtt__c); a.Agency_Name__c = t2t.AGENCY__c; a.Inside_Sales_Representive__c = t2t.VSEN__c; a.VPSE__c = t2t.VPSE__c; } if (Terr2Terr_SA.get(a.Agency__c)<>null){ Territory_Reference__c t2t = Terr2Terr_SA.get(a.Agency__c); a.Agency_Name__c = t2t.AGENCY__c; a.NAIS_VOSS_Mgr__c = t2t.NAIS_VOSS_Mgr__c; a.NAIS_VSEN_Mgr__c = t2t.NAIS_VSEN_Mgr__c; a.MSO__c = t2t.MSO__c; a.PSE_Representative__c = t2t.PSE__c; a.PSM__c = t2t.PSM__c; a.RMOS__c = t2t.RMOS__c; a.VOSS__c = t2t.VOSS__c; a.VPSO__c = t2t.RVP__c; a.MTSS__c = t2t.MTSS__c; a.Xerox_Entity__c = t2t.REGION__c; a.Area_Title_t2t__c = t2t.Area_Title__c; a.ASD__c = t2t.ASD__c; a.CBM__c = t2t.CBM__c; a.VSPM__c = t2t.VSPM__c; } List<Contact> contactList = new List<Contact>(); List<Contact> contactList1 = new List<Contact>(); for(Account acc: [SELECT Id,RecordTypeID,(SELECT Id,RecordTypeID,Account.Id FROM Contacts) FROM Account WHERE Id in: Trigger.new]){ If(acc.Contacts.size()>0 && acc.RecordTypeID =='012E0000000PR3u'){ contactList.addALL(acc.Contacts); } for(contact c:contactList){ If(c.RecordTypeID !='012440000002T6J' && c.Account.Id == acc.Id){ c.checkbox__c=true; contactList1.add(c); //a.NAIS_VOSS_Mgr__c = t2t.NAIS_VOSS_Mgr__c; //a.NAIS_VSEN_Mgr__c = t2t.NAIS_VSEN_Mgr__c; c.CBM__c = a.MSO__c; // a.PSE__c = t2t.PSE__c; // a.PSM__c = t2t.PSM__c; // a.RVP__c = t2t.RMOS__c; // a.VOSS__c = t2t.VOSS__c; // a.RVP__c = t2t.RVP__c; // a.MTSS__c = t2t.MTSS__c; // a.Xerox_Entity__c = t2t.REGION__c; // a.Area_Title_t2t__c = t2t.Area_Title__c; // a.ASD__c = t2t.ASD__c; // a.CBM__c = t2t.CBM__c; // a.VSPM__c = t2t.VSPM__c } } } update contactList1; } } }
Apuroop
Hey Chuck, right off the top, on line 128 there's an update inside the for loop. Also on line 11, there's a if statement but there's no scope for it to check and get into it. Get the indentation right to kill the confusion.