+ Start a Discussion
SAI CHARAN MADUGULASAI CHARAN MADUGULA 

Error:Apex trigger OCRtrigger caused an unexpected exception, contact your administrator: OCRtrigger: System.LimitException: Apex CPU time limit exceeded

OCRtrigger:
trigger OCRtrigger on OCR_Pharmacy__c (after update) {

    OCRTriggerHandler handler = new OCRTriggerHandler();
    
    handler.UpdateAccountOnAddressChange(trigger.new,Trigger.OldMap)

Trigger Handler: 
public class OCRTriggerHandler{

    public void UpdateAccountOnAddressChange(List<OCR_Pharmacy__c> OCRList, Map<Id,OCR_Pharmacy__c> TriggerOldMap){
    
        Set<Id> ocrIdSet = new Set<Id>();
        List<Account> accountToUpdateList = new List<Account>();
    
        for(OCR_Pharmacy__c OCR : OCRList){
            if((OCR.Address__c != TriggerOldMap.get(OCR.id).Address__c || OCR.City__c!= TriggerOldMap.get(OCR.id).City__c|| 
            OCR.State_Province__c != TriggerOldMap.get(OCR.id).State_Province__c ||
            OCR.Zip_Postal_Code__c != TriggerOldMap.get(OCR.id).Zip_Postal_Code__c || 
            OCR.Country__c!= TriggerOldMap.get(OCR.id).Country__c) && OCR.Status__c != 'Inactive'){
                ocrIdSet.add(OCR.Id);
            }
        }
        
        if(!ocrIdSet.isEmpty()){
            List<OCR_Pharmacy__c> ocrAccList = [
                Select Id,Address__c
                     , City__c
                     , State_Province__c
                     , Zip_Postal_Code__c
                     , Country__c
                     , (Select Id
                             , Distance_to_Service_Facility__c 
                          from Accounts__r
                         where (RecordType.Name = 'LTC Prospect Facility'
                         OR
                         RecordType.Name='LTC Customer Facility'
                         OR
                         RecordType.Name='LTC Former Facility'       
                         ) 
                           
                       )
                  from OCR_Pharmacy__c where Id IN : ocrIdSet
            ];
            
            
            for(OCR_Pharmacy__c OCR : ocrAccList){
                //system.assert(false, '(OCR.Accounts__r.size()' + OCR.Accounts__r.size());
                if(OCR.Accounts__r.size()>0){
                    for(Account acc : OCR.Accounts__r){
                        if(acc.Distance_to_Service_Facility__c <= 0 || String.isBlank(String.valueOf(acc.Distance_to_Service_Facility__c))) {
                            if(Schema.SObjectType.Account.fields.Distance_to_Service_Facility__c.isUpdateable() && Schema.SObjectType.Account.fields.Override_Calculate_Miles__c.isUpdateable()){
                                acc.Distance_to_Service_Facility__c = NULL;
                                acc.Override_Calculate_Miles__c = false;
                                accountToUpdateList.add(acc);
                            }
                            
                        }
                    }
                }    
            }
            if(!accountToUpdateList.isEmpty() && Schema.SObjectType.Account.isUpdateable()){
                update accountToUpdateList ;
            }
        }
        
    }

}
SAI CHARAN MADUGULASAI CHARAN MADUGULA
can anbody please help me what to change in the code
Dagny FernandesDagny Fernandes
1. Avoid using for loop inside the for loop.
2. in the above code there are no System.debug statments, If you are using System.Debug statments remove it or minimize the debug statment s.