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
AwkwardAAwkwardA 

System.LimitException: Too many SOQL queries: 101

I'm getting the "Too many SOQL queries" error on Line 17. Help me troubleshoot?

trigger InsideSalesAppointment on Task (after insert, after update) {

Map<Id,Date> LeadsToUpdate = new Map<Id,Date>();
List<User> InsideSalesUser = [SELECT Id FROM User where UserRoleId='00E30000001rctV'];
Set<Id> InsideSalesUserId = new set<Id>();
for (User u :InsideSalesUser) {
    InsideSalesUserId.add(u.Id);
    }


for (Task t: Trigger.new) {
    if(InsideSalesUserId.contains(t.OwnerID) && t.IsClosed == false && String.valueOf(t.WhoId).startsWith('00Q') && t.WhoId != NULL)
    
    LeadsToUpdate.put(t.WhoId, t.ActivityDate);
    }

List<Lead> LeadsAppointment = [SELECT Id FROM Lead where Id IN:LeadsToUpdate.keyset()];

if (LeadsToUpdate.keyset().size()>0){

for (Lead l: LeadsAppointment) {

    l.Next_Appointment_Date__c = LeadsToUpdate.get(l.id);
    }
    update LeadsAppointment;
}
}


Best Answer chosen by AwkwardA
MaxPowerForceMaxPowerForce
The leadAppList update is in the conditional block, not the loop.  See if this variant of the test works.  I bulkified the test lead insert and added test.startTest/stoptest methods.
@isTest
private class TestISRHandoffandIncrementDials {
   

