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
santhu kumarsanthu kumar 

Need Help: Write a trigger to prevent User to create duplicate opportunity record

Write a trigger to prevent the User to create a duplicate opportunity record under one Account, but the same name can be created in another Account record. so Under the same account, no duplicate opportunity record can't be created.
Best Answer chosen by santhu kumar
AnkaiahAnkaiah (Salesforce Developers) 
Hi Santhu,

try with below code.
trigger OpportunityDuplicate on Opportunity (before insert)
{

	Set<String> setName = new Set<String>();
	set<id> accids = new set<id>();
	For(Opportunity acc : trigger.new)
	{
		setName.add(acc.name);
		accids.add(acc.accountId);
	}
	
	if(setName.size() > 0 )
	{
		List<Opportunity> lstOpportunity = [select name ,id from Opportunity where name in :setName AND AccountId IN :accids];
		
		Map<String ,Opportunity> mapNameWiseOpportunity = new Map<String,Opportunity>();
		For(Opportunity acc: lstOpportunity)
		{
			mapNameWiseOpportunity.put(acc.name ,acc);
		}
		
		For(Opportunity acc : trigger.new)
		{
			if(mapNameWiseOpportunity.containsKey(acc.name))
			{
				acc.Name.addError('Name already Exist ');
			}
		}
		
	}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Santhu,

try with below code.
trigger OpportunityDuplicate on Opportunity (before insert)
{

	Set<String> setName = new Set<String>();
	set<id> accids = new set<id>();
	For(Opportunity acc : trigger.new)
	{
		setName.add(acc.name);
		accids.add(acc.accountId);
	}
	
	if(setName.size() > 0 )
	{
		List<Opportunity> lstOpportunity = [select name ,id from Opportunity where name in :setName AND AccountId IN :accids];
		
		Map<String ,Opportunity> mapNameWiseOpportunity = new Map<String,Opportunity>();
		For(Opportunity acc: lstOpportunity)
		{
			mapNameWiseOpportunity.put(acc.name ,acc);
		}
		
		For(Opportunity acc : trigger.new)
		{
			if(mapNameWiseOpportunity.containsKey(acc.name))
			{
				acc.Name.addError('Name already Exist ');
			}
		}
		
	}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer
santhu kumarsanthu kumar
Hi Ankaiah,
perfect, it works, Kudos, thanks for the quick reply. 
just changed names according to my requirement,.. Here is the working Apex Class.

public static void opportunityDuplicate(List<opportunity> newOpportunity){
        Set<String> setOfOpp = new Set<String>();
        Set<Id> setOfAccId = new Set<Id>();
        List<Opportunity> lstOpp = new List<opportunity>();
        Map<String,Opportunity> mapNameWiseOpportunity = new Map<String,Opportunity>();
        for(Opportunity opp : newOpportunity){
            setOfOpp.add(opp.Name);
            setOfAccId.add(opp.AccountId);
         }
        if(setOfOpp.size()>0){
            lstOpp=[select name,Id from Opportunity where name IN:setOfOpp AND AccountId IN:setOfAccId];
            }
        for(Opportunity opp : lstOpp){
            mapNameWiseOpportunity.put(opp.name, Opp);
        }
        for(Opportunity acc : newOpportunity){
            if(mapNameWiseOpportunity.containsKey(acc.Name)){
                acc.Name.addError('Name Already Exist');
            }
        }
    }
}