+ Start a Discussion
Thomas CailletThomas Caillet 

My Opportunity trigger doesn't recognize my standard fields on sandbox

Hello,

I need your help about something strange on my sandbox:
I want to write a trigger that create one case for each opportunity line items when my opportunity is "Closed Won".
But I'm a junior developper so I started to write just the case creation when opportunity is "Closed Won". But I've got this error: 
Error: Compile Error: Variable does not exist: StageName ...
I think the trigger don't recognize all fields.
Please tell me what I'm doing wrong. Thanks
Here is my "simple" code :

trigger createNewCase on Opportunity (before insert, before update) {  
  
List<Case> listcase = new List<Case>();    
for(Opportunity o: Trigger.new)     
    {        
     
    if ( o.StageName == 'Closed Won' )                     
        {                    
        listCase.add(                
        new Case(                                          
        Priority = 'Medium',                
        Status = 'New'));        
        }    
    }    
        if(listcase.size()>0)     
            {        
            insert listcase;    
            }
}
Abhishek_DEOAbhishek_DEO
DO not see any issue with your code as it saves 100% in my org. Please run below SOQL just to confirm whether you have access on stage field.

Select StageName from opportunity
Thomas CailletThomas Caillet
Thanks to your answer, I try SOQL and yes I have access on stage field.
I don't understand why I can't save my trigger :/
Anil MalneniAnil Malneni
Hi Thomas,

Above code looks good..Its working fine in my DEV environment.

Scenario: Whenever opportunity is created/updated with stage name as Closed Won.  New case will get created ..

let me know exact error message which you are geeting.

Thank you,
Anil
Thomas CailletThomas Caillet
Here my error : Error: Compile Error: Variable does not exist: StageName at line 7 column 10
Thomas CailletThomas Caillet
Ok I find my error, I have a class names "opportunity" so I could not save my trigger 
Thanks to you 
Abhishek_DEOAbhishek_DEO
This error has educated us ... . :)