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
GhulamGhulam 

Avoid Duplicate Name Only on master in Master-Detail relationship

Hi,
I have two custom object Claim__c(master) and Labour_Cost__c(detail).
They have master detail relationship.
I want to create a Trigger which prevent duplicate Name(standard field which is created default when we create any custom object) only on master object.
but we can multipe record with same Name from detail object and we select it from lookup field (Claim_Name__c) on detail object.
plz write a trigger for same.
for example: I have a record on Master Object with Name='S1 Claim' .
if i create a new record with Name 'S1 Claim' then it will display an error 'record already exists with same name'.
But,I can create multiple record with Name 'S1 Claim' from detail object.

Thanks,
Ghulam
 
Best Answer chosen by Ghulam
Nayana KNayana K
trigger on Claim__c(before insert)
{
	ClaimHandler objClaimHandler = new ClaimHandler();
	if(Trigger.isBefore && Trigger.isInsert)
		objClaimHandler.onBeforeInsert(Trigger.New);

}


public with sharing class ClaimHandler
{
	public ClaimHandler()
	{
	}

	public void onBeforeInsert(List<Claim__c> lstNewClaim)
	{
		
		Map<String,Claim__c> mapStrNameToClaim = new Map<String,Claim__c>();
		
		// Collect All Names from Trigger.New
		for(Claim__c objNewClaim : lstNewClaim)
			mapStrNameToClaim.put(objNewClaim.Name, objNewClaim);
		
		// iterate over matching claim and add error
		for(Claim__c objMatchingClaim : [SELECT Id,Name 
                                         FROM Claim__c 
                                         WHERE Name IN:mapStrNameToClaim.keySet())
		{
			mapStrNameToClaim.get(objMatchingClaim.Name).addError('Record already exists with same name!');
		}
		
	}
}

 

All Answers

GhulamGhulam
Hi frnds,
give me solution for same.. it means trigger avoid duplicate value  only on master object but not in detail object.
Nayana KNayana K
trigger on Claim__c(before insert)
{
	ClaimHandler objClaimHandler = new ClaimHandler();
	if(Trigger.isBefore && Trigger.isInsert)
		objClaimHandler.onBeforeInsert(Trigger.New);

}


public with sharing class ClaimHandler
{
	public ClaimHandler()
	{
	}

	public void onBeforeInsert(List<Claim__c> lstNewClaim)
	{
		
		Map<String,Claim__c> mapStrNameToClaim = new Map<String,Claim__c>();
		
		// Collect All Names from Trigger.New
		for(Claim__c objNewClaim : lstNewClaim)
			mapStrNameToClaim.put(objNewClaim.Name, objNewClaim);
		
		// iterate over matching claim and add error
		for(Claim__c objMatchingClaim : [SELECT Id,Name 
                                         FROM Claim__c 
                                         WHERE Name IN:mapStrNameToClaim.keySet())
		{
			mapStrNameToClaim.get(objMatchingClaim.Name).addError('Record already exists with same name!');
		}
		
	}
}

 
This was selected as the best answer
GhulamGhulam
Hi Nayana,
thank you so much.... ts working very well.

Regards,
Ghulam
V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
Hi Ghulam,

You can achieve the same functionality through customisation by using Duplicate Management.  
Duplicate Management will support for both Standard and Custom Objects.
you can find at Setup --> Data.com Administration​ --> Duplicate Management​
https://help.salesforce.com/HTViewHelpDoc?id=managing_duplicates_overview.htm

Thanks
Satya
GhulamGhulam
Hi Satya,
thnx for ur respone... and its really good to do same.
regards,
Ghulam