+ Start a Discussion
Kunal Purohit 4Kunal 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');
           }
        }
    }
}
Best Answer chosen by Kunal Purohit 4
Abhishek BansalAbhishek Bansal
Hi Kunal,

You need to iterate on all the opp records for the account. Please find the updated code below:
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)
        {
           for(Opportunity opp : acmap.get(opp.AccountId).Opportunities)
		   {
			   if(opp.StageName=='Prospecting')
			   {
				   (opp.AccountId).addError('Duplicate value');
			   }
		   }
        }
    }
}

Let me know if there is any issue.

Thanks,
Abhishek Bansal.​​​​​​​

All Answers

Abhishek BansalAbhishek Bansal
Hi Kunal,

You need to iterate on all the opp records for the account. Please find the updated code below:
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)
        {
           for(Opportunity opp : acmap.get(opp.AccountId).Opportunities)
		   {
			   if(opp.StageName=='Prospecting')
			   {
				   (opp.AccountId).addError('Duplicate value');
			   }
		   }
        }
    }
}

Let me know if there is any issue.

Thanks,
Abhishek Bansal.​​​​​​​
This was selected as the best answer
Kunal Purohit 4Kunal Purohit 4
Thanks Abhishek... However, inner for loop was showing error,  duplicate opp. I tried removing it,code got run Thnaks a lot..