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
Leonard Silon 8Leonard Silon 8 

Why is my test class timing out?

I am helping my developer by posting this. Here is teh trigger code, followed by the test class code followed by the picture of the failure notice when trying to validate in production. The trigger works in the sandbox. Failure happens when validating the change set. What is the issue?
TRIGGER:
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
trigger updateContactOwnerWithTerritory2 on Territory__c (after update) {
    List<User> users = [select Id, Name from User]; 
    List<Id> contactIds = new List<Id>();
    Map<Id, Id> extWholesailers = new Map<Id, Id>();
    
    for (Territory__c territory: trigger.new) {
        
        if (trigger.oldMap.get(territory.Id).Ext_Wholesaler__c != territory.Ext_Wholesaler__c) {
          Boolean userFound = false;
            User user;
            for (Integer j = 0; (j < users.size() && !userFound); j++) {
                
                System.debug('User name ' + users[j].Name.trim().toLowerCase());
                System.debug('Territory ext wholesaler ' + territory.Ext_Wholesaler__c);
                
                if((territory.Ext_Wholesaler__c != null) && (users[j].Name.trim().toLowerCase() == territory.Ext_Wholesaler__c.trim().toLowerCase())) {
                    user = users[j];
                    userFound = true;
                }
            }
            
            if (userFound) {
                extWholesailers.put(territory.Id, user.Id);
            }
        }

    }
    
    for (Contact contact: [select Id, Territory__c, OwnerId from Contact where Territory__c in : extWholesailers.keySet()]) {
        
        if (extWholesailers.containsKey(contact.Territory__c) && contact.OwnerId != extWholesailers.get(contact.Territory__c)) {
            contactIds.add(contact.Id);
        }       
    }
    
    System.debug('Contacts to be updated' + contactIds.size());
    System.debug('Territories ' + extWholesailers.keySet().size());
    
    if (System.isFuture() || System.isBatch()) {
        System.debug('this trigger can\'t be triggered by a batch/future process');
    } else {
            updateContactOwnerUtility.updateContactOwners(contactIds, extWholesailers, 10000);

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
@isTest(SeeAllData=true)
public class updateContactOwnerWithTerritory2_test {
  static testMethod void validateOwnerAllocationDuringTerritoryUpdate() {
      List<Territory__c> territories = [select Id, Ext_Wholesaler__c from Territory__c where Ext_Wholesaler__c != null and (not Ext_Wholesaler__c like 'WHOLESALER:%') limit 10];
    
      Boolean completed = false;
        
        for (Integer i = 0; (i < territories.size() && !completed); i++) {
            Territory__c territory = territories[i];
            List<User> users = [select Id, Name from User where (Name != null) and (not Name like :territory.Ext_Wholesaler__c) limit 1];
            
            if (users.size() > 0) {
                completed = true;
                User user = users[0];
                
                territory.Ext_Wholesaler__c = user.Name;
                Test.startTest();
                update territory;
                Test.stopTest();
                
                List<Contact> contacts = [select Id, OwnerId, Territory__c from Contact where Territory__c =: territory.Id]; 
                
                for (Contact contact: contacts) {
                    System.assertEquals(contact.OwnerId, user.Id);
                }
            }
        }
    }
}

ERROR SCREEN SHOT
User-added image
Shruti SShruti S
You have set the seeAllData as true for the Test Class. Because of this, all your data in the production will be visible in the test context, hence querying a large amount of data in the test context will take time. This is the reason of timing out in the production org. Whereas in the case of sandbox, you might not have large number of data and hence this error might not occur while running the test class. Whatever data is required in the test class should be generated in the test class itself.

Changing 
isTest(seeAllData=false)
line from your Test Class should solve the problem.
Leonard Silon 8Leonard Silon 8
That failed stating it must have at least 1% code coverage Thank you Leonard Silon Senior Salesforce.com Manager [cid:image005.jpg@01D2E38F.29D466E0][cid:image006.jpg@01D2E38F.29D466E0] Create a Retail Salesforce Support Case Create an Institutional Salesforce Support Case