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
agieragier 

Help Resolving Class test failure - System.AssertException: Assertion Failed

I am working on preventing dup contacts by matching email address. The trigger seems fine but am struggling with writing the class. Here is the first part of the code for the class. Error is at line 36.

 

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
public class testemailDup { 
static testMethod void testContactDuplicateEmailTrigger2() {
// First make sure there are no contacts already in the system
// that have the email addresses used for testing
Set<String> testEmailAddress = new Set<String>();
testEmailAddress.add('test1@duptest.com');
testEmailAddress.add('test2@duptest.com');
testEmailAddress.add('test3@duptest.com');
testEmailAddress.add('test4@duptest.com');
testEmailAddress.add('test5@duptest.com');
System.assert([SELECT count() FROM Contact WHERE Email IN :testEmailAddress] == 0);
// Seed the database with some contacts, and make sure they can
// be bulk inserted successfully.
Contact contact1 = new Contact(LastName='Test1',Email='test1@duptest.com');
Contact contact2 = new Contact(LastName='Test2',Email='test4@duptest.com');
Contact contact3 = new Contact(LastName='Test3',Email='test5@duptest.com');
Contact[] Contacts = new Contact[] {contact1, contact2, contact3};
insert contacts;
// Now make sure that some of these contactss can be changed and
// then bulk updated successfully. Note that contact1 is not
// being changed, but is still being passed to the update
// call. This should be OK.
contact2.Email = 'test2@duptest.com';
contact3.Email = 'test3@duptest.com';
update contacts;
// Make sure that single row Contact duplication prevention works
// on insert.
Contact dup1 = new Contact(LastName='Test1Dup',Email='test1@duptest.com');
try {
insert dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
System.assert(e.getDmlFields(0)[0] == Contact.Email);
System.assert(e.getDmlMessage(0).indexOf(
'A contact with this email address already exists.') > -1);
}

Here's the trigger I am using which is really simple.

 

trigger ContactDuplicateTriggerEmail2 on Contact (before insert) {
for (Contact c : Trigger.new){
Contact[] contacts= [select id from Contact where Email = :c.Email and Email != ''];
if (contacts.size() > 0 ) {
c.LastName.addError('Contact cannot be created. A contact already exists with the same email.');
} 
}
}

 

I have another trigger which I need to have a class for to prevent dups based on name & account. Is there a way to integrate that into my existing class or an easy way to write a new class to cover it?

1
2
3
4
5
6
7
8
9

trigger ContactDuplicateTriggerAcct on Contact (before insert) {
for (Contact c : Trigger.new){
Contact[] contacts= [select id from Contact where 
    (FirstName = :c.FirstName and LastName = :c.LastName and AccountID=:c.AccountID)];
if (contacts.size() > 0) {
c.LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');

}
}

 

Thanks!

Anand@SAASAnand@SAAS

Please reformat the code and include it as code using the "C" icon. It's a little hard to read.

agieragier

Sorry about that! I have updated the posting. Thanks for looking! I have some really basic triggers which will make a big difference if I can deploy them. Obviously I am not a "developer" so it is much appreciated!

Anand@SAASAnand@SAAS

Your test class is looking for an error in Contact.Email whereas you are adding the error to Contact.LastName. I would print out the e.getDMLFields() to determine if that has any data and assert suitably.

 

Also, I see you are doing SOQL inside a "for" loop. That's not a good practice. Hope you were going to change that yo "bulkify" the trigger so that you don't run into governor limits.