0

I'd like to render a VF component if a list is empty, but the VF page is giving an error of "Unknown property 'CheckOpps.opportunity'. How to correct this error?

I am building the list in a custom controller:

public class CheckOpps {
    public List<Opportunity> CheckOpps() {
        Opportunity[] oList = [select id, name, amount from opportunity
                                            where amount = 1000];
        return oList;
    }
 }

and the VF page to reference the controller:

<apex:page controller="CheckOpps">
    
    <apex:form rendered="{!oList.size = 0}">
        <a href="https://google.com">Click Here</a>
    </apex:form>
    
</apex:page>
2

Your oList is currently just a local variable in your controller's constructor. It currently isn't able to be used outside of your constructor (because of how variable scoping works)

If you want to access information from your controller you'll need to declare it as a property of the controller class

public class CheckOpps {
    public List<Opportunity> oList {get; set;}

    public CheckOpps(){
        // unchanged
    }
}

or, alternatively, declare it as a member variable, and provide your own getter method (of the form public <return type> get<variable name>())

The documentation on Apex Properties contains more detailed information.

2
  • Thanks @Derek for the reply, I've changed controller code but still getting the same error on VF page. New code is: public class CheckOpps { public List<Opportunity> oList {get; set;} public List<Opportunity> CheckOpps() { Opportunity[] oList = [select id, name, amount from opportunity where amount = 1000]; return oList; } } – Pluto May 4 at 2:12
  • @Pluto My bad, when you write Opportunity[] oList = ..., you're shadowing your class property. Remove the Opportunity[] bit from that line so you're only setting the variable (instead of redeclaring it). If you're unfamiliar with any of those terms, I'd recommend going through some apex and visualforce modules on trailhead to work on the foundation of your programming knowledge. – Derek F May 4 at 2:23

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.