function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC 2017SFDC 2017 

Trigger for avoiding Duplicates in Contact ....

Hi All,
  My scenario is I want custom Dupecatcher functionality for Contact Object using Trigger.For that condition Account is having two types of Recordtypes.Company and Person .So i created a formula field in Contact for getting that Account Recordtype in contact based on that formula field i created in contact it should check duplicates.Because the  email and mobile which they are  having in Person will be replicted in Company contacts also but it will not replicate in Person contatc types.Which means email and mobile in Person Contacts will also be in Company contacts but willnot be again in Person Contacts.

Can any one suggest me for writing trigger for this Scenario.


Instead of writing a complex trigger to check duplicates, you can achieve this functionality by creating an additional unique Text field on Contact, and create a simple trigger to combine the values of record-type, mobile and email in that field. Salesforce would give an error if duplicate values, i.e., duplicate combinations are entered. 
  1. Create a field named "MyUniqueCombination" on Contact, Text(255), Unique, Case-Insensitive 
  2. Write a trigger: 
trigger UpdateUniqueField on Contact (before insert) {
	//Query Account to get RecordTypeID, as formula field result won't appear in Before Trigger 
	Set<ID> accountIdSet = new Set<ID>(); 
	for(Contact c: {
	//get AccountMap, will use this to get RecordType later 
	Map<ID, Account> accMap = new Map<ID, Account>([select RecordTypeId from Account where id in :accountIdSet]); 
	for(Contact c: {
		//create combination and store in your new field.
		c.MyUniqueCombination__c = ''; 
		if(accMap.containsKey(c.AccountID) && accMap.get(c.AccountID)!=null && 
			accMap.get(c.AccountID).RecordTypeId !=null){
			c.MyUniqueCombination__c += accMap.get(c.AccountID).RecordTypeId;
		c.MyUniqueCombination__c += (c.Email!=null)?c.Email:'';
		c.MyUniqueCombination__c += (c.MobilePhone!=null)?c.MobilePhone:'';

Please modify the trigger for your requirement, but hope you get the idea. 

SFDC 2017SFDC 2017
I tried using your trigger but it is not throwing Duplicate error if the contact is already in Person type contacts.
SFDC 2017SFDC 2017
It should check for both Mobile and Email and also the account belonging to which recordtype.Duplicates canbe replicated in different Contact Types but not in same.If it is in same type it should throw error as Duplicate Exists.
Prabha, have you created the new field, and set it to Unique as well ? 

SFDC 2017SFDC 2017
Ya i created that field but then also it is not throwing .And my scenario is it should allow duplicate Email or Mobile in two record types but not in the same recordtypes
SFDC 2017SFDC 2017
Any Solution for this scenario ????
Hi Prabha, 

Can you create a sample record, and post the values of email, phone, record-type and this new field as well on this forum ? 

Ideally, when you create a new record, this new field should have a string value of recordtypeId, Email and Phone.