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
KasiWKasiW 

Help with Test Class Error - System.LimitException: Too many SOQL queries: 101

Hello,

I have created a test class and trigger (see below).  The trigger works as expected but when I run the test I get the error:


Error MessageSystem.LimitException: Too many SOQL queries: 101
Stack TraceTrigger.d_Contract_Termination: line 30, column 1

 Woudl yo be able to help?   Thank you Kasia 

Trigger

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* {Purpose}
*
* @author   
* @version  
*/   
  
/**
* CHANGE HISTORY
* =============================================================================
* Date          Name                    Description
    Fixed bulk processing
* =============================================================================
*/
trigger d_Contract_Termination on Opportunity (before insert, after update)
    {   String firstname = UserInfo.getFirstName();
        String profile = UserInfo.getProfileId();
        boolean result = false;
        if (firstname != null && firstname.startsWith('-') == true && profile != null && profile == '00e30000001ion1AAA')
            {
               result = firstname.contains('d');               
            }
    if (result == false)
        {
            if (Trigger.isUpdate)
               
                {
                    //Prepare the booking map outside of the for loop to support bulk processing.
                    Map<String, List<GCRM_Booking_Period__c>> bookingMap = new Map<String, List<GCRM_Booking_Period__c>>();
                    for (GCRM_Booking_Period__c booking : [SELECT Id, Opportunity__c, Gcrm_Booking_Period__c, Booking_Value__c, Gcrm_Active_Booking__c 
                                    FROM GCRM_Booking_Period__c
                                   WHERE Opportunity__c = :Trigger.NewMap.keySet() AND Gcrm_Booking_Period__c > :date.today()]) {
                        List<GCRM_Booking_Period__c> innerList = bookingMap.get(booking.Opportunity__c);
                        if (innerList == null) {
                            innerList = new List<GCRM_Booking_Period__c>();
                            bookingMap.put(booking.Opportunity__c, innerList);
                        }                                       
                        innerList.add(booking);
                    }
                    

                    Map<Id,Opportunity> newOpportunityMap = Trigger.newMap;
                    Map<Id,Opportunity> oldOpportunityMap = Trigger.oldMap;
                    List<GCRM_Booking_Period__c> bookingsToUpdate = new List<GCRM_Booking_Period__c>();
                    
                    for(Id OpportunityId:newOpportunityMap.keySet())
                        {
                            Opportunity NewOpp = newOpportunityMap.get(opportunityId);
                            Opportunity OldOpp = oldOpportunityMap.get(opportunityId);        
                        
                            Date myDate = date.today();

// If a contract is not active yet, bookings will be recalculated if needed. So a user should not use the Actual Contract End Date but the Contract End Date.
                            
                            if (NewOpp.Actual_Contract_End_Date__c != null && (NewOpp.StageName != 'Closed Won' || NewOpp.Contract_Start_Date__c > myDate))
                                {
                                    NewOpp.addError('Actual Contract End Date can only be entered when Phase Line is Closed Won and the Contract is active.');
                                }

// When the contract is active and the Actual Contract End Date is set (for the first time), select all bookings with booking date past the Actual Contract End Date and render them inactive.
                            
                            else if (NewOpp.Actual_Contract_End_Date__c != null && OldOpp.Actual_Contract_End_Date__c == null)
                                {                                   
                                    List<GCRM_Booking_Period__c> bmap = bookingMap.get(opportunityId);
                                    if (bmap != null) {
                                        for (GCRM_Booking_Period__c booking : bmap) {
                                            if (booking.Gcrm_Booking_Period__c >= NewOpp.Actual_Contract_End_Date__c) {
                                                booking.Gcrm_Active_Booking__c = FALSE;
                                                bookingsToUpdate.add(booking);
                                            }
                                        }
                                    }
                                }                                                           
                            
// Once the Actual Contract End Date has been set, it can not be changed anymore.                            
                            
                            else if (NewOpp.Actual_Contract_End_Date__c <> OldOpp.Actual_Contract_End_Date__c && OldOpp.Actual_Contract_End_Date__c != null)
                                {
                                    NewOpp.addError('Actual Contract End Date cannot be changed.');
                                }
                        }
                        update bookingsToUpdate;  //keep this update outside of forloop to support bulk processing.
                }
            else

// In case an opportunity is created, it is not allowd to enter an Actual Contract End Date. Users should use the Contract End Date.

                {
                    for (Opportunity opp : Trigger.new)
                        {
                            if (opp.Actual_Contract_End_Date__c != null)
                                {
                                    opp.addError('Actual Contract End Date cannot be entered. Use the Contract End Date when creating an opportunity.');
                                }
                        }
                }
        }    
}

Test Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@isTest

private class TestTrigger3
//Test trigger in order to test Opportunity Trigger Actual_Close_Date
{

static testMethod void testOpportunityTrigger()
    {
       
        Account acct = new Account(name='Test Account', billingcountry = 'United States', billingstate ='CO');
        insert acct;
        Datetime myDate = Datetime.now();
        String dateOutput = myDate.format('yyyy-MM-dd');
    
        Opportunity opp = new Opportunity(AccountID=acct.Id,amount=300000, notice_period__c=4, name='test', CurrencyIsoCode='EUR', 
                   // CloseDate= Date.valueOf('2011-09-14'),StageName='Qualified',Contract_Start_Date__c= Date.valueOf('2011-10-01'),
                   // CloseDate= Date.valueOf(date.today().format('yyyy-MM-dd')),StageName='Qualified',Contract_Start_Date__c= Date.valueOf(date.today().format('yyyy-MM-dd')), 
                   CloseDate= Date.valueOf(dateOutput),StageName='Qualified',Contract_Start_Date__c= Date.valueOf(dateOutput), 
                    
                    Contract_Duration__c= 12,Billing_Type__c='SLA (includes all types)', Insurance_Coverage__c = 'Yes', Unusual_Insurance_Requirements__c = 'Yes', 
                    Warranty_Clause__c = 'Yes', Unusual_Damages__c = 'Yes', Risk_Do_Dont__c = 'Yes', Unusual_Risk__c = 'Yes', 
                    External_Counsel__c = 'Yes', Payment_Terms__c = 'Yes', Contract_Type__c='Staffing',Number_of_Positions__c=5);
        insert opp;
        
        opp.StageName='Closed Won';
        try
        {update opp;}
        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('is'));
        }
        
        opp.StageName='Qualified';
        try
        {update opp;}
        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('is'));
        }

        opp.StageName='Closed Won';    
        opp.name='test2';
        try
        {update opp;}
        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('is'));
        }
        
    
        opp.StageName='Closed Won';
        opp.name='test3';
        try
        {update opp;}

        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('Insertion failed'), 'message=' + e.getMessage());
             System.assert(e.getMessage().contains('phase'));
        }
    
        opp.Actual_Close_Date__c=Date.valueOf('2011-09-19');
        try
        {update opp;}

        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('phase'));
        }

        Opportunity opp2 = new Opportunity(AccountID=acct.Id, name='test', CurrencyIsoCode='EUR', CloseDate= Date.valueOf('2011-09-14'),
        StageName='Qualified',Contract_Start_Date__c= Date.valueOf('2011-10-01'), Contract_Duration__c= 12, 
        Insurance_Coverage__c = 'Yes', Unusual_Insurance_Requirements__c = 'Yes', Warranty_Clause__c = 'Yes', Unusual_Damages__c = 'Yes', 
        Risk_Do_Dont__c = 'Yes', Unusual_Risk__c = 'Yes', Amount = 30, External_Counsel__c = 'Yes', Billing_Type__c = 'SLA (includes all types)', Payment_Terms__c = 'Yes', Contract_Type__c='Staffing',Number_of_Positions__c=5);
        insert opp2;
        
        opp2.StageName='Developed';
        try
        {update opp2;}
        
        catch(System.DMLException e)
        {
             System.assert(e.getMessage().contains('is'));
        }
    }
}