I am writing this trigger to prevent duplicate entry. But this trigger runs even at time of creating new record..How to resolve?
trigger TriggerOpp on Opportunity (before insert,before update) {
set<id> setid=new set<id>();
for(Opportunity opp:trigger.new)
{
setid.add(opp.AccountId);
}
Map<id,Account> acmap=new map<id,Account>();
for(account acc:[select id,Name,(select id,Name,StageName from Opportunities) from Account where id in:setid])
{
acmap.put(acc.id,acc);
}
for(Opportunity opp:trigger.new)
{
if(acmap.containsKey(opp.AccountId) && acmap.get(opp.AccountId).Opportunities!=null)
{
{
if(opp.StageName=='Prospecting')
{
(opp.AccountId).addError('Duplicate value');
}
}
}
}
}
I have junction object Author - research paper. Prevent duplicate Author for same research paper help on this