+ Start a Discussion
John AthitakisJohn Athitakis 

ystem.LimitException: Too many SOQL queries: 101

Have some very simple code. Basically when a custom object (sales area) is updated, I want all the related opportunities the not won/lost opps that are related to it to be updated as well (which for most sales areas, is just 5-6 opps atm since this is a new process). I've tried a few dozen ways to get around the error in the title, have moved my update out of a for look as well as the select statement, but cannot for the life of me understand what I'm doing wrong?
 
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
      if(checkRecursive.runOnce())
    {
    List<ID> salesAreaId = new List<ID>(); 
        
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
        
        List <Opportunity> updateOps=[SELECT Id FROM Opportunity WHERE IsClosed=False AND (Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in  :salesAreaId)];
        update updateOps;
        
    }
}
Best Answer chosen by John Athitakis
Mahesh DMahesh D
Hi John,

Take backup of your existing code and replace it with below code:
 
trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
//Used to update text fields within the Opportunity to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to BIZOPS-4428
//
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
    
    Set<Id> saIdSet = new Set<id>();
    
    for (Opportunity sa : Trigger.new){
        saIdSet.add(sa.Sales_Area__c);
		if(sa.Field_Rep_Overwrite__c != null)
            Field_Rep.add(sa.Field_Rep_Overwrite__c);   
        if(sa.Inside_Rep_Overwrite__c != null)
            Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
        if(sa.TSE1_Overwrite__c != null)
            TSE1.add(sa.TSE1_Overwrite__c);
        if(sa.TSE2_Overwrite__c != null)
            TSE2.add(sa.TSE2_Overwrite__c);
    }
    
    Map<Id, Sales_Area__c> saMap = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: saIdSet]);
	Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
    Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]);
    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]);
    
    for (Opportunity sa : Trigger.new){
        
        Sales_Area__c salesarea = saMap.get(sa.Sales_Area__C);
        if(salesarea.Inside_Rep_ID__c !=null){
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null)
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;
        }
        
        if(salesarea.Field_Rep_ID__c !=null){
            sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            
            if(sa.Field_Rep_At_Close_ID__c !=null)
                sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
                sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;
        }
        
        if(salesarea.TSE_ID__c !=null){
            sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  
            
            
        }
        
        if(sa.Secondary_Sales_Area__c != null){
            Sales_Area__c salesarea2 = saMap.get(sa.Sales_Area__C);
            if(salesarea.Inside_Rep_ID__c !=null){
                sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                
                if(sa.Inside_Rep_Split_At_Close_ID__c !=null)
                    sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
            }
            
            if(salesarea.Field_Rep_ID__c !=null){
                sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  
            }
            
            if(salesarea.TSE_ID__c !=null){
                sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;                
            }
        }
        
        if(sa.Field_Rep_Overwrite__c != null)
            sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
        else
            sa.Field_Rep_Overwrite_text__c ='';
        if(sa.Inside_Rep_Overwrite__c != null)
            sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
        else
            sa.Inside_Rep_Overwrite_text__c ='';

        if(sa.TSE1_Overwrite__c != null)
            sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
        else
            sa.TSE1_Overwrite_text__c ='';
        if(sa.TSE2_Overwrite__c != null)
            sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
        else
            sa.TSE2_Overwrite_text__c ='';        
    }
}

Reduced one for loop, which will improve the performance.

In the similar way if we can re-factor the other code, for sure you will not get CPU error.
If you still gets into then we will think of @future / batch apex.

Please do let me know if it helps.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi John,

There may be an issue with the other triggers on the Opportunity.

Just to test it, disable all the Triggers on Opportunity and try testing it.

Regards,
Mahesh
John AthitakisJohn Athitakis

Here is my other code. I do not believe this can be the source of the problem? I will try to disable it later tonight--are you seeing anything wrong here? (I had a itch that my first trigger had no problems...)

 

trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
//Used to update text fields within the Opportunity to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to BIZOPS-4428
//
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
    for (Opportunity sa : Trigger.new){
    //
    //
   
    Sales_Area__c salesarea = [select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id =: sa.Sales_Area__c];
        if(salesarea.Inside_Rep_ID__c !=null){
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null)
            sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
            sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;
           
        }
        
        
        if(salesarea.Field_Rep_ID__c !=null){
          sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            
          if(sa.Field_Rep_At_Close_ID__c !=null)
            sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
            sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;  
      
        }
        if(salesarea.TSE_ID__c !=null){
          sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  
            
            
        }
        
        if(sa.Secondary_Sales_Area__c != null){
             Sales_Area__c salesarea2 = [select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id =: sa.Sales_Area__c];
            if(salesarea.Inside_Rep_ID__c !=null){
              sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                
            if(sa.Inside_Rep_Split_At_Close_ID__c !=null)
            sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
                
                
            }
            if(salesarea.Field_Rep_ID__c !=null){
              sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                
            if(sa.Field_Rep_Split_At_Close_ID__c !=null)
            sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  
            }
            
            if(salesarea.TSE_ID__c !=null){
              sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                
            if(sa.Field_Rep_Split_At_Close_ID__c !=null)
            sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;     
                
            } 
            
            
        }
        
        
     if(sa.Field_Rep_Overwrite__c != null)
             Field_Rep.add(sa.Field_Rep_Overwrite__c);   
     if(sa.Inside_Rep_Overwrite__c != null)
             Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
     if(sa.TSE1_Overwrite__c != null)
         	 TSE1.add(sa.TSE1_Overwrite__c);
     if(sa.TSE2_Overwrite__c != null)
 			 TSE2.add(sa.TSE2_Overwrite__c);   
        

       }

    Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
	Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]); 

    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]); 

    for (Opportunity sa : Trigger.new){
    if(sa.Field_Rep_Overwrite__c != null)
        sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
    	else
        sa.Field_Rep_Overwrite_text__c ='';
    if(sa.Inside_Rep_Overwrite__c != null)
        sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
    	else
        sa.Inside_Rep_Overwrite_text__c ='';

    if(sa.TSE1_Overwrite__c != null)
        sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
    	else
        sa.TSE1_Overwrite_text__c ='';
    if(sa.TSE2_Overwrite__c != null)
        sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
    	else
        sa.TSE2_Overwrite_text__c ='';
    }

}
Mahesh DMahesh D
Yes, It contains SOQL inside the for loop.
Mahesh DMahesh D
Hi John,

I modified the above trigger as below:
 
trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
//Used to update text fields within the Opportunity to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to BIZOPS-4428
//
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
	
	Set<Id> saIdSet = new Set<>();
	
	for (Opportunity sa : Trigger.new){
		saIdSet.add(sa.Sales_Area__c);
	}
	
	Map<Id, Sales_Area__c> saMap = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: saIdSet);
	
    for (Opportunity sa : Trigger.new){
		
		Sales_Area__c salesarea = saMap.get(sa.Sales_Area__C);
        if(salesarea.Inside_Rep_ID__c !=null){
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null)
				sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
				sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;
        }
        
        if(salesarea.Field_Rep_ID__c !=null){
            sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            
            if(sa.Field_Rep_At_Close_ID__c !=null)
				sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
				sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;
        }
		
        if(salesarea.TSE_ID__c !=null){
			sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  
            
            
        }
        
        if(sa.Secondary_Sales_Area__c != null){
            Sales_Area__c salesarea2 = saMap.get(sa.Sales_Area__C);
            if(salesarea.Inside_Rep_ID__c !=null){
				sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                
				if(sa.Inside_Rep_Split_At_Close_ID__c !=null)
					sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
            }
			
            if(salesarea.Field_Rep_ID__c !=null){
				sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                
				if(sa.Field_Rep_Split_At_Close_ID__c !=null)
					sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  
			}
            
            if(salesarea.TSE_ID__c !=null){
				sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                
				if(sa.Field_Rep_Split_At_Close_ID__c !=null)
					sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;                
            }
        }
        
        
		if(sa.Field_Rep_Overwrite__c != null)
            Field_Rep.add(sa.Field_Rep_Overwrite__c);   
		if(sa.Inside_Rep_Overwrite__c != null)
            Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
		if(sa.TSE1_Overwrite__c != null)
         	TSE1.add(sa.TSE1_Overwrite__c);
		if(sa.TSE2_Overwrite__c != null)
 			TSE2.add(sa.TSE2_Overwrite__c);
    }

    Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
	Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]); 

    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]); 

    for (Opportunity sa : Trigger.new){
		if(sa.Field_Rep_Overwrite__c != null)
			sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
		else
			sa.Field_Rep_Overwrite_text__c ='';
		if(sa.Inside_Rep_Overwrite__c != null)
			sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
		else
			sa.Inside_Rep_Overwrite_text__c ='';

		if(sa.TSE1_Overwrite__c != null)
			sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
		else
			sa.TSE1_Overwrite_text__c ='';
		if(sa.TSE2_Overwrite__c != null)
			sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
		else
			sa.TSE2_Overwrite_text__c ='';
    }
}

First try to de-activate it and perform the test.
Next, activate the trigger, take a backup of your code and replace it above code and perform the test.

