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
James HayesJames Hayes 

How do I reference My Opportunities

Hey All, 

I am trying to get a visualforce page to reference the "My Opportunities" view in salesforce.  I am doing a list view on the left and a page view of the opportunity on the right.  But, I only want it to view the opportunities that are associated to the owner opening the visualforce page. 

The left, list of opportunities should only be those belonging to the logged in user

Here is my current code.





<apex:page standardController="Opportunity" recordSetVar="ops" sidebar="false">
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:pageBlock Title="List of Opportunities">
        <apex:pageblockTable value="{!ops}" var="o">
            <apex:column >
                <apex:commandLink value="{!o.Name}" reRender="refresharea">
                    <apex:param name="OpportunityID" value="{!o.ID}"/>
                    <apex:param name="OpportunityName"  value="{!o.Name}" />
                    </apex:commandLink>             
            </apex:column>
            <apex:column value="{!o.Amount}"/>
            <apex:inlineEditSupport />
            <apex:column value="{!o.StageName}"/>
            <apex:column value="{!o.Closedate}"/>

        </apex:pageBlockTable>
    </apex:PageBlock>
    
    <apex:pageBlock title="{!$CurrentPage.parameters.OpportunityName}" id="refresharea">
        <apex:detail subject="{!$CurrentPage.parameters.opportunityID}" relatedList="false"/>
    </apex:pageBlock>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>


Any help would be appreciated.  I am fairly new at this.  Thanks
 
Best Answer chosen by James Hayes
AnjunaAnjuna
Can you try this?
Apex controller
public class MyOpportunityController{
    public List<Opportunity> oppList{get;set;}
    public MyOpportunityController(ApexPages.StandardSetController controller) {
        oppList = new List<Opportunity>();
        for(Opportunity opp : [SELECT id,name,amount,stagename,closedate,ownerid FROM Opportunity
                                WHERE owner.id =: userinfo.getuserid()]) {
            oppList.add(opp );
        }
    }

}

VF Page
<apex:page standardController="Opportunity" recordSetVar="ops" sidebar="false" extensions="MyOpportunityController">
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:pageBlock Title="List of Opportunities">
        <apex:pageblockTable value="{!ops}" var="o">
            <apex:column >
                <apex:commandLink value="{!o.Name}" reRender="refresharea">
                    <apex:param name="OpportunityID" value="{!o.ID}"/>
                    <apex:param name="OpportunityName"  value="{!o.Name}" />
                    </apex:commandLink>             
            </apex:column>
            <apex:column value="{!o.Amount}"/>
            <apex:inlineEditSupport />
            <apex:column value="{!o.StageName}"/>
            <apex:column value="{!o.Closedate}"/>

        </apex:pageBlockTable>
    </apex:PageBlock>
    
    <apex:pageBlock title="{!$CurrentPage.parameters.OpportunityName}" id="refresharea">
        <apex:detail subject="{!$CurrentPage.parameters.opportunityID}" relatedList="false"/>
    </apex:pageBlock>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

All Answers

AnjunaAnjuna
Hi James,
There is an attribute 'rendered' in apex:column.You can check whether opportunity owner is same as the logged in user in this attribute. Column will be rendered only if the value of 'rendered' attribute is true.(By default it is true).
Give condition as follows.
rendered="{!CASESAFEID(o.ownerid) == $User.Id}"
Return value of o.ownerid in visualforce page is 15 digit and $User.Id is 18 digit. That is why CASESAFEID() is used for converting the 15 digit ID to 18 digit and make them comparable.
See the Modified code
 <apex:pageblockTable value="{!ops}" var="o">
            <apex:column rendered="{!CASESAFEID(o.ownerid) == $User.Id}">
                <apex:commandLink value="{!o.Name}" reRender="refresharea">
                    <apex:param name="OpportunityID" value="{!o.ID}"/>
                    <apex:param name="OpportunityName"  value="{!o.Name}" />
                    </apex:commandLink>             
            </apex:column>
            
            <apex:column value="{!o.Amount}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>
            <apex:inlineEditSupport />
            <apex:column value="{!o.StageName}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>
            <apex:column value="{!o.Closedate}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>
        </apex:pageBlockTable>
    </apex:PageBlock>
It will return opportunities of logged in user only.
James HayesJames Hayes
Hey Anjuna, 

The code looks like it is working with one exception.  It does not show the opportunity name with the command link.  It shows the following without the opportunity name. 

User-added image

