function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thomas CailletThomas Caillet 

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


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:     
    if ( o.StageName == 'Closed Won' )                     
        new Case(                                          
        Priority = 'Medium',                
        Status = 'New'));        
            insert listcase;    
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,
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 
This error has educated us ... . :)