// 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});
}
}
Thursday, 1 August 2013
Schedulable Duplicate contact finder and email notification
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment