• ajayDixit
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    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