function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Muhammad MoneebMuhammad Moneeb 

Apex Cpu Limit Exceeded

i am working on a trigger in its giving cpu limit exceeded error can anyone help me here is my code 
 
trigger OPP_Percentage_Change on Opportunity (after update,after insert) {
    if(Trigger.isUpdate){
    List<Opportunity> OppToPass = new List<Opportunity>();
    Map<id,Opportunity> MapToPass = new Map<Id,Opportunity>();
    for(Opportunity opp : Trigger.new){
        OppToPass.add(opp);
    }
    for(Opportunity OppOld: Trigger.Old){
        MapToPass.put(OppOld.Id, OppOld);
    }
    if(OppToPass.size()>0 && MapToPass.size()>0){
        SaleOrderOpportunityTriggersHandler.Opp_Percentage_Change(OppToPass,MapToPass);
        SaleOrderOpportunityTriggersHandler.OppAmount(OppToPass);
    }
    }
    
    
}

here is class function 
public static void Opp_Percentage_Change(List<Opportunity> OppForPercentage,Map<id,Opportunity> OldMap){
     Set<Id> OppSet = new Set<Id>();
     Map<Id,Sales_Order__c> SoToUpdate = new Map<Id,Sales_Order__c>();
        Map<Id,Sales_Order__c> mapSo = new Map<Id,Sales_Order__c>();
        for (Opportunity opp : OppForPercentage){
            OppSet.add(OPP.Id);
        }
        for(Sales_Order__c SO : [Select Quote_Odds__c,Quote__c, Requested_Delivery_Date__c, OrderNum__c
                                        From Sales_Order__c Where Quote__c IN: OppSet]){
           mapSo.put(SO.Quote__c,SO); 
        }
        for(Opportunity oop : OppForPercentage){
            if(oop.Id == Oldmap.get(oop.Id).Id){
                if(oop.Probability != OldMap.get(oop.Id).Probability){
                    mapSo.get(oop.Id).Quote_Odds__c  = oop.Probability;
                    SoToUpdate.put(mapso.get(oop.id).Id, mapso.get(oop.id));
	                    
                }
                
            }
            if(Oop.CloseDate != Oldmap.get(Oop.Id).CloseDate){
                mapso.get(oop.Id).Requested_Delivery_Date__c = Oop.CloseDate;
                SoToUpdate.put(mapso.get(oop.id).Id, mapso.get(oop.Id));
            }
            if(Oop.StageName != OldMap.get(oop.Id).StageName){
                if(Oop.StageName == 'Closed Lost'){
                    if(!((mapso.get(oop.Id).OrderNum__c != null) && (mapso.get(oop.Id).OrderNum__c.charAt(0) == 67 || mapso.get(oop.Id).OrderNum__c.charAt(0) ==99))){
                        mapso.get(oop.Id).OrderNum__c = 'Quote Lost';
                        SoToUpdate.put(mapso.get(oop.Id).Id,mapso.get(oop.Id));
                    }
                }
            }
            
        }
        Update SoToUpdate.values();
        
    }

 
venkat-Dvenkat-D
In the trigger, why dont you simply use 
SaleOrderOpportunityTriggersHandler.Opp_Percentage_Change(Trigger.New,Trigger.oldmap);
SaleOrderOpportunityTriggersHandler.OppAmount(Trigger.new);

this will avoid lot of for loop statements
venkat-Dvenkat-D
In the class use trigger lists and map to avoid any new queries.