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
Behzad Bahadori 18Behzad Bahadori 18 

Trying to display the current Users Opportunities in the page

HI I have the following code but i get an error. I dont know why I cant show the recent opportunities 
public with sharing class OppExt {
    


     public List<Opportunity> myOpportunities { get; private set; }

     public OppExt(){
     myOpportunities = [ SELECT Id, Name, Account.Name
                 from Opportunity 
                 WHERE OwnerId = :UserInfo.getUserId()];

 }
 //Set<Opportunity> ids = new Set<Opportunity>();
        
public List<Opportunity> getOpp() {

 System.debug(myOpportunities);
   return myOpportunities;

}

}


Apex page


    <header>
        <div class="container">
            <div class="intro-text">
                               
                <a href="#services" class="page-scroll btn btn-xl">Create New Opportunity</a>
                   <a href="#services" class="page-scroll btn btn-xl">Add to Existing Opportunity</a>
                   <a href="#services" class="btn-custom">Add to Existing Opportunity</a>
                   <div class = "span4">
                           <button class="btn custom large f" >Foxtrot</button>
                   </div>
                   
            </div>
            <apex:outputText value="{!$User.Id}"/> 
            <apex:outputText value="{!$User.FirstName}"/>
            <apex:outputText value="{!$User.LastName}"/>
            <!-- <apex:outputfield value="{!currentuser.Id}"/> -->
            <apex:repeat >
            <apex:outputText value="{!myOpportunities.Name}"/>
            </apex:repeat>
            

        </div>
    </header>
</apex:page>
Best Answer chosen by Behzad Bahadori 18
ClintLeeClintLee
In you controller you don't need the getOpp() method.  The attribute myOpportunities can be accessed by name.

In your visualforce page your repeat is not setup to loop through the list.  You need to use the "value" and "var" attributes.

Try this:

Controller:
public with sharing class OppExt
{
    public List<Opportunity> myOpportunities { get; private set; }

    public OppExt()
    {
        myOpportunities = [ SELECT Id, Name, Account.Name
                 from Opportunity 
                 WHERE OwnerId = :UserInfo.getUserId()];

    }
}

Visualforce:
 
<header>
    <div class="container">
        <div class="intro-text">                       
            <a href="#services" class="page-scroll btn btn-xl">Create New Opportunity</a>
            <a href="#services" class="page-scroll btn btn-xl">Add to Existing Opportunity</a>
            <a href="#services" class="btn-custom">Add to Existing Opportunity</a>
            <div class = "span4">
                <button class="btn custom large f" >Foxtrot</button>
            </div>       
        </div>

        <apex:outputText value="{!$User.Id}"/> 
        <apex:outputText value="{!$User.FirstName}"/>
        <apex:outputText value="{!$User.LastName}"/>
        <!-- <apex:outputfield value="{!currentuser.Id}"/> -->
        
         <apex:repeat value="{!myOpportunities}" var="opp">
             <apex:outputText value="{!opp.Name}"/>
         </apex:repeat>
    </div>
</header>

Hope that helps,

Clint

All Answers

ClintLeeClintLee
In you controller you don't need the getOpp() method.  The attribute myOpportunities can be accessed by name.

In your visualforce page your repeat is not setup to loop through the list.  You need to use the "value" and "var" attributes.

Try this:

Controller:
public with sharing class OppExt
{
    public List<Opportunity> myOpportunities { get; private set; }

    public OppExt()
    {
        myOpportunities = [ SELECT Id, Name, Account.Name
                 from Opportunity 
                 WHERE OwnerId = :UserInfo.getUserId()];

    }
}

Visualforce:
 
<header>
    <div class="container">
        <div class="intro-text">                       
            <a href="#services" class="page-scroll btn btn-xl">Create New Opportunity</a>
            <a href="#services" class="page-scroll btn btn-xl">Add to Existing Opportunity</a>
            <a href="#services" class="btn-custom">Add to Existing Opportunity</a>
            <div class = "span4">
                <button class="btn custom large f" >Foxtrot</button>
            </div>       
        </div>

        <apex:outputText value="{!$User.Id}"/> 
        <apex:outputText value="{!$User.FirstName}"/>
        <apex:outputText value="{!$User.LastName}"/>
        <!-- <apex:outputfield value="{!currentuser.Id}"/> -->
        
         <apex:repeat value="{!myOpportunities}" var="opp">
             <apex:outputText value="{!opp.Name}"/>
         </apex:repeat>
    </div>
</header>

Hope that helps,

Clint
This was selected as the best answer
Behzad Bahadori 18Behzad Bahadori 18
Thank you very much.