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
Synthia BeauvaisSynthia Beauvais 

Apex CPU time limit exceeded Class

I am getting these 4 errors when trying to close opportunities. I am not sure how to correct this. 

Account: System.LimitException: Apex CPU time limit exceeded
Opportunity: execution of AfterUpdate caused by: System.LimitException: Apex CPU time limit exceeded Trigger.Opportunity: line 66, column 1

Opportunity: execution of BeforeUpdate caused by: System.LimitException: Apex CPU time limit exceeded Class.OpportunityTriggerUtil.carryOverSalesTeamOnOwnerChange: line 149, column 1 Trigger.Opportunity: line 108, column 1

Opportunity: execution of BeforeUpdate caused by: System.LimitException: Apex CPU time limit exceeded Class.OpportunityTriggerUtil.carryOverSalesTeamOnOwnerChange: line 150, column 1 Trigger.Opportunity: line 108, column 1
 
trigger Opportunity on Opportunity (before insert, after insert, before update, after update) {

	Set<String> specialistProductCategories = new Set<String>();
	Boolean addSpecialistsToSalesTeam = false;
	for(RoleNameMapping__c rnm : OpportunityTriggerUtil.roleNameMapping)
	{
		specialistProductCategories.add(rnm.Product_Category__c);
	}

	Id testingCaraId = [select Id from Profile where Name = 'Testing - Cara'].Id;
	System.debug(testingCaraId);
	System.debug(UserInfo.getProfileId());
	if ( UserInfo.getProfileId() !=  testingCaraId )
	{
		if(Trigger.isInsert)
		{
			if(Trigger.IsBefore)
			{
				OpportunityTriggerUtil.updateAccountDEUField(Trigger.new);
				
				/* 10/12 - Taken out to remove Account Vertical field and functionality on Opportunity to remove an inconsistent error. */
				/*
				List<Account> populatedAccounts = OpportunityTriggerUtil.populateAccountVerticalField(trigger.new);
                if(! populatedAccounts.isEmpty()){
                    OpportunityTriggerUtil.updateAccounts(populatedAccounts, trigger.new);
                }
                */
			}
			else
			{
				for(Opportunity o : Trigger.new)
				{
					if( o.Prod_Category__c != null )
					{
						for(String category : o.Prod_Category__c.split(';'))
						{
							if(specialistProductCategories.contains(category))
							{
								addSpecialistsToSalesTeam = true;
								break;
							}
						}
					}
				}
				if(addSpecialistsToSalesTeam )
				{
					OpportunityTriggerUtil.updateExistingOpportunitiesSalesTeam(Trigger.new);
				}
				
			}
		}
		else if(Trigger.isUpdate && trigger.isAfter)
		{
			/*Only update opportunity sales team if new product categories that
			map to specialists are added*/

			Set<String> oldProductCategories = new Set<String>();
			Set<String> newProductCategories = new Set<String>();


			for(Opportunity o : Trigger.old)
			{
				if(o.Prod_Category__c != null )
				{
					for(String category : o.Prod_Category__c.split(';'))
					{
						oldProductCategories.add(category);
					}
				}
			}

			for(Opportunity o : Trigger.new)
			{
				if(o.Prod_Category__c != null  )
				{
					for(String category : o.Prod_Category__c.split(';'))
					{
						if(specialistProductCategories.contains(category) && !oldProductCategories.contains(category))
						{
							newProductCategories.add(category);
						}
					}
				}
			}
			Boolean ownerHasChanged = false;
			for(Opportunity o : Trigger.new)
			{
				if(trigger.oldMap.get(o.Id).OwnerId != o.ownerId )
					ownerHasChanged = true;
			}


			if(ownerHasChanged || !newProductCategories.isEmpty())
				OpportunityTriggerUtil.updateExistingOpportunitiesSalesTeam(Trigger.new);
		}
	}

	if( trigger.isBefore && trigger.isUpdate )
	{
		/* 10/12 - Taken out to remove Account Vertical field and functionality on Opportunity to remove an inconsistent error. */
		/*
		List<Account> populatedAccounts = OpportunityTriggerUtil.populateAccountVerticalField(trigger.new);
		if(! populatedAccounts.isEmpty()){
		    OpportunityTriggerUtil.updateAccounts(populatedAccounts, trigger.new);
		}
		*/
		
		OpportunityTriggerUtil.carryOverSalesTeamOnOwnerChange( trigger.newMap, trigger.oldMap );

		OpportunityTriggerUtil.updateRepFieldsOnAccountFieldChange( trigger.newMap, trigger.oldMap );
	}
	else if( trigger.isAfter && trigger.isInsert )
	{
		OpportunityTriggerUtil.insertSalesTeamForPSRAndIRep( trigger.newMap );

		List<Opportunity> openOpportunities = ChatterServices.filterOpenOpportunities( trigger.new );

		ChatterServices.subscribePSRandOwnerToSObject( openOpportunities, new Map<ID,Opportunity>());
	} 
	else if( trigger.isAfter && trigger.isUpdate )
	{
		OpportunityTriggerUtil.createTeamMembersAfterTriggerFires( trigger.newMap );
		OpportunityTriggerUtil.updateSalesTeamForPSRAndIRep( trigger.newMap, trigger.oldMap );

		List<Opportunity> openOpportunities = ChatterServices.filterOpenOpportunities( trigger.new );
		ChatterServices.subscribePSRandOwnerToSObject(openOpportunities, trigger.oldMap);
	}
	
	if( trigger.isAfter && trigger.isUpdate  )
	{
		List<Opportunity> filteredOpportunities = OpportunityTriggerUtil.filterBasedOnFieldUpdates( trigger.new, trigger.oldMap);
		if( !filteredOpportunities.isEmpty() )
		{
			List<Account> accountsToBeUpdated = OpportunityTriggerUtil.updateAccountFields( filteredOpportunities );
			OpportunityTriggerUtil.updateAccounts(accountsToBeUpdated, trigger.new);
		}
	}
}


 
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Code in OpportunityTriggerUtil class is throwing error. Share that code.