+ Start a Discussion
Amit Jadhav 13Amit Jadhav 13 

Apex CPU time exceeded error in below method

Apex Method
 
public static void updateOwner(Map<Id,Project__c> newMap){
        system.debug('Inside');
        set<Id> setProjectId = new set<Id>();
        set<Id> setOpportunityId = new set<Id>();
        for(Project__c objProject : newMap.values()){
            if(objProject.id != null){
                setProjectId.add(objProject.id); 
            }
        }
        //List<OpportunityLineItem> lstOpportunityLineItem =[Select id,Projects__c,opportunityId,opportunity.ownerId from OpportunityLineItem where Projects__c IN:setProjectId LIMIT 3000];
        map<Id,OpportunityLineItem> mapOpportunityLineItem = new map<Id,OpportunityLineItem>([Select id,Projects__c,opportunityId,opportunity.ownerId from OpportunityLineItem where Projects__c IN:setProjectId LIMIT 3000]);
        for(OpportunityLineItem objOpportunityLineItem : mapOpportunityLineItem.values()){
            if(objOpportunityLineItem.opportunityId != null){
                setOpportunityId.add(objOpportunityLineItem.opportunityId);
            }
        }
        map<Id,Opportunity> mapOpportunity = new map<Id,Opportunity>([Select id,ownerId,Total_Products_under_Opportunity__c,type,stageName,(Select id,opportunityId from OpportunityLineItems) from Opportunity where id IN :setOpportunityId AND isClosed =false AND type != null]);
        map<Id,Project__c> mapProject = new map<Id,Project__c>([Select id,name,Opportunity_owner__c,Renewal_owner__c,Program_Manager_U__c,(Select id,Projects__c from Opportunity_Product__r) from project__c where Id IN:setProjectId]); 
        for(OpportunityLineItem objOpportunityLineItem : mapOpportunityLineItem.values()){
            /*Project__c objProject = mapProject.get(objOpportunityLineItem.Projects__c);
            if(objOpportunityLineItem.opportunityId != null){
                Opportunity objOpportunity = mapOpportunity.get(objOpportunityLineItem.opportunityId);
                if( objOpportunity != null){ 
                    if(objProject.name =='The Linux Foundation' && objOpportunity.Total_Products_under_Opportunity__c <= 1 && objProject.Opportunity_owner__c !=null && objOpportunity.type =='New Business'){
                        system.debug('Inside Linux');
                        objOpportunity.OwnerId = objProject.Opportunity_Owner__c;
                        system.debug('objOpportunity'+objOpportunity);
                    }
                    
                    if( objProject.Opportunity_owner__c !=null && objOpportunity.type =='New Business' && objProject.name !='The Linux Foundation'){
                        objOpportunity.OwnerId = objProject.Opportunity_Owner__c;
                    }
                    if(objProject.Renewal_owner__c !=null && objOpportunity.type =='Renewal' ){
                        System.debug('Inside Renewal');
                        objOpportunity.OwnerId = objProject.Renewal_owner__c;
                        System.debug('objOpportunity'+objOpportunity);
                    }else if(objProject.Renewal_owner__c ==null && objProject.Program_Manager_U__c == null){
                        objOpportunity.OwnerId = objProject.Opportunity_Owner__c;
                    }
                    
                    if(objProject.Opportunity_owner__c ==null && objProject.Renewal_owner__c ==null && objProject.Program_Manager_U__c != null ){
                        objOpportunity.OwnerId = objProject.Program_Manager_U__c;
                    }
                    if(objProject.Opportunity_owner__c ==null && objProject.Renewal_owner__c ==null && objProject.Program_Manager_U__c == null ){
                        system.debug('Inside Null');
                        objOpportunity.OwnerId = objOpportunityLineItem.opportunity.ownerId;
                    }
                }
            
        }
        update mapOpportunity.values();

 
AnudeepAnudeep (Salesforce Developers) 
Hi Amit, 

Check the cpu usage in your loops and try and optimize wherever there is high usage
 
Integer cpuStart = Limits.getCpuTime();

System.debug('Before For loop = ' +cpuStart);

for(OpportunityLineItem objOpportunityLineItem : mapOpportunityLineItem.values()){ 

// Your for loop code

 }

 System.debug('CPU time consumed in loop ' + (Limits.getCpuTime() - cpuStart));

 cpuStart = Limits.getCpuTime();

 System.debug('Before DML = ' +cpuStart);

 update mapOpportunity.values();

 System.debug('CPU time consumed in DML = '+ (Limits.getCpuTime() - cpuStart));

https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1&mode=1