Here is the code. 
 
<apex:page standardController="Opportunity" recordSetVar="ops" sidebar="false">
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:pageBlock Title="List of Opportunities">
        <apex:pageblockTable value="{!ops}" var="o">
            <apex:column rendered="{CASESAFEID(o.ownerid) == $User.Id}">
                <apex:commandLink value="{!o.Name}" reRender="refresharea">
                    <apex:param name="OpportunityID" value="{!o.ID}"/>
                    <apex:param name="OpportunityName"  value="{!o.Name}" />
                    </apex:commandLink>             
            </apex:column>
            
            <apex:column value="{!o.Amount}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>
            <apex:inlineEditSupport />
            <apex:column value="{!o.StageName}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>
            <apex:column value="{!o.Closedate}" rendered="{!CASESAFEID(o.ownerid) == $User.Id}"/>

        </apex:pageBlockTable>
    </apex:PageBlock>
    
    <apex:pageBlock title="{!$CurrentPage.parameters.OpportunityName}" id="refresharea">
        <apex:detail subject="{!$CurrentPage.parameters.opportunityID}" relatedList="false"/>
    </apex:pageBlock>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Can you assist?  Appreciate it! 
AnjunaAnjuna
Hi James,
Replace this line in your code with the below line.
            <apex:column rendered="{!CASESAFEID(o.ownerid) == $User.Id}">
You have missed '!'. That is the reson the name of opportunity is not displaying in your view.
James HayesJames Hayes
You're awesome! Really appreciate the help.  Thanks!
James HayesJames Hayes
Okay, 

Running into a new problem.  It looks like the code is only pulling My Opportunities if I actually click on a my opportunities report prior to previewing the code.  If I select another report, then it just comes back blank.  Any thoughts on this?  Here is a snapshot of the report I click in the opportunity object. 

User-added image

If I select another report and then preview the VF, nothing shows up. 
AnjunaAnjuna
Hi James,
This page does not specify a filter in the request, so the page is displayed with the last used filter. For information on using filters with list controllers, see https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_sosc_list_views.htm
You can simply achieve this functionality by adding a custom controller as an extension and fetch required opportunities in its constructor.
AnjunaAnjuna
Hi James, 
If  we are using standard controller and recordsetvar for fetching data, it will display records only in the current view. For more detail see https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_sosc_list_views.htm
AnjunaAnjuna
Can you try this?
Apex controller
public class MyOpportunityController{
    public List<Opportunity> oppList{get;set;}
    public MyOpportunityController(ApexPages.StandardSetController controller) {
        oppList = new List<Opportunity>();
        for(Opportunity opp : [SELECT id,name,amount,stagename,closedate,ownerid FROM Opportunity
                                WHERE owner.id =: userinfo.getuserid()]) {
            oppList.add(opp );
        }
    }

}

VF Page
<apex:page standardController="Opportunity" recordSetVar="ops" sidebar="false" extensions="MyOpportunityController">
<apex:form >
    <apex:pageBlock >
    <apex:pageBlockSection >
    <apex:pageBlock Title="List of Opportunities">
        <apex:pageblockTable value="{!ops}" var="o">
            <apex:column >
                <apex:commandLink value="{!o.Name}" reRender="refresharea">
                    <apex:param name="OpportunityID" value="{!o.ID}"/>
                    <apex:param name="OpportunityName"  value="{!o.Name}" />
                    </apex:commandLink>             
            </apex:column>
            <apex:column value="{!o.Amount}"/>
            <apex:inlineEditSupport />
            <apex:column value="{!o.StageName}"/>
            <apex:column value="{!o.Closedate}"/>

        </apex:pageBlockTable>
    </apex:PageBlock>
    
    <apex:pageBlock title="{!$CurrentPage.parameters.OpportunityName}" id="refresharea">
        <apex:detail subject="{!$CurrentPage.parameters.opportunityID}" relatedList="false"/>
    </apex:pageBlock>
    </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

 
This was selected as the best answer
James HayesJames Hayes
Hey Anjuna, 

Appreciate it.  This worked, I have one additional question though, would there be a way to have it only display curren open opportunities that belong to the user viewing it? 
AnjunaAnjuna
Hi James,
Current Open opportunities means the opportunities with status not equal to closed. Right?
If Right, modify the query as follows.
SELECT id,name,amount,stagename,closedate,ownerid FROM Opportunity WHERE owner.id =: userinfo.getuserid() AND isClosed=false