 static testmethod void testTypeMapping() {

List<Task> testTasks = new List<Task>();

//Creates a Lead for Testing
   Lead l = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu', ISR_Handoff_Counter__c = NULL, Dials__c = 0);   
   insert l;

//Creates a Lead for Testing
   Lead l2 = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu',Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);   
   insert l2;

//Creates Task For Testing
   Task t = new Task (WhoId = l.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu' , Call_Result__c = 'Contact');
   testTasks.add(t);

//Creates Task For Testing
   Task t2 = new Task (WhoId = l2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks.add(t2);

//Creates an Account For Testing
   Account a = new Account(name = 'Test Account', BillingState = 'NY', BillingCountry = 'US', ownerid = '00530000003uFyu', Do_Not_Call__c = true);   
   insert a;
   
//Creates Opportunity For Testing
   Opportunity o = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), ISR_Handoff_Counter__c = NULL);
   insert o;
   
//Creates Opportunity For Testing
   Opportunity o2 = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);
   insert o2;
   
//Creates Task For Testing
   Task t3 = new Task (WhatId = o.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks(t3);  

//Creates Task For Testing
   Task t4 = new Task (WhatId = o2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks.(t4);

   //Start the testing
   Test.startTest();

   // Insert the list of tasks
   insert testTasks;
  

l =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c, Dials__c FROM Lead WHERE Id = :l.Id];
    system.assert (l.OwnerId == '00530000009sb4g');
    system.assert (l.SD_Pipeline__c == true);
    system.assert (l.ISR_Handoff_Counter__c == 1);
    system.assert (l.Dials__c == 1);
    
l2 =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000009rgSK');
    system.assert (l2.SD_Pipeline__c == true);
    system.assert (l2.ISR_Handoff_Counter__c == 1);
    
o =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o.Id];
    system.assert (o.OwnerId == '00530000009sb4g');
    system.assert (o.ISR_Handoff_Counter__c == 1);
    
o2 =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000009rgSK');
    system.assert (o2.ISR_Handoff_Counter__c == 1);
    
//Creates Task For Testing
   Task t5 = new Task (WhatId = o2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t5;  

//Creates Task For Testing
   Task t6 = new Task (WhoId = l2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t6;
   
o2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000003uFyu');
    system.assert (o2.SD_Handoff_Counter__c == 1); 

l2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000003uFyu');
    system.assert (l2.SD_Handoff_Counter__c == 1);

//End of testing
test.stopTest();   

   }}

All Answers

MaxPowerForceMaxPowerForce
There are no obvious problems with this trigger.   Can you post a full debug log?  Something else must be going on.
AwkwardAAwkwardA
Here is the debug code. It looks like there are a lot of other triggers on tasks -- could this be the issue?

21:32:28.942 (18942701051)|CODE_UNIT_STARTED|[EXTERNAL]|01q30000000a7y5|InsideSalesAppointment on Task trigger event AfterInsert for [00TV0000007BlXw]
21:32:28.942 (18942727816)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
21:32:28.942 (18942765793)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
21:32:28.942 (18942783817)|VARIABLE_SCOPE_BEGIN|[1]|this|InsideSalesAppointment|true|false
21:32:28.942 (18942871019)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x65e7b3a4
21:32:28.942 (18942896750)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:20
21:32:28.942 (18942913713)|VARIABLE_SCOPE_BEGIN|[1]|this|InsideSalesAppointment|true|false
21:32:28.942 (18942972828)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x65e7b3a4
21:32:28.942 (18942985021)|STATEMENT_EXECUTE|[1]
21:32:28.942 (18942989662)|STATEMENT_EXECUTE|[3]
21:32:28.943 (18943003909)|HEAP_ALLOCATE|[3]|Bytes:4
21:32:28.943 (18943074567)|VARIABLE_ASSIGNMENT|[3]|this.LeadsToUpdate|{"serId":1,"value":{}}|0x65e7b3a4
21:32:28.943 (18943084037)|STATEMENT_EXECUTE|[4]
21:32:28.943 (18943096754)|HEAP_ALLOCATE|[4]|Bytes:4
21:32:28.946 (18946859495)|HEAP_ALLOCATE|[4]|Bytes:12
21:32:28.946 (18946883301)|HEAP_ALLOCATE|[4]|Bytes:66
21:32:28.946 (18946898292)|HEAP_ALLOCATE|[4]|Bytes:12
21:32:28.946 (18946985496)|VARIABLE_ASSIGNMENT|[4]|this.InsideSalesUser|{"serId":1,"value":[{"serId":2,"value":{"Id":"00530000009rgSKAAY"}},{"serId":3,"value":{"Id":"00530000009sb50AAA"}}]}|0x65e7b3a4
21:32:28.946 (18946997539)|STATEMENT_EXECUTE|[5]
21:32:28.947 (18947015482)|HEAP_ALLOCATE|[5]|Bytes:4
21:32:28.947 (18947062603)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>(Integer)
21:32:28.947 (18947081843)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>(Integer)
21:32:28.947 (18947146632)|VARIABLE_ASSIGNMENT|[5]|this.InsideSalesUserId|{"serId":1,"value":[]}|0x65e7b3a4
21:32:28.947 (18947201367)|HEAP_ALLOCATE|[6]|Bytes:5
21:32:28.947 (18947237206)|HEAP_ALLOCATE|[6]|Bytes:8
21:32:28.947 (18947261442)|VARIABLE_SCOPE_BEGIN|[6]|u|User|true|false
21:32:28.947 (18947307151)|VARIABLE_ASSIGNMENT|[6]|u|{"serId":1,"value":{"Id":"00530000009rgSKAAY"}}|0x1d21fa84
21:32:28.947 (18947318539)|STATEMENT_EXECUTE|[6]
21:32:28.947 (18947322953)|STATEMENT_EXECUTE|[7]
21:32:28.947 (18947369806)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
21:32:28.947 (18947399040)|HEAP_ALLOCATE|[6]|Bytes:5
21:32:28.947 (18947430958)|HEAP_ALLOCATE|[6]|Bytes:8
21:32:28.947 (18947452330)|VARIABLE_SCOPE_BEGIN|[6]|u|User|true|false
21:32:28.947 (18947490047)|VARIABLE_ASSIGNMENT|[6]|u|{"serId":1,"value":{"Id":"00530000009sb50AAA"}}|0x21c5ff0d
21:32:28.947 (18947499378)|STATEMENT_EXECUTE|[6]
21:32:28.947 (18947503488)|STATEMENT_EXECUTE|[7]
21:32:28.947 (18947539993)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
21:32:28.947 (18947567608)|HEAP_ALLOCATE|[6]|Bytes:5
21:32:28.947 (18947608508)|VARIABLE_ASSIGNMENT|[6]|u|null|
21:32:28.947 (18947652144)|HEAP_ALLOCATE|[11]|Bytes:5
21:32:28.947 (18947685252)|VARIABLE_SCOPE_BEGIN|[11]|t|Task|true|false
21:32:28.948 (18948167065)|VARIABLE_ASSIGNMENT|[11]|t|{"RecordTypeId":"01230000001dnNMAAY","Event_Completed__c":false,"CurrencyIsoCode":"USD","LastModifiedById":"00530000009862MAAQ","Subject":"Handoff to SD","OwnerId":"00530000009rgSKAAY","LastModifiedDate":"2014-05-16T04:32:22.000Z","ActivityDate":"2014-05-15T00:00:00.000Z","IsRecurrence":false,"Priority":"Normal","IsArchived":false,"Status":"Completed","IsClosed":true,"IsVisibleInSelfServi (2 more) ...":false,"qbdialer__SMSReminde (4 more) ...":false,"DB_Activity_Type__c":"Other","IsReminderSet":false,"SystemModstamp":"2014-05-16T04:32:22.000Z","WhoId":"00QV0000004Dyq4MAC","CreatedById":"00530000009862MAAQ","CreatedDate":"2014-05-16T04:32:22.000Z","IsDeleted":false,"Id":"00TV0000007BlXwMAK","IS_Response__c":false}|0x7cc90f17
21:32:28.948 (18948186747)|STATEMENT_EXECUTE|[11]
21:32:28.948 (18948242577)|STATEMENT_EXECUTE|[12]
21:32:28.948 (18948266920)|HEAP_ALLOCATE|[11]|Bytes:5
21:32:28.948 (18948314092)|VARIABLE_ASSIGNMENT|[11]|t|null|
21:32:28.948 (18948323185)|STATEMENT_EXECUTE|[17]
21:32:28.948 (18948335918)|HEAP_ALLOCATE|[17]|Bytes:4
21:32:28.948 (18948382915)|HEAP_ALLOCATE|[17]|Bytes:4
21:32:28.948 (18948982172)|EXCEPTION_THROWN|[17]|System.LimitException: Too many SOQL queries: 101
21:32:28.949 (18949229620)|HEAP_ALLOCATE|[17]|Bytes:30
21:32:28.949 (18949411267)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.InsideSalesAppointment: line 17, column 1
21:32:28.949 (18949432559)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.InsideSalesAppointment: line 17, column 1
21:32:28.950 (18950366548)|CODE_UNIT_FINISHED|InsideSalesAppointment on Task trigger event AfterInsert for [00TV0000007BlXw]
21:32:28.958 (18958712529)|HEAP_ALLOCATE|[70]|Bytes:30
21:32:28.958 (18958761783)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.InsideSalesAppointment: line 17, column 1
21:32:28.958 (18958778613)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.InsideSalesAppointment: line 17, column 1
21:32:28.958 (18958792264)|CODE_UNIT_FINISHED|TestISRHandoffandIncrementDials.testTypeMapping
21:32:28.958 (18958799728)|EXECUTION_FINISHED

MaxPowerForceMaxPowerForce
Whatever the problem is, it is not in this section of debug log.  I suspect there are other triggers contributing to this issue and this query is just putting it over the top.  Also, is this happening all the time or just when you run a unit test?   The 'TestISRHandoffandIncrementDials.testTypeMapping' sounds like a test method.  If it is occuring during the test, can you post the test code.  If you are executing DML in a loop in the test method, you might see an error like this.
AwkwardAAwkwardA
Yeah, it's only when I run a unit test. I'm pretty certain you are correct in that other triggers are contributing ... this org has a lot of triggers on the tasks object.

Here's the test class:

@isTest
private class TestISRHandoffandIncrementDials {
   

