1

Could you pls explain why I get this Error? Thank you! I try save

trigger ONOpp on Opportunity (after insert) {
    for(Opportunity opp : Trigger.new){

    }

}

And get this error Error:(6, 5) Invalid loop variable type expected Opportunity was Opportunity

0
3

This happens if you have a class named Opportunity.
In such case, Opportunity opp is a reference to the custom class Opportunity, while Trigger.New holds references to Schema.Opportunity.
Writing for (Schema.Opportunity opp : Trigger.new) { will fix the error.

Docs:

you must fully qualify your uses of Schema namespace elements when they have naming conflicts with items in your unmanaged code. If your org contains an Apex class that has the same name as an sObject, add the Schema namespace prefix to the sObject name in your code.

By the way, I strongly advise against giving standard objects names to custom classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.