Regards,
Mahesh
Amit Chaudhary 8Amit Chaudhary 8
Please check below post how to resolve the issue
1) http://help.salesforce.com/apex/HTViewSolution?id=000181404&language=en_US
2) https://help.salesforce.com/apex/HTViewSolution?id=000213152&language=en_US
Here are some best practices that will stop the error messages and/or help you avoid hitting the Governors Limit: 

 
1. Since Apex runs on a Multitenant structure, Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources. Learn about the Governors Limit.
2. Avoid SOQL queries that are inside FOR loop. 
3. Check out the Salesforce Developer Blog where you can find Best Practices for Triggers.
4. Review best practices for Trigger and Bulk requests on our Force.com Apex Code Developer's Guide. 
5. Be sure you're following the key coding principals for Apex Code in our Developer's Guide.

Let us know if this will help you

Thanks
Amit Chaudhary
John AthitakisJohn Athitakis

So, this seems to eliminate the error (Thank you Mahesh) but the original code does not seem to be updating the Opportunities.

The ideal is that editing the sales area would cause the opportunities to 'update', in turn firing off the apex trigger that you helped fix for me. 

Basically there are 2 lookups that relate an Opp to a Sales area (Sales_Area__c and Secondary_Sales_Area__c).

I feel something is wrong here and that maybe my list or update is wrong? 

John AthitakisJohn Athitakis

Notably, doing the following reintroduces the 101 error even with the Opportunity Trigger disabled

1. Changing my Sales Area Trigger to a before
2. Removing the recursive check (to be expected I imagine)
3. I updated the code to the below with the opp trigger disabled and I'm seeing unexpected behavior. I would expect that of the 30-40 related opps in my sandbox, that all of them would get a Best Case (Currency field) of 1000, but none of them are being updated.

Em I attempting to update all of these records incorrectly in my original code? I'm tempted to say my List is empty (Although I'm certain the logic is sound). 

 

trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
      if(checkRecursive.runOnce())
    {
    List<ID> salesAreaId = new List<ID>(); 
        
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
        
        List <Opportunity> updateOps=[SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId];
        for(Opportunity op: updateOps){
            op.Best_Case__c =  1000;
            update op;
           }
        update updateOps;
        //IsClosed=False AND (
    } 
}
Mahesh DMahesh D
Remove line 13 as it is not required.
 
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
      if(checkRecursive.runOnce())
    {
    List<ID> salesAreaId = new List<ID>(); 
        
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
        
        List <Opportunity> updateOps=[SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId];
        for(Opportunity op: updateOps){
            op.Best_Case__c =  1000;
            
           }
        update updateOps;
        
    } 
}

Regards,
Mahesh
John AthitakisJohn Athitakis
I did---no difference. Children are not being updated. I included it just to see if I could force it to function. Scratching my head on this. 

I simplified down my select statement, trying to take out any noise. Currently -this- is doing nothing it seems (none of the fields im forcing in the for loop to update are actually updating.  (Note: PO_Number__c is a text string). 
 
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
      if(checkRecursive.runOnce())
    {
    List<ID> salesAreaId = new List<ID>(); 
        
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
        
        List <Opportunity> updateOps=[SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId];
        for(Opportunity op: updateOps){
            op.PO_Number__c = '1000';
          //  update op;
           }
        update updateOps;
        //IsClosed=False AND (    OR Secondary_Sales_Area__c in :salesAreaId
    } 
}

 
Mahesh DMahesh D
Hi John,

Please enable the debug log and do test.
 
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
    
    Set<ID> salesAreaId = new Set<ID>(); 
        
	for(Sales_Area__c sa : Trigger.new){
		salesAreaId.add(sa.id);
	}
	System.debug('===========salesAreaId:'+salesAreaId);
	List <Opportunity> updateOps = [SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId];
	System.debug('===========updateOps:'+updateOps);
	if(!updateOps.isEmpty()) {
		for(Opportunity op: updateOps){
			op.Best_Case__c =  1000;
			System.debug('================op:'+op);
		}
		update updateOps;
    }
}

Paste the debug log once you are done with test.

Regards,
Mahesh
John AthitakisJohn Athitakis
After adapting your code and trying out the update, I have been receiving APEX CPU time outs---which persists now even after I revert back to my old code. I have disabled triggers on opportunity as well --so I'm confused as to what is causing the issue. 

It might be related to this (unsure). There are two other triggers that exists at Sales Area, which updates the users per below. It could be the reason for it? Both are however before triggers, while this one is after. 
 
//Used to update text fields within the Sales Area to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to 

trigger SalesAreaReferenceUpdate on Sales_Area__c (before insert, before update) {
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    
    for (Sales_Area__c sa : Trigger.new){
    //
     if(sa.Field_Rep__c != null)
             Field_Rep.add(sa.Field_Rep__c);   
     if(sa.Inside_Rep__c != null)
             Inside_Rep.add(sa.Inside_Rep__c);
     if(sa.TSE1__c != null)
            TSE1.add(sa.TSE1__c);      
        //
       }
    // query for all the User records for the unique userIds in the records
    // create a map for a lookup / hash table for the user info
    Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
  Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]); 
    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    // iterate over the list of records being processed in the trigger and
    // set the color before being inserted or updated
    for (Sales_Area__c sa : Trigger.new){
    if(sa.Field_Rep__c != null)
        sa.Field_Rep_Name_text__c = Field_Rep_Names.get(sa.Field_Rep__c).Name;   
      else
        sa.Field_Rep_Name_text__c ='';
    if(sa.Inside_Rep__c != null)
        sa.Inside_Rep_Name_text__c = Inside_Rep_Names.get(sa.Inside_Rep__c).Name;   
      else
        sa.Inside_Rep_Name_text__c ='';
    if(sa.TSE1__c != null)
        sa.TSE1_Name_text__c = TSE1_Names.get(sa.TSE1__c).Name;   
      else
        sa.TSE1_Name_text__c ='';
    }
}
 
//Used to update User Records in SFDC. 'Abacus' code.
//Author: John Athitakis
//Relates to 

