forked from frankverrill/trailhead-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRestrictContactByName.cls
93 lines (72 loc) · 3.36 KB
/
TestRestrictContactByName.cls
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
87
88
89
90
91
92
93
@isTest
private class TestRestrictContactByName {
@isTest
static void TestPositive(){
Contact SingleTestPositiveContact= new Contact(FirstName='Lovely', LastName='InvalidName',Email='[email protected]');
//Action
Test.startTest();
Database.SaveResult result = Database.insert(SingleTestPositiveContact, false);
Test.stopTest();
//Assert
System.assert(!result.isSuccess());
System.assert(result.getErrors().size()>0);
System.assertEquals('The Last Name "'+SingleTestPositiveContact.LastName+'" is not allowed for DML' ,
result.getErrors()[0].getMessage());
system.debug(result.getErrors()[0].getMessage());
}
@isTest
static void TestNegative(){
Contact SingleTestNegativeContact= new Contact(FirstName='Lovely', LastName='InvalidName1', Email='[email protected]');
//Action
Test.startTest();
Database.SaveResult result = Database.insert(SingleTestNegativeContact, false);
Test.stopTest();
//Assert
System.assert(result.isSuccess());
System.assert(result.getErrors().size()==0);
system.debug('Successfully Inserted:'+result.getId());
}
@isTest
static void TestforBulk(){
List<Contact> TestContactList = new List<Contact>{
new Contact(FirstName='Happy', LastName='InvalidName', Email='[email protected]'),
new Contact(FirstName='NotSoHappy', LastName='ValidName',Email='[email protected]'),
new Contact(Firstname='Fine', LastName='InvalidName1', Email='[email protected]'),
new Contact(Firstname='Fine', LastName='FineLastName', Email='[email protected]')};
//Action
Test.startTest();
Database.SaveResult[] result = Database.insert(TestContactList, false);
Test.stopTest();
// Iterate through each returned result
for (Database.SaveResult sr :result) {
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted Contact. Contact ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Contact fields that affected this error: ' + err.getFields());
}
}
}
}
@isTest
static void TestforUpdateContact(){
Contact TestCon = new Contact(FirstName='Superb', Lastname='FineLastName', Email='[email protected]');
insert TestCon;
system.debug(TestCon.Id+': '+TestCon.LastName);
TestCon.LastName='InvalidName';
//Action
Test.startTest();
Database.SaveResult result = Database.update(TestCon, false);
Test.stopTest();
System.assert(!result.isSuccess());
System.assert(result.getErrors().size()>0);
System.assertEquals('The Last Name "'+TestCon.LastName+'" is not allowed for DML' ,
result.getErrors()[0].getMessage());
system.debug(result.getErrors()[0].getMessage());
}
}