• D S.ax1052
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

I am attempting to implement a trigger that prevents duplicate contacts from being added.

 

Being a non developer (mere admin) I have download the app from the AppEx and although this gets me 90% of the way, I need to enhance the logic to include "Account Name". I have used a bit of trial and error (blantent guess work) but to no avail.

 

Below is the the apex trigger, but how do I include "Account"??

 

 

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 Email = :c.Email];
      
      if (contacts.size() > 0) {
          c.LastName.addError('Contact cannot be created - Contact already exists');
      }    
   }
}

 

Additionally I know that you need to test the trigger using an Apex Class, which of course I have, but wouldn't know how to tie in the changes above with below....if that makes sense.?!"

  

public class DuplicateContactTestClass {
   
   public static testMethod void testContactDuplicateTrigger() {
    Contact existingContact = new Contact(FirstName = 'John', LastName = 'Smith', Email = 'js@gmail.com');
    insert existingContact;
    
    Contact duplicateContact = new Contact(FirstName = 'John', LastName = 'Smith', Email = 'js@gmail.com');
    try {
       insert duplicateContact;
    }
    catch (Exception e) {
       System.debug('We want to see this.  This means the trigger is working.');
    } 
   }
}

 

 Help!!!

  • July 13, 2010
  • Like
  • 0