trigger UpdateSalesAreaToUserCount on Sales_Area__c (after insert, after update) {
if(Trigger.isUpdate){
	Set<Id> fieldRepIds = new Set<Id>();
    Set<Id> insideRepIds = new Set<Id>();
    Set<Id> tse1Ids=new Set<Id>();
    List<Sales_Area__c> cosChanged = new List<Sales_Area__c>();
    double counter=0;
	//filter for Sales Areas that have had a change in Field Rep
	for(Sales_Area__c co : Trigger.new){
		Sales_Area__c coOld = Trigger.oldMap.get(co.Id);
		if(coOld.Field_Rep__c != co.Field_Rep__c){
			fieldRepIds.add(coOld.Field_Rep__c);
			fieldRepIds.add(co.Field_Rep__c);
            counter++;
           

		}
        if(coOld.Inside_Rep__c != co.Inside_Rep__c){
			InsideRepIds.add(coOld.Inside_Rep__c);
			InsideRepIds.add(co.Inside_Rep__c);
            counter++;
		}
         if(coOld.TSE1__c != co.TSE1__c){
			tse1Ids.add(coOld.TSE1__c);
			tse1Ids.add(co.TSE1__c);
            counter++;
		}
        if(counter>0){
            cosChanged.add(co);
            counter=0;
            }
	}

	if(!fieldRepIds.isEmpty()){
		//get the user records for the field reps, including the Sales Area Field Rep Total
		Map<Id,User> fieldReps = new Map<Id,User>([SELECT Sales_Area_Field_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: fieldRepIds]);
	
		for(Sales_Area__c co : cosChanged){
			Sales_Area__c coOld = Trigger.oldMap.get(co.Id);

			//decrement Sales Area Field Rep for the old field rep value
			if(!String.isBlank(coOld.Field_Rep__c)){
				User u = fieldReps.get(coOld.Field_Rep__c);
                String uid = u.id;
                if(u.Sales_Area_Field_Rep__c == null)
               	u.Sales_Area_Field_Rep__c =0;
                else
				u.Sales_Area_Field_Rep__c --;
                
                if(u.Sales_Area_Total_Areas__c==1){
                List<Sales_Area__c> safr= [select id, name from Sales_Area__c where Field_Rep__c = :u.id OR Inside_Rep__c = :u.id OR TSE1__c = :u.id LIMIT 1];
                     if(!safr.isEmpty()){
      			    u.Default_Sales_Area_ID__c = safr[0].id;
    			      u.Default_Sales_Area_Name__c = safr[0].name;
         				}
                    }
                    else {
  					  u.Default_Sales_Area_ID__c = null;
    			      u.Default_Sales_Area_Name__c = null;
						}
                    
                 
			}

			//increment Sales Area Field rep for the new field rep value
			if(!String.isBlank(co.Field_Rep__c)){
				User u = fieldReps.get(co.Field_Rep__c);
                if(u.Sales_Area_Field_Rep__c == null)
                u.Sales_Area_Field_Rep__c =1;
                else
				u.Sales_Area_Field_Rep__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
                
			}
		}

		update fieldReps.values();
	}
	//Inside Rep
    if(!insideRepIds.isEmpty()){
		//get the user records for the Inside reps, including the Sales Area Inside Rep Total
		Map<Id,User> insideReps = new Map<Id,User>([SELECT Sales_Area_Inside_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: insideRepIds]);
	
		for(Sales_Area__c co : cosChanged){
			Sales_Area__c coOld = Trigger.oldMap.get(co.Id);

			//decrement Sales Area Inside Rep for the old inside rep value
			if(!String.isBlank(coOld.Inside_Rep__c)){
				User u = insideReps.get(coOld.Inside_Rep__c);
                if(u.Sales_Area_Inside_Rep__c == null)
               	u.Sales_Area_Inside_Rep__c =0;
                else
				u.Sales_Area_Inside_Rep__c --;
                
                
                if(u.Sales_Area_Total_Areas__c==1){
                List<Sales_Area__c> safr= [select id, name from Sales_Area__c where Field_Rep__c = :u.id OR Inside_Rep__c = :u.id OR TSE1__c = :u.id LIMIT 1];
                     if(!safr.isEmpty()){
      			    u.Default_Sales_Area_ID__c = safr[0].id;
    			      u.Default_Sales_Area_Name__c = safr[0].name;
         				}
                    }
                    else {
  					  u.Default_Sales_Area_ID__c = null;
    			      u.Default_Sales_Area_Name__c = null;
						}
			}

			//increment Sales Area Inside rep for the new field rep value
			if(!String.isBlank(co.Inside_Rep__c)){
				User u = insideReps.get(co.Inside_Rep__c);
                if(u.Sales_Area_Inside_Rep__c == null)
                u.Sales_Area_Inside_Rep__c =1;
                else
				u.Sales_Area_Inside_Rep__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
			}
		}
		update insideReps.values();
	}
    
    //TSE1
    if(!tse1Ids.isEmpty()){
		//get the user records for the Inside reps, including the Sales Area Inside Rep Total
		Map<Id,User> tse1Reps = new Map<Id,User>([SELECT Sales_Area_TSE1__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: tse1Ids]);
	
		for(Sales_Area__c co : cosChanged){
			Sales_Area__c coOld = Trigger.oldMap.get(co.Id);

			//decrement Sales Area Inside Rep for the old inside rep value
			if(!String.isBlank(coOld.TSE1__c)){
				User u = tse1Reps.get(coOld.TSE1__c);
                if(u.Sales_Area_TSE1__c == null)
               	u.Sales_Area_TSE1__c =0;
                else
				u.Sales_Area_TSE1__c --;
                if(u.Sales_Area_Total_Areas__c==1){
                List<Sales_Area__c> safr= [select id, name from Sales_Area__c where Field_Rep__c = :u.id OR Inside_Rep__c = :u.id OR TSE1__c = :u.id LIMIT 1];
                     if(!safr.isEmpty()){
      			    u.Default_Sales_Area_ID__c = safr[0].id;
    			      u.Default_Sales_Area_Name__c = safr[0].name;
         				}
                    }
                    else {
  					  u.Default_Sales_Area_ID__c = null;
    			      u.Default_Sales_Area_Name__c = null;
						}
                
			}

			//increment Sales Area Inside rep for the new field rep value
			if(!String.isBlank(co.TSE1__c)){
				User u = tse1Reps.get(co.TSE1__c);
                if(u.Sales_Area_TSE1__c == null)
                u.Sales_Area_TSE1__c =1;
                else
				u.Sales_Area_TSE1__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
			}
		}
		update tse1Reps.values();
	}    
}
    
  if(Trigger.isInsert){  

	Set<Id> fieldRepIds = new Set<Id>();
    Set<Id> insideRepIds = new Set<Id>();
    Set<Id> tse1Ids=new Set<Id>();
    List<Sales_Area__c> cosChanged = new List<Sales_Area__c>();
    double counter=0;
	//filter for Sales Areas that have had a change in Field Rep
	for(Sales_Area__c co : Trigger.new){
		if(!String.isBlank(co.Field_Rep__c)){
			fieldRepIds.add(co.Field_Rep__c);
            counter++;
		}
        if(!String.isBlank(co.Inside_Rep__c)){
			InsideRepIds.add(co.Inside_Rep__c);
            counter++;
		}
         if(!String.isBlank(co.TSE1__c)){
			tse1Ids.add(co.TSE1__c);
            counter++;
		}
        if(counter>0){
            cosChanged.add(co);
            counter=0;
            }
	}
	if(!fieldRepIds.isEmpty()){
		//get the user records for the field reps, including the Sales Area Field Rep Total
		Map<Id,User> fieldReps = new Map<Id,User>([SELECT Sales_Area_Field_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: fieldRepIds]);
	
		for(Sales_Area__c co : cosChanged){
			//increment Sales Area Field rep for the new field rep value
			if(!String.isBlank(co.Field_Rep__c)){
				User u = fieldReps.get(co.Field_Rep__c);
                if(u.Sales_Area_Field_Rep__c == null)
                u.Sales_Area_Field_Rep__c =1;
                else
				u.Sales_Area_Field_Rep__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
			}
		}
		update fieldReps.values();
	}
	//Inside Rep
    if(!insideRepIds.isEmpty()){
		//get the user records for the Inside reps, including the Sales Area Inside Rep Total
		Map<Id,User> insideReps = new Map<Id,User>([SELECT Sales_Area_Inside_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: insideRepIds]);
	
		for(Sales_Area__c co : cosChanged){		
			//increment Sales Area Inside rep for the new field rep value
			if(!String.isBlank(co.Inside_Rep__c)){
				User u = insideReps.get(co.Inside_Rep__c);
                if(u.Sales_Area_Inside_Rep__c == null)
                u.Sales_Area_Inside_Rep__c =1;
                else
				u.Sales_Area_Inside_Rep__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
			}
		}
		update insideReps.values();
	} 
    //TSE1
    if(!tse1Ids.isEmpty()){
		//get the user records for the Inside reps, including the Sales Area Inside Rep Total
		Map<Id,User> tse1Reps = new Map<Id,User>([SELECT Sales_Area_TSE1__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id in: tse1Ids]);
	
		for(Sales_Area__c co : cosChanged){
			//increment Sales Area Inside rep for the new field rep value
			if(!String.isBlank(co.TSE1__c)){
				User u = tse1Reps.get(co.TSE1__c);
                if(u.Sales_Area_TSE1__c == null)
                u.Sales_Area_TSE1__c =1;
                else
				u.Sales_Area_TSE1__c ++;
                if(u.Sales_Area_Total_Areas__c==0){
				u.Default_Sales_Area_ID__c=co.id;
                u.Default_Sales_Area_Name__c=co.name;
                }
                else{
				u.Default_Sales_Area_ID__c=null;
                u.Default_Sales_Area_Name__c=null;
                }
			}
		}
		update tse1Reps.values();
	}      
  }
}

 
John AthitakisJohn Athitakis
Edit; obviously one one is a before trigger, the 'abbacus' code is an after. 

Might be one of these is being greedy and causing the issues with my much simpler 'update child opportunities' code..?
John AthitakisJohn Athitakis
Ok I doubt its the above code. Disabled both, got same error. Checking other things . 
Mahesh DMahesh D
Hi John,

Please make sure that you are not having any SOQL queries inside for loop.

Regards,
Mahesh
John AthitakisJohn Athitakis

Hello Mahesh. 

I totally agree. However, I've disabled all other code and getting the CPU APEX limit error, so I'm not entirely sure what it is going on and believe it might be an installed package. I will research until then. I believe the issue originally (the Trigger not updating the Opportunity) is likely still an issue. I will update ticket soon as I clear up the apex cpu issue. 

Mahesh DMahesh D
Ok Sure John, enable the debug and go through the debug and see what is happening.

Regards,
Mahesh
John AthitakisJohn Athitakis
Whatever the apex issue was, it seems to have cleared up! All code is working while activated, no errors, but the issue (where the primary code is not updating the opportunities) persists.

Here is the log.
35.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:13:36.0 (551660)|EXECUTION_STARTED
15:13:36.0 (585091)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
15:13:36.0 (615464)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkH|SalesAreaReferenceUpdate on Sales_Area trigger event BeforeUpdate for [a0OG000000tthFi]
15:13:36.0 (6198748)|SOQL_EXECUTE_BEGIN|[23]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
15:13:36.0 (9046962)|SOQL_EXECUTE_END|[23]|Rows:1
15:13:36.0 (9402781)|SOQL_EXECUTE_BEGIN|[24]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
15:13:36.0 (11320047)|SOQL_EXECUTE_END|[24]|Rows:1
15:13:36.0 (11564630)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
15:13:36.0 (13650595)|SOQL_EXECUTE_END|[25]|Rows:1
15:13:36.14 (14182273)|CUMULATIVE_LIMIT_USAGE
15:13:36.14 (14182273)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100
  Number of query rows: 3 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:13:36.14 (14182273)|CUMULATIVE_LIMIT_USAGE_END

15:13:36.0 (15708084)|CODE_UNIT_FINISHED|SalesAreaReferenceUpdate on Sales_Area trigger event BeforeUpdate for [a0OG000000tthFi]
15:13:36.0 (74245152)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkJ|UpdateSalesAreaToUserCount on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
15:13:36.0 (75656200)|SOQL_EXECUTE_BEGIN|[40]|Aggregations:0|SELECT Sales_Area_Field_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id = :tmpVar1
15:13:36.0 (86059628)|SOQL_EXECUTE_END|[40]|Rows:2
15:13:36.0 (87307112)|DML_BEGIN|[88]|Op:Update|Type:User|Rows:2
15:13:36.0 (373016557)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkI|SalesAreaUserDefaults on User trigger event BeforeUpdate for [005G0000001Dabm, 005G0000001k0BQ]
15:13:36.0 (375065948)|SOQL_EXECUTE_BEGIN|[18]|Aggregations:0|SELECT id, name FROM Sales_Area__c WHERE Field_Rep__c = :tmpVar1 LIMIT 1
15:13:36.0 (379542358)|SOQL_EXECUTE_END|[18]|Rows:1
15:13:36.0 (380090438)|SOQL_EXECUTE_BEGIN|[27]|Aggregations:0|SELECT id, name FROM Sales_Area__c WHERE Inside_Rep__c = :tmpVar1 LIMIT 1
15:13:36.0 (383460780)|SOQL_EXECUTE_END|[27]|Rows:1
15:13:36.0 (383958308)|SOQL_EXECUTE_BEGIN|[36]|Aggregations:0|SELECT id, name FROM Sales_Area__c WHERE TSE1__c = :tmpVar1 LIMIT 1
15:13:36.0 (387880925)|SOQL_EXECUTE_END|[36]|Rows:1
15:13:36.388 (388343878)|CUMULATIVE_LIMIT_USAGE
15:13:36.388 (388343878)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 7 out of 100
  Number of query rows: 8 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:13:36.388 (388343878)|CUMULATIVE_LIMIT_USAGE_END

15:13:36.0 (390176167)|CODE_UNIT_FINISHED|SalesAreaUserDefaults on User trigger event BeforeUpdate for [005G0000001Dabm, 005G0000001k0BQ]
15:13:36.479 (479665035)|ENTERING_MANAGED_PKG|hoopla
15:13:36.525 (525346871)|CUMULATIVE_LIMIT_USAGE
15:13:36.525 (525346871)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 7 out of 100
  Number of query rows: 8 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 73 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:13:36.525 (525346871)|CUMULATIVE_LIMIT_USAGE_END

15:13:36.0 (526772456)|CODE_UNIT_FINISHED|UserUpdateRelatedLeadContactSalesAreas on User trigger event AfterUpdate for [005G0000001Dabm, 005G0000001k0BQ]
15:13:36.0 (526900707)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:User
15:13:36.0 (542266076)|WF_RULE_EVAL_BEGIN|Workflow
15:13:36.0 (542305831)|WF_CRITERIA_BEGIN|[User: John Athitakis 005G0000001Dabm]|QA - Territory Mgmt - Field Owner Added|01QG0000000gWtG|ON_ALL_CHANGES|0
15:13:36.0 (543027425)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]ISNEW() &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01C}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01E}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01D})&#124;&#124;

ISCHANGED({!ID:00NG000000FSZzW} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzX} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzY} )|Values:Inside_Territory__c=null, Budget_Manager__c=null, Field_Owner__c=null, Budget_Department__c=null, Authorized_Approval_Amount__c=0.00, Region__c=null
15:13:36.0 (543041363)|WF_CRITERIA_END|false
15:13:36.0 (543064742)|WF_CRITERIA_BEGIN|[User: Tiffany Longworth 005G0000001k0BQ]|QA - Territory Mgmt - Field Owner Added|01QG0000000gWtG|ON_ALL_CHANGES|0
15:13:36.0 (543278432)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]ISNEW() &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01C}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01E}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01D})&#124;&#124;

ISCHANGED({!ID:00NG000000FSZzW} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzX} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzY} )|Values:Inside_Territory__c=null, Budget_Manager__c=null, Field_Owner__c=null, Budget_Department__c=null, Authorized_Approval_Amount__c=0.0, Region__c=null
15:13:36.0 (543289621)|WF_CRITERIA_END|false
15:13:36.0 (543302678)|WF_SPOOL_ACTION_BEGIN|Workflow
15:13:36.0 (543308886)|WF_ACTION| None
15:13:36.0 (543311426)|WF_RULE_EVAL_END
15:13:36.0 (543362751)|WF_ACTIONS_END| None
15:13:36.0 (543369806)|CODE_UNIT_FINISHED|Workflow:User
15:13:36.0 (544240771)|DML_END|[88]
15:13:36.544 (544433602)|CUMULATIVE_LIMIT_USAGE
15:13:36.544 (544433602)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 7 out of 100
  Number of query rows: 8 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 73 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:13:36.544 (544433602)|CUMULATIVE_LIMIT_USAGE_END

15:13:36.0 (545873758)|CODE_UNIT_FINISHED|UpdateSalesAreaToUserCount on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
15:13:36.0 (621583986)|PUSH_TRACE_FLAGS|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:13:36.0 (621609692)|CODE_UNIT_STARTED|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
15:13:36.621 (621924245)|CUMULATIVE_LIMIT_USAGE
15:13:36.621 (621924245)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 7 out of 100
  Number of query rows: 8 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 73 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

15:13:36.621 (621924245)|CUMULATIVE_LIMIT_USAGE_END

15:13:36.0 (623194712)|CODE_UNIT_FINISHED|SalesAreaRelatedOpportunityUpdate on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
15:13:36.0 (623224217)|POP_TRACE_FLAGS|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:13:36.0 (670022038)|CODE_UNIT_FINISHED|TRIGGERS
15:13:36.0 (670039968)|EXECUTION_FINISHED


 
Mahesh DMahesh D
Hi John

Please use the below code so that we can get some debug messages.
 
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
    
    Set<ID> salesAreaId = new Set<ID>(); 
        
	for(Sales_Area__c sa : Trigger.new){
		salesAreaId.add(sa.id);
	}
	System.debug('===========salesAreaId:'+salesAreaId);
	List <Opportunity> updateOps = [SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId];
	System.debug('===========updateOps:'+updateOps);
	if(!updateOps.isEmpty()) {
		for(Opportunity op: updateOps){
			op.Best_Case__c =  1000;
			System.debug('================op:'+op);
		}
		update updateOps;
    }
}

Regards,
Mahesh
John AthitakisJohn Athitakis

Hello Mahesh, 
 

Here was the code I was running. I'm pulling the log off the dev console.

Is there a reason I should make the List into a Set? I changed it to a set (per what you have above). Removing the recursive check results in an APEX Cpu limit.

trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) {
      if(checkRecursive.runOnce())
    {
    List<ID> salesAreaId = new List<ID>(); 
        
        for (Sales_Area__c sa : Trigger.new){
        salesAreaId.add(sa.id);
        }
        
       System.debug('===========salesAreaId:'+salesAreaId);
    List <Opportunity> updateOps = [SELECT Id FROM Opportunity WHERE Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId];
    System.debug('===========updateOps:'+updateOps);
    if(!updateOps.isEmpty()) {
        for(Opportunity op: updateOps){
            op.Best_Case__c =  1000;
            System.debug('================op:'+op);
        }

        update updateOps;
        //IsClosed=False AND (
    } 
}
}
Here is the log I generated with this.
 
35.0 APEX_CODE,FINE;APEX_PROFILING,INFO;CALLOUT,INFO;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,FINE
10:14:00.0 (680018)|EXECUTION_STARTED
10:14:00.0 (722384)|CODE_UNIT_STARTED|[EXTERNAL]|TRIGGERS
10:14:00.0 (761324)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkH|SalesAreaReferenceUpdate on Sales_Area trigger event BeforeUpdate for [a0OG000000tthFi]
10:14:00.0 (1943532)|SYSTEM_CONSTRUCTOR_ENTRY|[7]|<init>(Integer)
10:14:00.0 (1987495)|SYSTEM_CONSTRUCTOR_EXIT|[7]|<init>(Integer)
10:14:00.0 (2175865)|SYSTEM_CONSTRUCTOR_ENTRY|[8]|<init>(Integer)
10:14:00.0 (2198515)|SYSTEM_CONSTRUCTOR_EXIT|[8]|<init>(Integer)
10:14:00.0 (2230506)|SYSTEM_CONSTRUCTOR_ENTRY|[9]|<init>(Integer)
10:14:00.0 (2249317)|SYSTEM_CONSTRUCTOR_EXIT|[9]|<init>(Integer)
10:14:00.0 (2424008)|SYSTEM_METHOD_ENTRY|[11]|List<Sales_Area__c>.iterator()
10:14:00.0 (2587630)|SYSTEM_METHOD_EXIT|[11]|List<Sales_Area__c>.iterator()
10:14:00.0 (2614638)|SYSTEM_METHOD_ENTRY|[11]|system.ListIterator.hasNext()
10:14:00.0 (2645536)|SYSTEM_METHOD_EXIT|[11]|system.ListIterator.hasNext()
10:14:00.0 (2772257)|SYSTEM_METHOD_ENTRY|[14]|Set<Id>.add(Object)
10:14:00.0 (2808558)|SYSTEM_METHOD_EXIT|[14]|Set<Id>.add(Object)
10:14:00.0 (2878622)|SYSTEM_METHOD_ENTRY|[16]|Set<Id>.add(Object)
10:14:00.0 (2906957)|SYSTEM_METHOD_EXIT|[16]|Set<Id>.add(Object)
10:14:00.0 (2974858)|SYSTEM_METHOD_ENTRY|[18]|Set<Id>.add(Object)
10:14:00.0 (3002661)|SYSTEM_METHOD_EXIT|[18]|Set<Id>.add(Object)
10:14:00.0 (3012025)|SYSTEM_METHOD_ENTRY|[11]|system.ListIterator.hasNext()
10:14:00.0 (3022798)|SYSTEM_METHOD_EXIT|[11]|system.ListIterator.hasNext()
10:14:00.0 (3852876)|SOQL_EXECUTE_BEGIN|[23]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
10:14:00.0 (6872271)|SOQL_EXECUTE_END|[23]|Rows:1
10:14:00.0 (7207664)|SOQL_EXECUTE_BEGIN|[24]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
10:14:00.0 (8913240)|SOQL_EXECUTE_END|[24]|Rows:1
10:14:00.0 (9161011)|SOQL_EXECUTE_BEGIN|[25]|Aggregations:0|SELECT Name FROM User WHERE Id = :tmpVar1
10:14:00.0 (10995619)|SOQL_EXECUTE_END|[25]|Rows:1
10:14:00.0 (11092035)|SYSTEM_METHOD_ENTRY|[28]|List<Sales_Area__c>.iterator()
10:14:00.0 (11112578)|SYSTEM_METHOD_EXIT|[28]|List<Sales_Area__c>.iterator()
10:14:00.0 (11121825)|SYSTEM_METHOD_ENTRY|[28]|system.ListIterator.hasNext()
10:14:00.0 (11132731)|SYSTEM_METHOD_EXIT|[28]|system.ListIterator.hasNext()
10:14:00.0 (11181387)|SYSTEM_METHOD_ENTRY|[30]|Map<Id,User>.get(Object)
10:14:00.0 (11209512)|SYSTEM_METHOD_EXIT|[30]|Map<Id,User>.get(Object)
10:14:00.0 (11326110)|SYSTEM_METHOD_ENTRY|[34]|Map<Id,User>.get(Object)
10:14:00.0 (11351033)|SYSTEM_METHOD_EXIT|[34]|Map<Id,User>.get(Object)
10:14:00.0 (11424700)|SYSTEM_METHOD_ENTRY|[38]|Map<Id,User>.get(Object)
10:14:00.0 (11448509)|SYSTEM_METHOD_EXIT|[38]|Map<Id,User>.get(Object)
10:14:00.0 (11502161)|SYSTEM_METHOD_ENTRY|[28]|system.ListIterator.hasNext()
10:14:00.0 (11515334)|SYSTEM_METHOD_EXIT|[28]|system.ListIterator.hasNext()
10:14:00.11 (11534733)|CUMULATIVE_LIMIT_USAGE
10:14:00.11 (11534733)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100
  Number of query rows: 3 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

