Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.

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');

}

}

}

}

}

4 answers
Loading
0/9000