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
SFDC12SFDC12 

duplicate recs

Hi experts ,how to prevent duplicate child (contact)records having same name related to particular account if contact lastname has same throws an error message.


Thanks in Advance.
Best Answer chosen by SFDC12
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Anusha,

Change the trigger as below and it should work.
 
trigger ContactDuplicate on Contact (before insert) 
{

	Set<Date> reviewDate = new Set<Date>();
    Set<String> Accountid= new set<String>();
	For(Contact con : trigger.new)
	{
		reviewDate.add(con.Review_Date__c);
        Accountid.add(con.accountid);
	}
	
	if(reviewDate.size() > 0 )
	{
		List<Contact> lstContact = [select Review_Date__c,ACCOUNTID ,id from CONTACT where Review_Date__c in :reviewDate and Accountid in :Accountid ];
		
		Map<Date ,Contact> mapNameWiseContact = new Map<Date,Contact>();
        Map<String ,Contact> mapNamewiseAccountid = new Map<String,Contact>();
		For(Contact acc: lstContact)
		{
			mapNameWiseContact.put(acc.Review_Date__c ,acc);
            mapNamewiseAccountid.put(acc.Accountid,acc);
		}
		
		For(Contact acc : trigger.new)
		{
			if(mapNameWiseContact.containsKey(acc.Review_Date__c) && mapNamewiseAccountid.containskey(acc.accountid))
			{
				acc.addError('Name already Exist ');
			}
		}
		
	}
}
If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try to use this trigger logic to check for duplicates.
 
trigger ContactDuplicate on Contact (before insert) 
{

	Set<String> laName = new Set<String>();
    Set<String> Accountid= new set<String>();
	For(Contact con : trigger.new)
	{
		laName.add(con.lastname);
        Accountid.add(con.accountid);
	}
	
	if(laName.size() > 0 )
	{
		List<Contact> lstContact = [select LASTNAME,ACCOUNTID ,id from CONTACT where lastname in :laName and Accountid in :Accountid ];
		
		Map<String ,Contact> mapNameWiseContact = new Map<String,Contact>();
        Map<String ,Contact> mapNamewiseAccountid = new Map<String,Contact>();
		For(Contact acc: lstContact)
		{
			mapNameWiseContact.put(acc.Lastname ,acc);
            mapNamewiseAccountid.put(acc.Accountid,acc);
		}
		
		For(Contact acc : trigger.new)
		{
			if(mapNameWiseContact.containsKey(acc.Lastname) && mapNamewiseAccountid.containskey(acc.accountid))
			{
				acc.addError('Name already Exist ');
			}
		}
		
	}
}

If this solution helps, Please mark it as best answer.

Thanks,
​​​​​​​
SFDC12SFDC12
Thanks praveen i tried this,but inplace of lastname i need to check for Date field and the field is Review_Date__c. Thanks in advance.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Anusha,

Change the trigger as below and it should work.
 
trigger ContactDuplicate on Contact (before insert) 
{

	Set<Date> reviewDate = new Set<Date>();
    Set<String> Accountid= new set<String>();
	For(Contact con : trigger.new)
	{
		reviewDate.add(con.Review_Date__c);
        Accountid.add(con.accountid);
	}
	
	if(reviewDate.size() > 0 )
	{
		List<Contact> lstContact = [select Review_Date__c,ACCOUNTID ,id from CONTACT where Review_Date__c in :reviewDate and Accountid in :Accountid ];
		
		Map<Date ,Contact> mapNameWiseContact = new Map<Date,Contact>();
        Map<String ,Contact> mapNamewiseAccountid = new Map<String,Contact>();
		For(Contact acc: lstContact)
		{
			mapNameWiseContact.put(acc.Review_Date__c ,acc);
            mapNamewiseAccountid.put(acc.Accountid,acc);
		}
		
		For(Contact acc : trigger.new)
		{
			if(mapNameWiseContact.containsKey(acc.Review_Date__c) && mapNamewiseAccountid.containskey(acc.accountid))
			{
				acc.addError('Name already Exist ');
			}
		}
		
	}
}
If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer