You need to sign in to do that
Don't have an account?
Kunal Purohit 4
Issue in triggers
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'); } } } } }
trigger TriggerOpp on Opportunity (before update) {
You can use the below trigger code. You need to add a filter on the existing Opprtunity records that the stage should be equal to Prospecting.
Thanks,
Abhishek Bansal.