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.
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?
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 :) .
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.
Here is the sample code to prevent account record. You can modify, replace Account with Opportunity and implement.
Kindly mark it as the best answer if it helps.
Thank You,
Priya Ranjan
All Answers
Here is the sample code to prevent account record. You can modify, replace Account with Opportunity and implement.
Kindly mark it as the best answer if it helps.
Thank You,
Priya Ranjan
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.
in this case, do we need to write a trigger on the opportunity?
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 :) .
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.