 static testmethod void testTypeMapping() {


//Creates a Lead for Testing
   Lead l = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu', ISR_Handoff_Counter__c = NULL, Dials__c = 0);   
   insert l;

//Creates a Lead for Testing
   Lead l2 = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu',Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);   
   insert l2;

//Creates Task For Testing
   Task t = new Task (WhoId = l.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu' , Call_Result__c = 'Contact');
   insert t;

//Creates Task For Testing
   Task t2 = new Task (WhoId = l2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   insert t2;

//Creates an Account For Testing
   Account a = new Account(name = 'Test Account', BillingState = 'NY', BillingCountry = 'US', ownerid = '00530000003uFyu', Do_Not_Call__c = true);   
   insert a;
   
//Creates Opportunity For Testing
   Opportunity o = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), ISR_Handoff_Counter__c = NULL);
   insert o;
   
//Creates Opportunity For Testing
   Opportunity o2 = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);
   insert o2;
   
//Creates Task For Testing
   Task t3 = new Task (WhatId = o.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   insert t3;  

//Creates Task For Testing
   Task t4 = new Task (WhatId = o2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   insert t4;   
  

l =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c, Dials__c FROM Lead WHERE Id = :l.Id];
    system.assert (l.OwnerId == '00530000009sb4g');
    system.assert (l.SD_Pipeline__c == true);
    system.assert (l.ISR_Handoff_Counter__c == 1);
    system.assert (l.Dials__c == 1);
    
l2 =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000009rgSK');
    system.assert (l2.SD_Pipeline__c == true);
    system.assert (l2.ISR_Handoff_Counter__c == 1);
    
o =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o.Id];
    system.assert (o.OwnerId == '00530000009sb4g');
    system.assert (o.ISR_Handoff_Counter__c == 1);
    
o2 =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000009rgSK');
    system.assert (o2.ISR_Handoff_Counter__c == 1);
    
//Creates Task For Testing
   Task t5 = new Task (WhatId = o2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t5;  

//Creates Task For Testing
   Task t6 = new Task (WhoId = l2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t6;
   
o2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000003uFyu');
    system.assert (o2.SD_Handoff_Counter__c == 1); 

l2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000003uFyu');
    system.assert (l2.SD_Handoff_Counter__c == 1);
    
   

   }}

MaxPowerForceMaxPowerForce
The leadAppList update is in the conditional block, not the loop.  See if this variant of the test works.  I bulkified the test lead insert and added test.startTest/stoptest methods.
@isTest
private class TestISRHandoffandIncrementDials {
   

 static testmethod void testTypeMapping() {

List<Task> testTasks = new List<Task>();

//Creates a Lead for Testing
   Lead l = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu', ISR_Handoff_Counter__c = NULL, Dials__c = 0);   
   insert l;

//Creates a Lead for Testing
   Lead l2 = new Lead(LastName = 'Test', Company = 'Test Company', Status = 'Open', LeadSource = 'Web', OwnerID = '00530000003uFyu',Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);   
   insert l2;

//Creates Task For Testing
   Task t = new Task (WhoId = l.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu' , Call_Result__c = 'Contact');
   testTasks.add(t);

//Creates Task For Testing
   Task t2 = new Task (WhoId = l2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks.add(t2);

//Creates an Account For Testing
   Account a = new Account(name = 'Test Account', BillingState = 'NY', BillingCountry = 'US', ownerid = '00530000003uFyu', Do_Not_Call__c = true);   
   insert a;
   
//Creates Opportunity For Testing
   Opportunity o = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), ISR_Handoff_Counter__c = NULL);
   insert o;
   
//Creates Opportunity For Testing
   Opportunity o2 = new Opportunity (Name = 'Test', AccountId = a.id, ownerid = a.ownerid, LeadSource = 'Unknown', StageName = 'Prospect', Amount = 500.00, CloseDate =  System.today(), Last_ISR_ID__c = '00530000009rgSK', Last_SD_ID__c = '00530000003uFyu', ISR_Handoff_Counter__c = 0, SD_Handoff_Counter__c = 0);
   insert o2;
   
//Creates Task For Testing
   Task t3 = new Task (WhatId = o.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks(t3);  

//Creates Task For Testing
   Task t4 = new Task (WhatId = o2.id, Subject = 'Handoff to ISR', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000003uFyu');
   testTasks.(t4);

   //Start the testing
   Test.startTest();

   // Insert the list of tasks
   insert testTasks;
  

l =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c, Dials__c FROM Lead WHERE Id = :l.Id];
    system.assert (l.OwnerId == '00530000009sb4g');
    system.assert (l.SD_Pipeline__c == true);
    system.assert (l.ISR_Handoff_Counter__c == 1);
    system.assert (l.Dials__c == 1);
    
l2 =[SELECT OwnerID, SD_Pipeline__c, ISR_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000009rgSK');
    system.assert (l2.SD_Pipeline__c == true);
    system.assert (l2.ISR_Handoff_Counter__c == 1);
    
o =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o.Id];
    system.assert (o.OwnerId == '00530000009sb4g');
    system.assert (o.ISR_Handoff_Counter__c == 1);
    
o2 =[SELECT OwnerID, ISR_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000009rgSK');
    system.assert (o2.ISR_Handoff_Counter__c == 1);
    
//Creates Task For Testing
   Task t5 = new Task (WhatId = o2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t5;  

//Creates Task For Testing
   Task t6 = new Task (WhoId = l2.id, Subject = 'Handoff to SD', ActivityDate = System.today(), Status = 'Completed', OwnerID = '00530000009rgSK');
   insert t6;
   
o2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Opportunity WHERE Id = :o2.Id];
    system.assert (o2.OwnerId == '00530000003uFyu');
    system.assert (o2.SD_Handoff_Counter__c == 1); 

l2 =[SELECT OwnerID, SD_Handoff_Counter__c FROM Lead WHERE Id = :l2.Id];
    system.assert (l2.OwnerId == '00530000003uFyu');
    system.assert (l2.SD_Handoff_Counter__c == 1);

//End of testing
test.stopTest();   

   }}

This was selected as the best answer
OnpursuitOnpursuit
Hi,

The above solution looks good. Also, do check your debug in details as there can be a scenario that triggers are getting executed again and again. If above solution does not work for you then you need to add a helper class with a static boolean variable so that trigger fire only once .

Thanks
AwkwardAAwkwardA
Awesome, thank you so much.