10:14:00.11 (11534733)|CUMULATIVE_LIMIT_USAGE_END

10:14:00.0 (13498544)|CODE_UNIT_FINISHED|SalesAreaReferenceUpdate on Sales_Area trigger event BeforeUpdate for [a0OG000000tthFi]
10:14:00.0 (53943299)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkJ|UpdateSalesAreaToUserCount on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
10:14:00.0 (54313013)|SYSTEM_CONSTRUCTOR_ENTRY|[7]|<init>(Integer)
10:14:00.0 (54366107)|SYSTEM_CONSTRUCTOR_EXIT|[7]|<init>(Integer)
10:14:00.0 (54416207)|SYSTEM_CONSTRUCTOR_ENTRY|[8]|<init>(Integer)
10:14:00.0 (54452640)|SYSTEM_CONSTRUCTOR_EXIT|[8]|<init>(Integer)
10:14:00.0 (54492210)|SYSTEM_CONSTRUCTOR_ENTRY|[9]|<init>(Integer)
10:14:00.0 (54519806)|SYSTEM_CONSTRUCTOR_EXIT|[9]|<init>(Integer)
10:14:00.0 (54563551)|SYSTEM_CONSTRUCTOR_ENTRY|[10]|<init>()
10:14:00.0 (54586028)|SYSTEM_CONSTRUCTOR_EXIT|[10]|<init>()
10:14:00.0 (54670152)|SYSTEM_METHOD_ENTRY|[13]|List<Sales_Area__c>.iterator()
10:14:00.0 (54701995)|SYSTEM_METHOD_EXIT|[13]|List<Sales_Area__c>.iterator()
10:14:00.0 (54725975)|SYSTEM_METHOD_ENTRY|[13]|system.ListIterator.hasNext()
10:14:00.0 (54751280)|SYSTEM_METHOD_EXIT|[13]|system.ListIterator.hasNext()
10:14:00.0 (55002368)|SYSTEM_METHOD_ENTRY|[14]|Map<Id,Sales_Area__c>.get(Object)
10:14:00.0 (55055861)|SYSTEM_METHOD_EXIT|[14]|Map<Id,Sales_Area__c>.get(Object)
10:14:00.0 (55194243)|SYSTEM_METHOD_ENTRY|[15]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55246945)|SYSTEM_METHOD_EXIT|[15]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55292813)|SYSTEM_METHOD_ENTRY|[16]|Set<Id>.add(Object)
10:14:00.0 (55348227)|SYSTEM_METHOD_EXIT|[16]|Set<Id>.add(Object)
10:14:00.0 (55381827)|SYSTEM_METHOD_ENTRY|[17]|Set<Id>.add(Object)
10:14:00.0 (55424249)|SYSTEM_METHOD_EXIT|[17]|Set<Id>.add(Object)
10:14:00.0 (55496829)|SYSTEM_METHOD_ENTRY|[22]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55523640)|SYSTEM_METHOD_EXIT|[22]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55575482)|SYSTEM_METHOD_ENTRY|[27]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55599922)|SYSTEM_METHOD_EXIT|[27]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (55625138)|SYSTEM_METHOD_ENTRY|[33]|List<Sales_Area__c>.add(Object)
10:14:00.0 (55657059)|SYSTEM_METHOD_EXIT|[33]|List<Sales_Area__c>.add(Object)
10:14:00.0 (55673548)|SYSTEM_METHOD_ENTRY|[13]|system.ListIterator.hasNext()
10:14:00.0 (55684798)|SYSTEM_METHOD_EXIT|[13]|system.ListIterator.hasNext()
10:14:00.0 (55697965)|SYSTEM_METHOD_ENTRY|[38]|Set<Id>.isEmpty()
10:14:00.0 (55717005)|SYSTEM_METHOD_EXIT|[38]|Set<Id>.isEmpty()
10:14:00.0 (56097829)|SOQL_EXECUTE_BEGIN|[40]|Aggregations:0|SELECT Sales_Area_Field_Rep__c, Default_Sales_Area_ID__c, Default_Sales_Area_Name__c, Sales_Area_Total_Areas__c FROM User WHERE Id = :tmpVar1
10:14:00.0 (65394289)|SOQL_EXECUTE_END|[40]|Rows:2
10:14:00.0 (65508763)|SYSTEM_METHOD_ENTRY|[42]|List<Sales_Area__c>.iterator()
10:14:00.0 (65529521)|SYSTEM_METHOD_EXIT|[42]|List<Sales_Area__c>.iterator()
10:14:00.0 (65539255)|SYSTEM_METHOD_ENTRY|[42]|system.ListIterator.hasNext()
10:14:00.0 (65551062)|SYSTEM_METHOD_EXIT|[42]|system.ListIterator.hasNext()
10:14:00.0 (65596649)|SYSTEM_METHOD_ENTRY|[43]|Map<Id,Sales_Area__c>.get(Object)
10:14:00.0 (65621362)|SYSTEM_METHOD_EXIT|[43]|Map<Id,Sales_Area__c>.get(Object)
10:14:00.0 (65670381)|SYSTEM_METHOD_ENTRY|[46]|String.isBlank(String)
10:14:00.0 (65694943)|SYSTEM_METHOD_EXIT|[46]|String.isBlank(String)
10:14:00.0 (65725146)|SYSTEM_METHOD_ENTRY|[47]|Map<Id,User>.get(Object)
10:14:00.0 (65764180)|SYSTEM_METHOD_EXIT|[47]|Map<Id,User>.get(Object)
10:14:00.0 (65935431)|SYSTEM_METHOD_ENTRY|[52]|Decimal.subtract(Decimal)
10:14:00.0 (66013616)|SYSTEM_METHOD_EXIT|[52]|Decimal.subtract(Decimal)
10:14:00.0 (66130632)|SYSTEM_METHOD_ENTRY|[54]|Decimal.compareTo(Decimal)
10:14:00.0 (66167997)|SYSTEM_METHOD_EXIT|[54]|Decimal.compareTo(Decimal)
10:14:00.0 (66255611)|SYSTEM_METHOD_ENTRY|[70]|String.isBlank(String)
10:14:00.0 (66278643)|SYSTEM_METHOD_EXIT|[70]|String.isBlank(String)
10:14:00.0 (66305358)|SYSTEM_METHOD_ENTRY|[71]|Map<Id,User>.get(Object)
10:14:00.0 (66328838)|SYSTEM_METHOD_EXIT|[71]|Map<Id,User>.get(Object)
10:14:00.0 (66378251)|SYSTEM_METHOD_ENTRY|[75]|Decimal.add(Decimal)
10:14:00.0 (66417990)|SYSTEM_METHOD_EXIT|[75]|Decimal.add(Decimal)
10:14:00.0 (66487242)|SYSTEM_METHOD_ENTRY|[76]|Decimal.compareTo(Decimal)
10:14:00.0 (66520950)|SYSTEM_METHOD_EXIT|[76]|Decimal.compareTo(Decimal)
10:14:00.0 (66583344)|SYSTEM_METHOD_ENTRY|[42]|system.ListIterator.hasNext()
10:14:00.0 (66595825)|SYSTEM_METHOD_EXIT|[42]|system.ListIterator.hasNext()
10:14:00.0 (66611450)|SYSTEM_METHOD_ENTRY|[88]|Map<Id,User>.values()
10:14:00.0 (66662707)|SYSTEM_METHOD_EXIT|[88]|Map<Id,User>.values()
10:14:00.0 (66734414)|DML_BEGIN|[88]|Op:Update|Type:User|Rows:2
10:14:00.0 (249035657)|CODE_UNIT_STARTED|[EXTERNAL]|01qG00000017HkI|SalesAreaUserDefaults on User trigger event BeforeUpdate for [005G0000001Dabm, 005G0000001k0BQ]
10:14:00.0 (249670163)|SYSTEM_METHOD_ENTRY|[4]|List<User>.iterator()
10:14:00.0 (249835802)|SYSTEM_METHOD_EXIT|[4]|List<User>.iterator()
10:14:00.0 (249868054)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
10:14:00.0 (249884153)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
10:14:00.0 (249968014)|SYSTEM_METHOD_ENTRY|[8]|Map<Id,User>.get(Object)
10:14:00.0 (249995925)|SYSTEM_METHOD_EXIT|[8]|Map<Id,User>.get(Object)
10:14:00.0 (250092000)|SYSTEM_METHOD_ENTRY|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (250143599)|SYSTEM_METHOD_EXIT|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (250220898)|SYSTEM_METHOD_ENTRY|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (250244098)|SYSTEM_METHOD_EXIT|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (250364224)|SYSTEM_METHOD_ENTRY|[16]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (250425137)|SYSTEM_METHOD_EXIT|[16]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (250521859)|SYSTEM_METHOD_ENTRY|[17]|Decimal.compareTo(Decimal)
10:14:00.0 (250568067)|SYSTEM_METHOD_EXIT|[17]|Decimal.compareTo(Decimal)
10:14:00.0 (250643915)|SYSTEM_METHOD_ENTRY|[26]|Decimal.compareTo(Decimal)
10:14:00.0 (250679863)|SYSTEM_METHOD_EXIT|[26]|Decimal.compareTo(Decimal)
10:14:00.0 (250885555)|SYSTEM_METHOD_ENTRY|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (250918908)|SYSTEM_METHOD_EXIT|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (250954561)|SYSTEM_METHOD_ENTRY|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (250973827)|SYSTEM_METHOD_EXIT|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (251066228)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
10:14:00.0 (251082366)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
10:14:00.0 (251134848)|SYSTEM_METHOD_ENTRY|[8]|Map<Id,User>.get(Object)
10:14:00.0 (251149136)|SYSTEM_METHOD_EXIT|[8]|Map<Id,User>.get(Object)
10:14:00.0 (251197178)|SYSTEM_METHOD_ENTRY|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (251211309)|SYSTEM_METHOD_EXIT|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (251248651)|SYSTEM_METHOD_ENTRY|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (251261818)|SYSTEM_METHOD_EXIT|[9]|Id.compareTo(Id, Id, Boolean)
10:14:00.0 (251310434)|SYSTEM_METHOD_ENTRY|[16]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (251331917)|SYSTEM_METHOD_EXIT|[16]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (251385134)|SYSTEM_METHOD_ENTRY|[17]|Decimal.compareTo(Decimal)
10:14:00.0 (251407342)|SYSTEM_METHOD_EXIT|[17]|Decimal.compareTo(Decimal)
10:14:00.0 (251471930)|SYSTEM_METHOD_ENTRY|[26]|Decimal.compareTo(Decimal)
10:14:00.0 (251503321)|SYSTEM_METHOD_EXIT|[26]|Decimal.compareTo(Decimal)
10:14:00.0 (251871209)|SOQL_EXECUTE_BEGIN|[27]|Aggregations:0|SELECT id, name FROM Sales_Area__c WHERE Inside_Rep__c = :tmpVar1 LIMIT 1
10:14:00.0 (257513152)|SOQL_EXECUTE_END|[27]|Rows:1
10:14:00.0 (257582994)|SYSTEM_METHOD_ENTRY|[28]|List<Sales_Area__c>.isEmpty()
10:14:00.0 (257602182)|SYSTEM_METHOD_EXIT|[28]|List<Sales_Area__c>.isEmpty()
10:14:00.0 (257775616)|SYSTEM_METHOD_ENTRY|[35]|Decimal.compareTo(Decimal)
10:14:00.0 (257810844)|SYSTEM_METHOD_EXIT|[35]|Decimal.compareTo(Decimal)
10:14:00.0 (258023797)|SOQL_EXECUTE_BEGIN|[36]|Aggregations:0|SELECT id, name FROM Sales_Area__c WHERE TSE1__c = :tmpVar1 LIMIT 1
10:14:00.0 (279202528)|SOQL_EXECUTE_END|[36]|Rows:1
10:14:00.0 (279321004)|SYSTEM_METHOD_ENTRY|[37]|List<Sales_Area__c>.isEmpty()
10:14:00.0 (279357788)|SYSTEM_METHOD_EXIT|[37]|List<Sales_Area__c>.isEmpty()
10:14:00.0 (279739337)|SYSTEM_METHOD_ENTRY|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (279769589)|SYSTEM_METHOD_EXIT|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (279804687)|SYSTEM_METHOD_ENTRY|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (279824984)|SYSTEM_METHOD_EXIT|[44]|Decimal.compareTo(Decimal)
10:14:00.0 (279891335)|SYSTEM_METHOD_ENTRY|[4]|system.ListIterator.hasNext()
10:14:00.0 (279905936)|SYSTEM_METHOD_EXIT|[4]|system.ListIterator.hasNext()
10:14:00.279 (279933950)|CUMULATIVE_LIMIT_USAGE
10:14:00.279 (279933950)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 6 out of 100
  Number of query rows: 7 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

