You need to sign in to do that
Don't have an account?
Kunal Purohit 4
Error "Variable does not exist: StageName"
Scenario for trigger is that, if Opportunity Object having record with field stageName=='Prospecting ' and Parent Account name with,lets say XYZ.,so another record with same field value and same parent should not get duplicated.. I have written below trigger but it is showing error.
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)) { if(acmap.get(opp.AccountId).StageName=='Prospecting') { (opp.AccountId).addError('Duplicate value'); } } } }
You need to iterate on all the opp records for the account. Please find the updated code below:
Let me know if there is any issue.
Thanks,
Abhishek Bansal.
All Answers
You need to iterate on all the opp records for the account. Please find the updated code below:
Let me know if there is any issue.
Thanks,
Abhishek Bansal.