//Trigger to prevent duplicate contacts. Matches based on Email.
trigger contactDeDuplicationTrigger on Contact (before insert, before update) {
//Map to store email and contact details
Map> emailConMap = new Map>();
//Your dynamic query
String contactDynamicQuery = 'select Id,Name,Email from Contact' ;
//Typecasting it to map
Map mapCon = new Map((List)Database.query(contactDynamicQuery));
//Looping the map keys to prepare a map for Email and Contact object
for(Id id:mapCon.keySet())
{
if(emailConMap.get(mapCon.get(id).Email)!=null)
emailConMap.get(mapCon.get(id).Email).put(Id,mapCon.get(id));
else
emailConMap.put(mapCon.get(id).Email, new Map{Id => mapCon.get(id)});
}
//Processing records
for(Contact con:trigger.new)
{
Map tempMap = new Map();
if(emailConMap.get(con.Email)!=null && !emailConMap.get(con.Email).containsKey(con.Id))
{
tempMap = emailConMap.get(con.Email);
String temp = 'Duplicates found:';
for(Id id:tempMap.keySet())
temp+=tempMap.get(id).Name+'('+id+'), ';
//Removing last comma
temp = temp.substring(0,temp.length()-2);
//Adding error to the record
con.addError(temp);
}
}
}
Friday, 2 August 2013
Contact DeDupe Trigger
Account DeDupe Trigger
//Trigger to prevent duplicate accounts. Matches records based on Account Name and Account Number
trigger accountDeDuplicationTrigger on Account(before insert, before update) {
//Map to store account name and account details
Map> accNameMap = new Map>();
//Map to store account number and account details
Map> accNumMap = new Map>();
//Your dynamic query
String accountDynamicQuery = 'select Id,Name,AccountNumber from Account' ;
//Typecasting it to map
Map mapAcc = new Map((List)Database.query(accountDynamicQuery));
//Looping the map keys to prepare a map for Account Name and Account Number
for(Id id:mapAcc.keySet())
{
if(accNameMap.get(mapAcc.get(id).Name)!=null)
accNameMap.get(mapAcc.get(id).Name).put(Id,mapAcc.get(id));
else
accNameMap.put(mapAcc.get(id).Name, new Map{Id => mapAcc.get(id)});
if(accNumMap.get(mapAcc.get(id).AccountNumber)!=null)
accNumMap.get(mapAcc.get(id).AccountNumber).put(Id,mapAcc.get(id));
else
accNumMap.put(mapAcc.get(id).AccountNumber, new Map{Id => mapAcc.get(id)});
}
//Processing records
for(Account acc:trigger.new)
{
Map tempMap = new Map();
if((accNameMap.get(acc.Name)!=null && !accNameMap.get(acc.Name).containsKey(acc.Id)) || (accNumMap.get(acc.AccountNumber)!=null && !accNumMap.get(acc.AccountNumber).containsKey(acc.Id)))
{
String temp = '';
if(accNameMap.get(acc.Name)!=null && !accNameMap.get(acc.Name).containsKey(acc.Id))
{
tempMap = accNameMap.get(acc.Name);
temp = 'Duplicate Account Name(s) found:';
for(Id id:tempMap.keySet())
temp+=tempMap.get(id).Name+'('+id+'), ';
}
else if(accNumMap.get(acc.AccountNumber)!=null && !accNumMap.get(acc.AccountNumber).containsKey(acc.Id))
{
tempMap = accNumMap.get(acc.AccountNumber);
temp = 'Duplicate Account Number(s) found:';
for(Id id:tempMap.keySet())
temp+=tempMap.get(id).Name+' - '+tempMap.get(id).AccountNumber+'('+id+'), ';
}
//Removing last comma
temp = temp.substring(0,temp.length()-2);
//Adding error to the record
acc.addError(temp);
}
}
}
Thursday, 1 August 2013
Schedulable Duplicate contact finder and email notification
// Comment
//Scheduled class for checking duplicate contacts and send an email with the duplicate emails.
//Code can be resued by changing the SOQL and search criteria
global class duplicateContactCheck Implements Schedulable
{
//String to store the html email content
public String htmlOutput{get;set;}
global void execute(SchedulableContext sc)
{
htmlOutput='';
duplicateContactMerger();
}
//Method to check for duplicate contacts based on email.
//Sending all the duplicate email address to predefined email address
public void duplicateContactMerger()
{
AggregateResult[] groupedResults = [SELECT Count(Id),Email FROM Contact GROUP BY Email HAVING COUNT(Id)>1];
system.debug('size--'+groupedResults.size());
htmlOutput='
Multiple contacts found for the following email addresses:
';
for(AggregateResult ar:groupedResults)
{
htmlOutput = htmlOutput+ar.get('Email')+'
';
}
htmlOutput=htmlOutput+'';
//Sending email only when duplicates are found.
if(groupedResults.size()>0)
sendmail();
}
//Method to send email
public void sendmail()
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
//Hard coded email can be replaced with Custom Setting also
string [] toaddress= New string[]{'test@test.com'};
email.setSubject('Duplicate contacts');
email.setHtmlBody(htmlOutput);
email.setToAddresses(toaddress);
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
}
}
Subscribe to:
Posts (Atom)