10:14:00.279 (279933950)|CUMULATIVE_LIMIT_USAGE_END

10:14:00.0 (281939722)|CODE_UNIT_FINISHED|SalesAreaUserDefaults on User trigger event BeforeUpdate for [005G0000001Dabm, 005G0000001k0BQ]
10:14:00.385 (385154886)|ENTERING_MANAGED_PKG|hoopla
10:14:00.0 (402211354)|METHOD_ENTRY|[1]|01pG0000004jyBq|checkRecursive.checkRecursive()
10:14:00.0 (402237314)|METHOD_EXIT|[1]|checkRecursive
10:14:00.0 (402260789)|METHOD_ENTRY|[5]|01pG0000004jyBq|checkRecursive.runOnce()
10:14:00.0 (402305755)|METHOD_EXIT|[5]|01pG0000004jyBq|checkRecursive.runOnce()
10:14:00.0 (402335826)|SYSTEM_CONSTRUCTOR_ENTRY|[7]|<init>()
10:14:00.0 (402361519)|SYSTEM_CONSTRUCTOR_EXIT|[7]|<init>()
10:14:00.0 (402415852)|SYSTEM_METHOD_ENTRY|[8]|List<User>.iterator()
10:14:00.0 (402437794)|SYSTEM_METHOD_EXIT|[8]|List<User>.iterator()
10:14:00.0 (402447648)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
10:14:00.0 (402459221)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
10:14:00.0 (402531311)|SYSTEM_METHOD_ENTRY|[19]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (402568558)|SYSTEM_METHOD_EXIT|[19]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (402592055)|SYSTEM_METHOD_ENTRY|[20]|List<Id>.add(Object)
10:14:00.0 (402624822)|SYSTEM_METHOD_EXIT|[20]|List<Id>.add(Object)
10:14:00.0 (402633522)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
10:14:00.0 (402643605)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
10:14:00.0 (402692122)|SYSTEM_METHOD_ENTRY|[19]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (402711181)|SYSTEM_METHOD_EXIT|[19]|com.salesforce.api.fast.System.compareObjects(Object, Object)
10:14:00.0 (402727954)|SYSTEM_METHOD_ENTRY|[20]|List<Id>.add(Object)
10:14:00.0 (402744690)|SYSTEM_METHOD_EXIT|[20]|List<Id>.add(Object)
10:14:00.0 (402752550)|SYSTEM_METHOD_ENTRY|[8]|system.ListIterator.hasNext()
10:14:00.0 (402762520)|SYSTEM_METHOD_EXIT|[8]|system.ListIterator.hasNext()
10:14:00.402 (402779835)|CUMULATIVE_LIMIT_USAGE
10:14:00.402 (402779835)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 6 out of 100
  Number of query rows: 7 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 69 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

10:14:00.402 (402779835)|CUMULATIVE_LIMIT_USAGE_END

10:14:00.0 (404182946)|CODE_UNIT_FINISHED|UserUpdateRelatedLeadContactSalesAreas on User trigger event AfterUpdate for [005G0000001Dabm, 005G0000001k0BQ]
10:14:00.0 (404288062)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:User
10:14:00.0 (414105534)|WF_RULE_EVAL_BEGIN|Workflow
10:14:00.0 (414146812)|WF_CRITERIA_BEGIN|[User: John Athitakis 005G0000001Dabm]|QA - Territory Mgmt - Field Owner Added|01QG0000000gWtG|ON_ALL_CHANGES|0
10:14:00.0 (414881187)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]ISNEW() &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01C}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01E}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01D})&#124;&#124;

ISCHANGED({!ID:00NG000000FSZzW} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzX} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzY} )|Values:Inside_Territory__c=null, Budget_Manager__c=null, Field_Owner__c=null, Budget_Department__c=null, Authorized_Approval_Amount__c=0.00, Region__c=null
10:14:00.0 (414891409)|WF_CRITERIA_END|false
10:14:00.0 (414918012)|WF_CRITERIA_BEGIN|[User: Tiffany Longworth 005G0000001k0BQ]|QA - Territory Mgmt - Field Owner Added|01QG0000000gWtG|ON_ALL_CHANGES|0
10:14:00.0 (415143061)|WF_FORMULA|Formula:ENCODED:[treatNullAsNull]ISNEW() &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01C}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01E}) &#124;&#124;
ISCHANGED({!ID:00NG000000Cj01D})&#124;&#124;

ISCHANGED({!ID:00NG000000FSZzW} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzX} ) &#124;&#124;
ISCHANGED({!ID:00NG000000FSZzY} )|Values:Inside_Territory__c=null, Budget_Manager__c=null, Field_Owner__c=null, Budget_Department__c=null, Authorized_Approval_Amount__c=0.0, Region__c=null
10:14:00.0 (415151856)|WF_CRITERIA_END|false
10:14:00.0 (415166988)|WF_SPOOL_ACTION_BEGIN|Workflow
10:14:00.0 (415172867)|WF_ACTION| None
10:14:00.0 (415175323)|WF_RULE_EVAL_END
10:14:00.0 (415217144)|WF_ACTIONS_END| None
10:14:00.0 (415222285)|CODE_UNIT_FINISHED|Workflow:User
10:14:00.0 (416271274)|DML_END|[88]
10:14:00.0 (416388431)|SYSTEM_METHOD_ENTRY|[91]|Set<Id>.isEmpty()
10:14:00.0 (416416344)|SYSTEM_METHOD_EXIT|[91]|Set<Id>.isEmpty()
10:14:00.0 (416429162)|SYSTEM_METHOD_ENTRY|[141]|Set<Id>.isEmpty()
10:14:00.0 (416453417)|SYSTEM_METHOD_EXIT|[141]|Set<Id>.isEmpty()
10:14:00.416 (416488140)|CUMULATIVE_LIMIT_USAGE
10:14:00.416 (416488140)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 6 out of 100
  Number of query rows: 7 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 69 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

10:14:00.416 (416488140)|CUMULATIVE_LIMIT_USAGE_END

10:14:00.0 (417996368)|CODE_UNIT_FINISHED|UpdateSalesAreaToUserCount on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
10:14:00.0 (423827666)|PUSH_TRACE_FLAGS|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate|APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
10:14:00.0 (423845361)|CODE_UNIT_STARTED|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
10:14:00.424 (424050900)|CUMULATIVE_LIMIT_USAGE
10:14:00.424 (424050900)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 6 out of 100
  Number of query rows: 7 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 2 out of 10000
  Maximum CPU time: 69 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

10:14:00.424 (424050900)|CUMULATIVE_LIMIT_USAGE_END

10:14:00.0 (425882645)|CODE_UNIT_FINISHED|SalesAreaRelatedOpportunityUpdate on Sales_Area trigger event AfterUpdate for [a0OG000000tthFi]
10:14:00.0 (426087794)|POP_TRACE_FLAGS|[EXTERNAL]|01q11000000CxOB|SalesAreaRelatedOpportunityUpdate|APEX_CODE,FINE;APEX_PROFILING,INFO;CALLOUT,INFO;DB,FINEST;SYSTEM,FINE;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,FINE
10:14:00.0 (461707273)|CODE_UNIT_FINISHED|TRIGGERS
10:14:00.0 (461724455)|EXECUTION_FINISHED

 
Mahesh DMahesh D
Please remove this checkRecursive if condition and try once.

Regards,
Mahesh
John AthitakisJohn Athitakis

Mahesh---Let me thank you. You are a super star!

Removing the checkRecursive with a salesarea with only 13 opportunities was successful.

Moving to one with over 100 opportunities resulted in an APEX CPU error however. 

The log is quite large (to large to post) and I can't seem to figure out what code could be causing the issue. Since it works with Sales Areas with very few opps but not larger ones, I'm inclined to think its the following apex code. 

I've read on @future. Would converting this code to @future help? It's not time sensitive--but I've never used the @future function before. 

 

trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
//Used to update text fields within the Opportunity to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to BIZOPS-4428
//
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
    
    Set<Id> saIdSet = new Set<id>();
    
    for (Opportunity sa : Trigger.new){
        saIdSet.add(sa.Sales_Area__c);
    }
    
    Map<Id, Sales_Area__c> saMap = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: saIdSet]);
    
    for (Opportunity sa : Trigger.new){
        
        Sales_Area__c salesarea = saMap.get(sa.Sales_Area__C);
        if(salesarea.Inside_Rep_ID__c !=null){
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null)
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;
        }
        
        if(salesarea.Field_Rep_ID__c !=null){
            sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            
            if(sa.Field_Rep_At_Close_ID__c !=null)
                sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
                sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;
        }
        
        if(salesarea.TSE_ID__c !=null){
            sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  
            
            
        }
        
        if(sa.Secondary_Sales_Area__c != null){
            Sales_Area__c salesarea2 = saMap.get(sa.Sales_Area__C);
            if(salesarea.Inside_Rep_ID__c !=null){
                sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                
                if(sa.Inside_Rep_Split_At_Close_ID__c !=null)
                    sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
            }
            
            if(salesarea.Field_Rep_ID__c !=null){
                sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  
            }
            
            if(salesarea.TSE_ID__c !=null){
                sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;                
            }
        }
        
        
        if(sa.Field_Rep_Overwrite__c != null)
            Field_Rep.add(sa.Field_Rep_Overwrite__c);   
        if(sa.Inside_Rep_Overwrite__c != null)
            Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
        if(sa.TSE1_Overwrite__c != null)
            TSE1.add(sa.TSE1_Overwrite__c);
        if(sa.TSE2_Overwrite__c != null)
            TSE2.add(sa.TSE2_Overwrite__c);
    }

    Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
    Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]); 

    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]); 

    for (Opportunity sa : Trigger.new){
        if(sa.Field_Rep_Overwrite__c != null)
            sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
        else
            sa.Field_Rep_Overwrite_text__c ='';
        if(sa.Inside_Rep_Overwrite__c != null)
            sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
        else
            sa.Inside_Rep_Overwrite_text__c ='';

        if(sa.TSE1_Overwrite__c != null)
            sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
        else
            sa.TSE1_Overwrite_text__c ='';
        if(sa.TSE2_Overwrite__c != null)
            sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
        else
            sa.TSE2_Overwrite_text__c ='';
    }
}
 

 

Mahesh DMahesh D
Hi John,

Thank you :-)

We are done with one big stopper issue :-)

Now, talking about your next issue, I have to go through the whole trigger and streamline it. Give me some time I will modify and post it here.
Also we can look into @future options, will give you openion on it as well in my next post.

Regards,
Mahesh
John AthitakisJohn Athitakis
Thank you Mahesh. I think (if you have time, I really appreciate it) if you can show me how you might @future this, I can apply that to some othere code I have. 

You are amazing ^_^ Thank you so much. 
Mahesh DMahesh D
Hi John,

Take backup of your existing code and replace it with below code:
 
trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
//Used to update text fields within the Opportunity to reduce the amount of references due to SFDC Limits
//Author: John Athitakis
//Relates to BIZOPS-4428
//
 // create a set of all the unique ownerIds
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
    
    Set<Id> saIdSet = new Set<id>();
    
    for (Opportunity sa : Trigger.new){
        saIdSet.add(sa.Sales_Area__c);
		if(sa.Field_Rep_Overwrite__c != null)
            Field_Rep.add(sa.Field_Rep_Overwrite__c);   
        if(sa.Inside_Rep_Overwrite__c != null)
            Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
        if(sa.TSE1_Overwrite__c != null)
            TSE1.add(sa.TSE1_Overwrite__c);
        if(sa.TSE2_Overwrite__c != null)
            TSE2.add(sa.TSE2_Overwrite__c);
    }
    
    Map<Id, Sales_Area__c> saMap = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: saIdSet]);
	Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
    Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]);
    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]);
    
    for (Opportunity sa : Trigger.new){
        
        Sales_Area__c salesarea = saMap.get(sa.Sales_Area__C);
        if(salesarea.Inside_Rep_ID__c !=null){
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null)
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;
        }
        
        if(salesarea.Field_Rep_ID__c !=null){
            sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            
            if(sa.Field_Rep_At_Close_ID__c !=null)
                sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
                sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;
        }
        
        if(salesarea.TSE_ID__c !=null){
            sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  
            
            
        }
        
        if(sa.Secondary_Sales_Area__c != null){
            Sales_Area__c salesarea2 = saMap.get(sa.Sales_Area__C);
            if(salesarea.Inside_Rep_ID__c !=null){
                sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                
                if(sa.Inside_Rep_Split_At_Close_ID__c !=null)
                    sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
            }
            
            if(salesarea.Field_Rep_ID__c !=null){
                sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  
            }
            
            if(salesarea.TSE_ID__c !=null){
                sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                
                if(sa.Field_Rep_Split_At_Close_ID__c !=null)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;                
            }
        }
        
        if(sa.Field_Rep_Overwrite__c != null)
            sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
        else
            sa.Field_Rep_Overwrite_text__c ='';
        if(sa.Inside_Rep_Overwrite__c != null)
            sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
        else
            sa.Inside_Rep_Overwrite_text__c ='';

        if(sa.TSE1_Overwrite__c != null)
            sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
        else
            sa.TSE1_Overwrite_text__c ='';
        if(sa.TSE2_Overwrite__c != null)
            sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
        else
            sa.TSE2_Overwrite_text__c ='';        
    }
}

