0

I wrote a trigger to update a field (boolean value) in LEAD when a TASK is added to the lead. Trigger works fine in DEV box but when I deploy it, its giving me Exception error clashing with one other trigger in production. I have added both the codes here. Please help me resolve this.

The trigger is below -

trigger triggerOnLead on Task ( after insert, after update) { 
    List<Id> leadIds=new List<Id>();
    for(Task t : Trigger.new){    {
       leadIds.add(t.whoid);
       //system.debug('test' +leadIds);
    }
     List<Lead> leadList = [Select id, status, isActivity__c from Lead where id    =:leadIds and status='Open - Not Contacted' and isActivity__c=false ];
       for(Lead l:leadList ){
            l.isActivity__c = true;
       }       
       try{
            update leadList;
          } catch(DMLException e){
            system.debug('Lead Activity is not updated properly!.  Error: '+e);
            }
}

And I'm getting the below ERROR when deploying: System.LimitException: Too many SOQL queries: 101 Stack Trace: Trigger.sampleTrigger: line 10, column 1

As it's clashing with trigger -

trigger sampleTrigger on Event(after insert) {
  string sWhtId = trigger.new[0].WhatId;
  string sWhoId = trigger.new[0].WhoId;
  Event objT = [Select id,WhatId,WhoId from Event where id =: trigger.new[0].id ];
  if(sWhtId != '') {
     List<Account>lstAcc = [Select id,LastModifiedById,ownerid from Account where id =: sWhtId ];
    if(lstAcc != null && lstAcc.size()>0){
       if (trigger.new[0].LastModifiedById != null) {
           if(lstAcc[0].ownerid != trigger.new[0].LastModifiedById) {
                trigger.new[0].IsOwnerSame__c = true;
                }
            }
       else  {
           if(lstAcc[0].ownerid != trigger.new[0].CreatedById) {
               objT.IsOwnerSame__c = true;
             }
         }
      }
  }
  if(sWhoId != ''){
    //For Contact
     List<Contact>lstCon = [Select id,LastModifiedById,ownerid from Contact where id =: sWhoId ];
    if(lstCon != null && lstCon.size()>0) {
         if (trigger.new[0].LastModifiedById != null)  {
               if(lstCon[0].ownerid != trigger.new[0].LastModifiedById)  {
                    trigger.new[0].IsOwnerSame__c = true;
                 }
            }
         else  {
               if(lstCon[0].ownerid != trigger.new[0].CreatedById) {
               objT.IsOwnerSame__c = true;
               }
           }
       }
    }
}
2
  • 2
    You are updating the same object type that the trigger is on thus causing recursion. The second trigger is not bulkified and will not cause the issue is not doing anything at all since there are no DML and there is no way a properly written test method would pass. The fact that it errors on the second trigger is just the fact that it happened to be the spot the 101st query was executed, not the cause. Read up on recursion. Additionally, the second trigger is not being executed as a result of your first since it is on event. There is more going on here
    – Eric
    Feb 5, 2017 at 4:57
  • 1
    I would suggesting looking into using a "One trigger per object" design pattern, more info here: embracingthecloud.com/2010/07/08/… Also, you'll want to look into proper ways to bulkify your code, like @Eric mentioned. While you're at it, look into "SOQL for loops" as well. Those resources will help you consolidate your code and avoid the issues that you're seeing here as well as those that could crop up. Feb 5, 2017 at 5:57

1 Answer 1

1

I think you have an extra left curly on line 3 of the trigger... for(Task t : Trigger.new){ { This is having the effect of wrapping up your SOQL inside the for loop. I'll bet that's why you are getting a SOQL limit exception. Looks like just a simple typo...

refactored trigger...

trigger triggerOnLead on Task ( after insert, after update) { 
    List<Id> leadIds=new List<Id>();
    for(Task t : Trigger.new){    
       leadIds.add(t.whoid);
       //system.debug('test' +leadIds);
    }
    List<Lead> leadList = [Select id, status, isActivity__c from Lead where id =:leadIds and status='Open - Not Contacted' and isActivity__c=false ];
    for(Lead l:leadList ){
        l.isActivity__c = true;
        try{
            update leadList;
        } catch(DMLException e) {
            system.debug('Lead Activity is not updated properly!.  Error: '+e);
        }
    }
}

Load up a list of Ids from the Trigger using a loop... Run a single query against those Ids... Loop through that list!

Bob's your uncle!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .