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
akallioWileyakallioWiley 

Admin looking for help. Why is this trigger firing?

Hello. I wrote this trigger and it has been in  my production environment  for almost 1 year without any problems until yesterday. One of my routine processes is to reassign opportunities when rep Account assignments are changed...I use an ex-Anon script to make these kinds of updates and all of a sudden I am getting an error message that points me to my trigger when execute the script. The things is I can't see how my script would causing the trigger to fire. The script simply updates the OwnerId and TerritoryId fields of the Opportunity object. Although, the trigger is an after update the trigger there is criteria that checks for Stage changes before passing parameters to a class.  My script makes no updates to the stage field. So, I'm hoping some extra eyes could tell me why the trigger is being executed.

So, here is the trigger:

trigger opptyTrigger_EvalSyncController on Opportunity (after update) {
   
   
    if(trigger.isUpdate) {   
       
        Map<Id,Opportunity> courseToOppty = new Map<Id,Opportunity>();
       
        for(Opportunity oppty : trigger.new) {             
                       
            if((oppty.StageName == 'Active' || oppty.StageName == 'Short Stack' || oppty.StageName == 'Probable' ||
                oppty.StageName == 'Won' || oppty.StageName == 'Won Format TBD')
               &&
               (trigger.oldMap.get(oppty.Id).StageName != 'Active' || trigger.oldMap.get(oppty.Id).StageName != 'Short Stack' ||
                trigger.oldMap.get(oppty.Id).StageName != 'Probable' || trigger.oldMap.get(oppty.Id).StageName != 'Won' ||
                trigger.oldMap.get(oppty.Id).StageName != 'Won Format TBD') ) {
               
                    courseToOppty.put(oppty.Course__c,oppty);   
            }
        }
       
         if(!courseToOppty.isEmpty()) {
            coreSynchronizer.sendEvalsToCore(courseToOppty);
        }
    }
}
======================================================================================================================

And here is the script with the error message that follows:

List<Opportunity> upOppties = new List<Opportunity>();
for(Opportunity oppty : [select Id, Name, OwnerId, Owner.Name, TerritoryId, Territory.Name from Opportunity where Owner.Name = 'Andy Kallio' Limit 100]) {
system.debug('Results: '+oppty.Id+' '+oppty.Name+' '+oppty.Territory.name+' '+oppty.Owner.Name);
oppty.OwnerId = '005G0000004NdWEIA0';

oppty.territoryId = '04TA00000004GGfMAM';

upOppties.add(oppty);

}

update upOppties;

==============================================================================================================

Apex script unhandled trigger exception by user/organization: 005G0000002DZlD/00DA0000000YFEv

opptyTrigger_EvalSyncController: execution of AfterUpdate

caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

Class.coreSynchronizer.sendEvalsToCore: line 13, column 1
Trigger.opptyTrigger_EvalSyncController: line 42, column 1
akallioWileyakallioWiley
I think I figured it out with the help of DeMorgan's Law. This is where only having a bachelor's in English lit. really sucks!  

Had to make the following change. I need to do some more UAT, but I think this works. 


if((oppty.StageName == 'Active' || oppty.StageName == 'Short Stack' || oppty.StageName == 'Probable' ||
                oppty.StageName == 'Won' || oppty.StageName == 'Won Format TBD')
               &&
               !(trigger.oldMap.get(oppty.Id).StageName == 'Active' || trigger.oldMap.get(oppty.Id).StageName == 'Short Stack' ||
                trigger.oldMap.get(oppty.Id).StageName == 'Probable' || trigger.oldMap.get(oppty.Id).StageName == 'Won' ||
                trigger.oldMap.get(oppty.Id).StageName == 'Won Format TBD') )