You need to sign in to do that
Don't have an account?
santhu 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.
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
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');
}
}
}
}