Last active
August 29, 2015 14:05
-
-
Save mdhorine/f6a8b57712be13a6bdd2 to your computer and use it in GitHub Desktop.
Sample code for an Apex trigger that checks for duplicates before insert and setting a duplicate flag if a duplicate exists in the database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger DuplicateCheckTrigger on Contact (before insert) { | |
/* For this example, we will search by name and a unique ID prior | |
to a new contact being inserted. If a contact is found, the | |
duplicate flag is set and the contact is then inserted into the | |
database. | |
*/ | |
/* For before insert triggers, you are intercepting a group of contact | |
records to be inserted and then modifying the data. At the end of | |
the trigger all records in trigger.new are inserted into the database, | |
so no need to do manual insertion yourself. | |
*/ | |
/* Steps: Get a list of the names and unique IDs to check. Pull matching | |
items from the database. Then check your trigger list against the | |
database items and update the duplicate flag for matches. | |
*/ | |
// Create an empty set of unique contact names and IDs to check against the db | |
// (The use of set reduces duplicate queries against the db) | |
Set<String> contactNames = new Set<String>(); | |
Set<String> passportIDs = new Set<String>(); | |
// Iterate through the set of trigger contacts and add the items to match | |
// into the empty sets. | |
for (Contact c : trigger.new) { | |
/* Note - Name on the contact (containing the full name) | |
is apparently derived after insert, so it doesn't appear on trigger.new. | |
In order to get around that, join the first and last name together and | |
then search. | |
*/ | |
String fullName = c.FirstName + ' ' + c.LastName; | |
System.debug('Looking for ' + fullName + ', First Name = ' + c.FirstName); | |
System.debug('Also looking for ' + c.Passport_ID__c); | |
contactNames.add(fullName.toUpperCase()); | |
passportIDs.add(c.Passport_ID__c); | |
} | |
// Create an empty map for the names. | |
Map<String, Contact> dbNames = new Map<String, Contact>(); | |
// Query the database using your names to match set and then iterate through | |
// the results. For each result, put the name and contact into your map. | |
for (Contact c : [SELECT Name, Id FROM Contact WHERE Name in :contactNames]) { | |
System.debug('I found ' + c.Name); | |
dbNames.put(c.Name.toUpperCase(), c); | |
} | |
// Create an empty map for the passport IDs | |
Map<String, Contact> dbPassports = new Map<String, Contact>(); | |
// Query the database using your passport IDs to match set and then iterate through | |
// the results. For each result, put the passport ID and contact into your map. | |
for (Contact c : [SELECT Passport_ID__c, Id FROM Contact WHERE Passport_ID__c in :passportIDs]) { | |
System.debug('I found ' + c.Passport_ID__c); | |
dbPassports.put(c.Passport_ID__c, c); | |
} | |
// Iterate back through the trigger records. If the trigger record matches | |
// any of the keys in your dbNames or dbPassports maps, set the duplicate flag | |
// to true. At the end of this section, the trigger ends and the items are | |
// inserted into the database. | |
for (Contact c : trigger.new) { | |
String fullName = c.FirstName + ' ' + c.LastName; | |
if (dbNames.containsKey(fullName.toUpperCase()) || dbPassports.containsKey(c.Passport_ID__c)) { | |
c.Duplicate_Flag__c = TRUE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment