I'm brand new to Visualforce, and I built this simple page to show user opportunities by watching a tutorial and the teacher shows how to build it. I'd like to implement the page in my live org in a home page component, but I want it to only show the opportunities for the active user, i.e. if user x is logged in they only see opportunities owned by them, not all opportunities in our org. What code do I need to add/where can I learn how to add something like that?

Thanks. first question on Stack exchange so sorry if isn't formatted well.

Summary Controller:

public with sharing class SidebarSummaryController {

public Map<String, Integer> oppstages { get; set; }

public SidebarSummaryController() {
    oppStages = new Map<String, Integer>();
    for(AggregateResult ar : [Select StageName, count(ID) cnt FROM Opportunity GROUP BY StageName]) {
        oppStages.put((String) ar.get('StageName'), (Integer) ar.get('cnt'));

VF page:

<apex:page controller="SidebarSummaryController" showheader="false" sidebar="false" >
<apex:pageblock title="Opportunity Stages">
    <apex:pageBlockTable value="{!oppstages}" var="stage">
        <apex:column value="{!stage}" headerValue="Stage"/>
        <apex:column value="{!oppStages[Stage]}" headerValue="# Opps"/>
  • Have you looked into using a report or dashboard instead?
    – pchittum
    Commented Aug 31, 2015 at 1:37

1 Answer 1


"The with sharing clause" on your apex controller should automatically take care of the security or the sharing rules defined in your org .Only those opportunities the User has access the query will generate for you ."

In case you would like to strictly restrict to ones that user owns ,then filter ownerId with logged in User ID

for(AggregateResult ar : [Select StageName, count(ID) cnt FROM Opportunity where ownerId=:userinfo.getuserId() GROUP BY StageName]) {
    oppStages.put((String) ar.get('StageName'), (Integer) ar.get('cnt'));

The security is pretty complex topic .I would recommend you to go through Trailhead data security module for in depth analysis


There are chances some users may share their opportunities or there is sharing rule that shares the opportunities and in that case filtering by ownerId is not good and hence your with sharing clause on the class should automatically handle security for you .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .