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 

can anyone help me with the below trigger? Write a trigger to prevent to create duplicate opportunity record based on the name under same account.

Best Answer chosen by santhu kumar
PriyaPriya (Salesforce Developers) 
Hey Santhu,

Here is the sample code to prevent account record. You can modify, replace Account with Opportunity and implement.
trigger AccountDuplicate on Account (before insert)
{

	Set<String> setName = new Set<String>();
	For(Account acc : trigger.new)
	{
		setName.add(acc.name);
	}
	
	if(setName.size() > 0 )
	{
		List<Account> lstAccount = [select name ,id from account where name in :setName ];
		
		Map<String ,Account> mapNameWiseAccount = new Map<String,Account>();
		For(Account acc: lstAccount)
		{
			mapNameWiseAccount.put(acc.name ,acc);
		}
		
		For(Account acc : trigger.new)
		{
			if(mapNameWiseAccount.containsKey(acc.name))
			{
				acc.Name.addError('Name already Exist ');
			}
		}
		
	}
}

Kindly mark it as the best answer if it helps.

Thank You,
Priya Ranjan
 

All Answers

PriyaPriya (Salesforce Developers) 
Hey Santhu,

Here is the sample code to prevent account record. You can modify, replace Account with Opportunity and implement.
trigger AccountDuplicate on Account (before insert)
{

	Set<String> setName = new Set<String>();
	For(Account acc : trigger.new)
	{
		setName.add(acc.name);
	}
	
	if(setName.size() > 0 )
	{
		List<Account> lstAccount = [select name ,id from account where name in :setName ];
		
		Map<String ,Account> mapNameWiseAccount = new Map<String,Account>();
		For(Account acc: lstAccount)
		{
			mapNameWiseAccount.put(acc.name ,acc);
		}
		
		For(Account acc : trigger.new)
		{
			if(mapNameWiseAccount.containsKey(acc.name))
			{
				acc.Name.addError('Name already Exist ');
			}
		}
		
	}
}

Kindly mark it as the best answer if it helps.

Thank You,
Priya Ranjan
 
This was selected as the best answer
santhu kumarsanthu kumar
Hi Priya,
Thanks for the response,
the code should prevent users to create a duplicate opportunity name under one account, but the same opportunity name can be created in another account.
so no duplicate opportunity names under one account. 
santhu kumarsanthu kumar
to be more precise if we have 4 account records in all 4 records opportunity name xyz can be created but the same xyz can't be created 2nd time in any account as one opportunity already exist. 
in this case, do we need to write a trigger on the opportunity? 
santhu kumarsanthu kumar
i managed to copy your code and try to implement on Opportunity but i am getting error. can you take a look and advise?
Trigger Part:
if(trigger.isBefore && trigger.isInsert){
        trgOpportunityHandler.opportunityDuplicate(trigger.new, trigger.oldMap);
    }

Class part:
public static void opportunityDuplicate(List<opportunity> newOpportunity){
        Set<String> setOfOpp = new Set<String>();
        Map<String, Opportunity> mapNameOpp = new Map<String,Opportunity>();
        for(Opportunity opp : newOpportunity){
            if(setOfOpp.size()>0){
                List<Opportunity> lstOpp = [select name ,id from Opportunity where name in :setofOpp];
                for(Opportunity opp1:lstOpp){
                    mapNameOpp.put(opp.Name, opp1);
                }
                for(Opportunity oppEach : newOpportunity){
                    if(mapNameOpp.containsKey(opp.Name)){
                        opp.Name.addError('Name Already Exist');
                    }
                }
            }
         }
    }
}

seems i am missing something, but not sure what it is :) .
Minnesota MARKMinnesota MARK
Welcome mnsud2l
Please log in to Minnesota State University Moorhead's D2L Brightspace to view your courses. Please click here for a System Check before you login.