You need to sign in to do that
Don't have an account?
Leonard 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
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
Changing line from your Test Class should solve the problem.