Reduced one for loop, which will improve the performance.

In the similar way if we can re-factor the other code, for sure you will not get CPU error.
If you still gets into then we will think of @future / batch apex.

Please do let me know if it helps.

Regards,
Mahesh
This was selected as the best answer
John AthitakisJohn Athitakis

Mahesh! I don't know if you have the @future code handy? I attempted to do it myself but it seems it is doing nothing! Here is what I did.


Trigger Code

trigger OpportunityReferenceUpdate on Opportunity (after insert, after update) {

    OpportunityReferenceUpdateClass.processOpportunityReferences(Trigger.newMap.keySet());

}


@Future Code

public class OpportunityReferenceUpdateClass {
@future
    public static void processOpportunityReferences(Set<ID> ops){

        
    List<Opportunity> opl = [select id, Sales_Area__c, Secondary_Sales_Area__c, Inside_Rep_Lookup__c, Field_Rep_Lookup__c, TSE_Lookup__c, Inside_Rep_Split_Lookup__c, Field_Rep_Split_Lookup__c, TSE_Split_Lookup__c from Opportunity where id IN :ops]; 
        
    Set<id> Field_Rep = new Set<id>();
    Set<id> Inside_Rep = new Set<id>();
    Set<id> Sales_Associate = new Set<id>();
    Set<id> SDR = new Set<id>();
    Set<id> TSE1 = new Set<id>();
    Set<id> TSE2 = new Set<id>();
    
    Set<Id> saIdSet = new Set<id>();
    Set<Id> sa2IdSet = new Set<id>();
    for (Opportunity sa : opl){
        saIdSet.add(sa.Sales_Area__c);
        
        if(sa.Secondary_Sales_Area__c!=null)
        sa2IdSet.add(sa.Secondary_Sales_Area__c);
    }
            
    Map<Id, Sales_Area__c> saMap = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: saIdSet]);
    Map<Id, Sales_Area__c> sa2Map = new Map<Id, Sales_Area__C>([select ID, Field_Rep_ID__c, Inside_Rep_ID__c, TSE_ID__c from Sales_Area__c where id IN: sa2IdSet]);
        
        
    for (Opportunity sa : opl){
      sa.Inside_Rep_Lookup__c='005G0000006uYgi';
        if(sa.Sales_Area__c!=null){
        Sales_Area__c salesarea = saMap.get(sa.Sales_Area__C);
            sa.Inside_Rep_Lookup__c=salesarea.Inside_Rep_ID__c;
            sa.Inside_Rep_ID__c=salesarea.Inside_Rep_ID__c;
            
            if(sa.Inside_Rep_At_Close_ID__c !=null || sa.IsClosed==True)
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_At_Close_ID__c;
                
            if(sa.Inside_Rep_Overwrite__c!=null)   
                sa.Inside_Rep_Lookup__c=sa.Inside_Rep_Overwrite__c;

            sa.Field_Rep_Lookup__c=salesarea.Field_Rep_ID__c;
            sa.Field_Rep_ID__c=salesarea.Field_Rep_ID__c;
            
            if(sa.Field_Rep_At_Close_ID__c !=null || sa.IsClosed==True)
                sa.Field_Rep_Lookup__c=sa.Field_Rep_At_Close_ID__c;
                
            if(sa.Field_Rep_Overwrite__c!=null)   
                sa.Field_Rep_Lookup__c=sa.Field_Rep_Overwrite__c;


            sa.TSE_Lookup__c=salesarea.TSE_ID__c;
            sa.TSE_ID__c=salesarea.TSE_ID__c;
            
            if(sa.TSE_At_Close_ID__c !=null || sa.IsClosed==True)
            sa.TSE_Lookup__c=sa.TSE_At_Close_ID__c;
                
            if(sa.TSE1_Overwrite__c!=null)   
            sa.TSE_Lookup__c=sa.TSE1_Overwrite__c;  


        }   
  
        
        if(sa.Secondary_Sales_Area__c != null){
            Sales_Area__c salesarea2 = sa2Map.get(sa.Secondary_Sales_Area__C);
         
                sa.Inside_Rep_Split_Lookup__c=salesarea2.Inside_Rep_ID__c;
                sa.Inside_Rep_Split_Id__c=salesarea2.Inside_Rep_ID__c;
                
                if(sa.Inside_Rep_Split_At_Close_ID__c !=null || sa.IsClosed==True)
                    sa.Inside_Rep_Split_Lookup__c=sa.Inside_Rep_Split_At_Close_ID__c;
  
            
      
                sa.Field_Rep_Split_Lookup__c=salesarea2.Field_Rep_ID__c;
                sa.Field_Rep_Split_ID__c=salesarea2.Field_Rep_ID__c;
                if(sa.Field_Rep_Split_At_Close_ID__c !=null ||sa.IsClosed==True)
                    sa.Field_Rep_Split_Lookup__c=sa.Field_Rep_Split_At_Close_ID__c;  

                sa.TSE_Split_Lookup__c=salesarea2.TSE_ID__c;
                sa.TSE_Split_ID__c=salesarea2.TSE_ID__c;
                if(sa.TSE_Split_At_Close_ID__c !=null || sa.IsClosed==True)
                    sa.TSE_Split_Lookup__c=sa.TSE_Split_At_Close_ID__c;                

  
            
        }
        
        
        if(sa.Field_Rep_Overwrite__c != null)
            Field_Rep.add(sa.Field_Rep_Overwrite__c);   
        if(sa.Inside_Rep_Overwrite__c != null)
            Inside_Rep.add(sa.Inside_Rep_Overwrite__c);
        if(sa.TSE1_Overwrite__c != null)
            TSE1.add(sa.TSE1_Overwrite__c);
        if(sa.TSE2_Overwrite__c != null)
            TSE2.add(sa.TSE2_Overwrite__c);
    }

    Map<id, User> Field_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Field_Rep]);  
    Map<id, User> Inside_Rep_Names = new Map<id, User>([Select Name from User Where Id in :Inside_Rep]); 

    Map<id, User> TSE1_Names = new Map<id, User>([Select Name from User Where Id in :TSE1]); 
    Map<id, User> TSE2_Names = new Map<id, User>([Select Name from User Where Id in :TSE2]); 

    for (Opportunity sa : opl){
        if(sa.Field_Rep_Overwrite__c != null)
            sa.Field_Rep_Overwrite_text__c = Field_Rep_Names.get(sa.Field_Rep_Overwrite__c).Name;   
        else
            sa.Field_Rep_Overwrite_text__c ='';
        if(sa.Inside_Rep_Overwrite__c != null)
            sa.Inside_Rep_Overwrite_text__c = Inside_Rep_Names.get(sa.Inside_Rep_Overwrite__c).Name;   
        else
            sa.Inside_Rep_Overwrite_text__c ='';

        if(sa.TSE1_Overwrite__c != null)
            sa.TSE1_Overwrite_text__c = TSE1_Names.get(sa.TSE1_Overwrite__c).Name;   
        else
            sa.TSE1_Overwrite_text__c ='';
        if(sa.TSE2_Overwrite__c != null)
            sa.TSE2_Overwrite_text__c = TSE2_Names.get(sa.TSE2_Overwrite__c).Name;   
        else
            sa.TSE2_Overwrite_text__c ='';
    }
    }
}
However, it does nothing. I even hard coded a change early on (sa.Inside_Rep_Lookup__c='005G0000006uYgi';) and it is not doing it. I see from my apex jobs that the @future is firing and completing without any errors. 
vaani krvaani kr
Hi   John Athitakis, 
 Here you asked one custom object and another one is Opportunity(5), it means it is parent child relationship here. parent is custom object and  child is Opportunity object.  whenever update parent field (phone num or any field we have to update in child(opportunity) which have stage name not eqaul to(closed won and closed lost) 
Here The Trigger code is

public class customobjupdTriggerHelper {
    public void updateopportunityphn(set<id> salessids, Map<id,Sales_area__c> NewsalesMAp) 
    {
        list <Opportunity> lstopp = [select id,name,phone__c,Sales_area_name__c,StageName from opportunity  where Sales_area_name__c in:salessids];
        for(Opportunity opp : lstopp)
        { 
            //system.debug('----------------'+ opp.StageName);
            if( NewsalesMAp.containskey(opp.Sales_area_name__c) && opp.StageName != 'Closed Won' && opp.StageName != 'Closed Lost' )
            {
                
                //system.debug('=========='+NewsalesMAp.get(opp.Sales_area_name__c).phone__c);
                opp.phone__c = NewsalesMAp.get(opp.Sales_area_name__c).phone__c;
                opp.StageName = NewsalesMAp.get(opp.Sales_area_name__c).StageName__c;
            }
        }
        update lstopp;
        
    }


Trigger code