• agier
  • NEWBIE
  • 0 Points
  • Member since 2007

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 12
    Replies

I have a number of triggers in place on the Contacts object to prevent duplicate contacts from being entered. Now that we have multiple user groups on our org, I'd like to exclude records with a particular record type. Where would I insert this into the below two triggers for example? Thanks!

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

trigger ContactDuplicateTriggerAcct on Contact (before insert) {

    Map<String, Contact> contactLookup = new Map<String, Contact>();

    List<Id> accountIds = new List<Id>();

    List<String> firstNames = new List<String>();

    List<String> lastNames = new List<String>();

    for (Contact c : Trigger.new) {

        accountIds.add(c.AccountId);

        firstNames.add(c.FirstName);

        lastNames.add(c.LastName);

        contactLookup.put(c.AccountId + ':' + c.FirstName + ':' + c.LastName, c);

    }

 

    for (Contact c : [SELECT Id, AccountId, FirstName, LastName FROM Contact WHERE (FirstName in :firstNames and LastName in :lastNames) AND AccountId in :accountIds]) {

        if (contactLookup.containsKey(c.AccountId + ':' + c.FirstName + ':'  + c.LastName)) {

            contactLookup.get(c.AccountId + ':' + c.FirstName + ':' + c.LastName).LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');

        } 

    }

}

 

 

Email trigger:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

trigger ContactDuplicateTriggerEmail2 on Contact (before insert) {

    List<String> emailList = new List<String>();

    Set<String> lookupList = new Set<String>();

    for (Contact c : Trigger.new) {

        emailList.add(c.email);

    }

 

    for (Contact cl : [SELECT Email FROM Contact WHERE Email in :emailList AND Email != ''])

        lookupList.add(cl.Email);

 

    for (Contact c : Trigger.new) {

        if (lookupList.contains(c.Email)) {

            c.LastName.addError('Contact cannot be created. A contact already exists with the same email address.');

        }

    }

}

  • April 19, 2011
  • Like
  • 0
Would appreciate any advice on how to best re-write this trigger so I don't hit the SOQL limits on bulk loading. It works well for creating individual records. Thanks! I apologize the browser/message board won't allow me to format my posting so I am pasting it in text... 12345678 trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {for (Contact c : Trigger.new){Contact[] contacts= [select id from Contact where NameOffice_Combo__c=:c.NameOffice_Combo__c and NameOffice_Combo__c != ''];if (contacts.size() > 0) {c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');} }}
  • June 17, 2010
  • Like
  • 0

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!

  • May 26, 2010
  • Like
  • 0
I am new to creating a project in Eclipse. I have created the trigger and class but the class has an error/warning that says File saved only locally, not to Salesforce. When I look in my instance of Salesforce, I can see the trigger with the code but the Is Active box is not checked. When I look at the class, the name of it is there but none of the code. Can someone offer advice on these two issues? Also, when I double click on the error in Eclipse, it highlights the following code:

<?xml version="1.0" encoding="UTF-8"?>

  • June 03, 2008
  • Like
  • 0
I'm trying to test the sample provided on p. 118-119 of the Cookbook to prevent duplicate leads based on email address. I have come across two errors so far in the trigger:
 

Error: Compile Error: expecting a right parentheses, found 'get' at line 10 column 1

 

I seem to be able to get past this error by removing "get"

 

Error: Compile Error: Method does not exist or incorrect signature: [MAP:Id,SOBJECT:Lead].(Id) at line 9 column 16

I am not sure I know what is causing this error or how to fix. Any thoughts??

 

Here is the trigger from the cookbook:

 

trigger leadDuplicatePreventer on Lead (before insert, before update) {

Map<String, Lead> leadMap=new Map<String, Lead>();

for (Lead lead : System.Trigger.New) {

 

//make sure we don't treat an email address that

// isn't changing during an update as a duplicate.

if ((lead.Email !=null) &&

(System.Trigger.isInsert ||

(lead.Email != System.Trigger.oldMap.

get(lead.Id).Email))) {

 

//make sure another new lead isn't also a duplicate

if (leadMap.containsKey(lead.Email)) {

lead.Email.addError('Another new lead has the '

+'same email address.');

}else {

leadMap.put(lead.Email, lead);

}

}

}

 

//Using a single database query, find all the leads in

//the database that have the same email address as any

// of the leads being inserted or updated.

for (Lead lead : [SELECT email FROM Lead

WHERE Email IN :leadMap.KeySet ()]) {

Lead newLead=leadMap.get(lead.Email);

newLead.Email.addError('A lead with this email '

+ 'address already exists.');

}

}

 

 

 

  • May 20, 2008
  • Like
  • 0

I have a number of triggers in place on the Contacts object to prevent duplicate contacts from being entered. Now that we have multiple user groups on our org, I'd like to exclude records with a particular record type. Where would I insert this into the below two triggers for example? Thanks!

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

trigger ContactDuplicateTriggerAcct on Contact (before insert) {

    Map<String, Contact> contactLookup = new Map<String, Contact>();

    List<Id> accountIds = new List<Id>();

    List<String> firstNames = new List<String>();

    List<String> lastNames = new List<String>();

    for (Contact c : Trigger.new) {

        accountIds.add(c.AccountId);

        firstNames.add(c.FirstName);

        lastNames.add(c.LastName);

        contactLookup.put(c.AccountId + ':' + c.FirstName + ':' + c.LastName, c);

    }

 

    for (Contact c : [SELECT Id, AccountId, FirstName, LastName FROM Contact WHERE (FirstName in :firstNames and LastName in :lastNames) AND AccountId in :accountIds]) {

        if (contactLookup.containsKey(c.AccountId + ':' + c.FirstName + ':'  + c.LastName)) {

            contactLookup.get(c.AccountId + ':' + c.FirstName + ':' + c.LastName).LastName.addError('Contact cannot be created. A contact already exists with the same name-account combination.');

        } 

    }

}

 

 

Email trigger:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

trigger ContactDuplicateTriggerEmail2 on Contact (before insert) {

    List<String> emailList = new List<String>();

    Set<String> lookupList = new Set<String>();

    for (Contact c : Trigger.new) {

        emailList.add(c.email);

    }

 

    for (Contact cl : [SELECT Email FROM Contact WHERE Email in :emailList AND Email != ''])

        lookupList.add(cl.Email);

 

    for (Contact c : Trigger.new) {

        if (lookupList.contains(c.Email)) {

            c.LastName.addError('Contact cannot be created. A contact already exists with the same email address.');

        }

    }

}

  • April 19, 2011
  • Like
  • 0
Would appreciate any advice on how to best re-write this trigger so I don't hit the SOQL limits on bulk loading. It works well for creating individual records. Thanks! I apologize the browser/message board won't allow me to format my posting so I am pasting it in text... 12345678 trigger ContactDuplicateTriggerOfficePhone on Contact (before insert) {for (Contact c : Trigger.new){Contact[] contacts= [select id from Contact where NameOffice_Combo__c=:c.NameOffice_Combo__c and NameOffice_Combo__c != ''];if (contacts.size() > 0) {c.LastName.addError('Contact cannot be created. A contact already exists with the same name/office phone combination.');} }}
  • June 17, 2010
  • Like
  • 0

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!

  • May 26, 2010
  • Like
  • 0

I'm trying to implement a trigger to prevent duplicate contacts from being entered.  The criteria is that it should prevent the contact from being added if the first and last name and any of the phone numbers match those of an existing contact, or if the email matches an existing contact regardless of whether the name matches or not.

 

Here's the code I'm trying to get it to accept:

 trigger ContactDuplicateTrigger on Contact (before insert) {
for (Contact c : Trigger.new){
Contact[] contacts= [select id from Contact where
    (FirstName = :c.FirstName and LastName = :c.LastName) and
    (Phone=:c.Phone or MobilePhone=:c.MobilePhone or HomePhone=:c.HomePhone or OtherPhone=:c.OtherPhone or Home_Phone_2__c=:c.Home_Phone_2__c)
    or Email = :c.Email];
if (contacts.size() > 0) {
c.LastName.addError('Contact cannot be created - Contact already exists with the same email or name-phone combination.');
}
}
}

 

Here's the error it gives me:

 

 Error: Compile Error: expecting right square bracket, found 'or' at line 6 column 4

 

I tried adding in parentheses to group the logic correctly, but I wonder if Apex support it.  Can someone help me with the syntax to enforce the desired logic?

 

Thank you!

I'm trying to test the sample provided on p. 118-119 of the Cookbook to prevent duplicate leads based on email address. I have come across two errors so far in the trigger:
 

Error: Compile Error: expecting a right parentheses, found 'get' at line 10 column 1

 

I seem to be able to get past this error by removing "get"

 

Error: Compile Error: Method does not exist or incorrect signature: [MAP:Id,SOBJECT:Lead].(Id) at line 9 column 16

I am not sure I know what is causing this error or how to fix. Any thoughts??

 

Here is the trigger from the cookbook:

 

trigger leadDuplicatePreventer on Lead (before insert, before update) {

Map<String, Lead> leadMap=new Map<String, Lead>();

for (Lead lead : System.Trigger.New) {

 

//make sure we don't treat an email address that

// isn't changing during an update as a duplicate.

if ((lead.Email !=null) &&

(System.Trigger.isInsert ||

(lead.Email != System.Trigger.oldMap.

get(lead.Id).Email))) {

 

//make sure another new lead isn't also a duplicate

if (leadMap.containsKey(lead.Email)) {

lead.Email.addError('Another new lead has the '

+'same email address.');

}else {

leadMap.put(lead.Email, lead);

}

}

}

 

//Using a single database query, find all the leads in

//the database that have the same email address as any

// of the leads being inserted or updated.

for (Lead lead : [SELECT email FROM Lead

WHERE Email IN :leadMap.KeySet ()]) {

Lead newLead=leadMap.get(lead.Email);

newLead.Email.addError('A lead with this email '

+ 'address already exists.');

}

}

 

 

 

  • May 20, 2008
  • Like
  • 0