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
Salesforce BlitzSalesforce Blitz 

limit dashboard data based on user filter in apex

Hi.

I have created a visual dashboard in salesforce along with a filter at top which displays names of users.

I need the dashboard to be changed when the user is changed in dropdown.


How to achieve this.


CONTROLLER:
<apex:page controller="TopFiveOpenDeals" tabStyle="Opportunity">
    <apex:form >
    <apex:selectList label="Users" multiselect="false" size="1">
            <apex:actionSupport event="onchange" action="{!UpdateSelectedFirstItem}" rerender="page"  />
            <apex:selectOptions value="{!owner_list}"></apex:selectOptions>
    </apex:selectList>
    </apex:form>
    
    <apex:panelGrid columns="2" width="20">
    
    <apex:chart height="250" width="450" animate="true" data="{!gaugedata}">
    <apex:axis type="Gauge" position="gauge" title="Sum Of TCV"
        minimum="0" maximum="1500000" steps="10"/>
    <apex:gaugeSeries dataField="data1" donut="50" colorSet="#78c953,#ddd"/>
    </apex:chart>

    <apex:pageBlock title="Top 5 Open Deals" >
        <apex:pageBlockSection columns="2" >
        <apex:pageBlockTable value="{!opps}" var="opp">
            <apex:column value="{!opp.Name}"/>
            <apex:column value="{!opp.Total_TCV__c}"/>
        </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>   
   </apex:panelGrid>
</apex:page>


PAGE:
public with sharing class TopFiveOpenDeals {

  List<String> nameList{get;set;}
  List<selectOption> owner_List{get;set;}
  List<User> ownerList{get;set;}

  Public TopFiveOpenDeals()
  { 
         ownerList=new List<User>();
         ownerList=[Select Id, Name From User where isActive=true AND Profile.name='System Administrator'];
         //ownerList=[Select Id, Name From User where isActive=true AND Profile.name='Sales User' AND UserRole.name LIKE '%Sales%'];
         nameList=new List<String>();
         owner_List=new List<selectOption>();
        for(User a:ownerList)
        {
            nameList.add(a.name);
        }
        
        
         }
   public List<SelectOption> getOwner_list()
   {
     for(String obj:nameList)
     {
         owner_List.add(new selectOption(obj,obj));
     }  
   
      return owner_list;
   }
  
  public void UpdateSelectedFirstItem()
    {
   
     }

  public List<Opportunity> getOpps() {
    return [ Select Name,Total_TCV__c 
      from Opportunity 
      //WHERE Total_TCV__c !=NULL AND (CloseDate= THIS_QUARTER OR CloseDate=NEXT_QUARTER)  
      WHERE Total_TCV__c !=NULL
      ORDER BY Total_TCV__c 
      DESC LIMIT 5
    ];
  }
}


Thanks,
Laxman 
Chandra Sekhar CH N VChandra Sekhar CH N V
add a filer bases on record owner . something like ownerid = UserInfo.getUserId() which will limit data based on logged in user.
https://developer.salesforce.com/docs/atlas.en-us.198.0.apexcode.meta/apexcode/apex_methods_system_userinfo.htm#apex_System_UserInfo_getUserId
Salesforce BlitzSalesforce Blitz
Hi Chandra sekhar,

I need to display all users in the filter and change the dashboard view based user selected.

Thanks,
Laxman
 
Chandra Sekhar CH N VChandra Sekhar CH N V
Please clarify what exactly does 'visual dashboard' mean? Have you embeddeda standard dashboard in a VF page or createda whole new custom dashbaord?

1) If its a standard one , i am afraid you cannot control data being displayed based on a custom filter. You can instead make the dashboard as a dynamic dashboard and try displaying it.

2) If its a custom one, you need to build chart separately using <apex:chart> components and manipulate your queries based on a input selection.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_charting_overview_simple_example.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_charting_appearance_bar_charts.htm
 
Salesforce BlitzSalesforce Blitz
Hi chandra sekhar,

I have created a whole new cusom dashboard in visual force using apex:chart apex:axis apex:barseries apex;legend and all....

At top I created a picklist which inclides all users available.

now how do i edit my queries to show dashboard data related to particular user.

consider the below method of my controller:

 public List<Opportunity> getSalesOpps() {
    return [ Select Name,Opportunity.owner.name,Total_TCV__c 
      from Opportunity 
      WHERE Total_TCV__c !=NULL AND (CloseDate= THIS_QUARTER OR CloseDate=NEXT_QUARTER) 
      ORDER BY Total_TCV__c 
      DESC LIMIT 5

    ];    
  }

How should I modify aboveSOQL in method to display data related to user selected above.
Struck here ..would be thankful if sorted out asap

Thanks,
Laxman
Chandra Sekhar CH N VChandra Sekhar CH N V
You have defined 'owner_list' as a LIST, are you selecting single/multiple users in the page?  you need to simply add it in your where clause , something like 
WHERE OwnerId in: <your list>

//OR

WHERE Owner.Name in: <your list>
I would also suggest you to make your property as list<String> (if selecting multiple users) instead of list<Users>

 
Salesforce BlitzSalesforce Blitz
Thanks for helping out.

I will be selecting only one user from picklist.

On selection, the VF page neds to show only data which he has access(Read/write/Edit).

Example:If User A has User B and user C under him in role hierarchy., If i select user A in picklist, I need to display data acceessibeto B and C along with A's data.

Hope i am clear with explanation
Salesforce BlitzSalesforce Blitz
Hi Chandrasekar,

Could you let me know what to modify
 
Chandra Sekhar CH N VChandra Sekhar CH N V
Use something like this - 
//declare property for user 

public User u {get;set;}

//get values for list of users by below method 

public List<SelectOption> getUser() {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = User.Name.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry p : ple){
        options.add(new SelectOption(p.getValue(), p.getValue())); }      
        return options;        
    }

// some code..........

//Later in the query for retrieving opportunities part add filter like 

 [ Select Name,Opportunity.owner.name,Total_TCV__c 
      from Opportunity 
      WHERE Total_TCV__c !=NULL AND (CloseDate= THIS_QUARTER OR CloseDate=NEXT_QUARTER) AND 
      ownerid = :u.id
      ORDER BY Total_TCV__c 
      DESC LIMIT 5]
Salesforce BlitzSalesforce Blitz
Hi ..Thanks for reply,

I need to query records not just based on ownerId...I should query all records which the user has access to even if he is not a owner