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 

How to change dashboard running user in salesforce programatically

I need to change the Dashboard Running User through my apex class. 
How do I need to achieve this.

I tried below,but the RunningUserId is not writable, So this is not working.Any other way please.

      List<Dashboard> dbs=[select id, RunningUserId, Title from Dashboard WHERE id='01Z90000000AL9j'];
      dbs[0].RunningUserId = UserInfo.getUserId() ;
      Update dbs[0];
 
Chandra Sekhar CH N VChandra Sekhar CH N V
Don't think its possible through apex, But if you want that dashboard should run always in present logged in user then you simple follow the below - 
Go To Dashboard > Edit > View Dashboard As > Select "Run as Logged in User" > Save.
ManojjenaManojjena

HI Laxman .

I don't think it is possible in apex to update dashboard object and fields .

If you will open the particular object/field  property in workbench it is not updateable from apex .
You can achive this with metadata API .

User-added image

Let me know if it helps !!
Thanks
Manoj
Salesforce BlitzSalesforce Blitz
Hi Chandrasekar n Manoj,

I need to display dashboard in a VF page. And add a filter/picklist to it which displays all users.
If I change the user the Dashboard should show data depending on user security/access.

Is there any code/info which could help me out.

It would be really helpful.

Thanks.
Laxman
Chandra Sekhar CH N VChandra Sekhar CH N V
Two things here - 

1) In case you need to display your filter and dashboard on the same page - You may need to customize your chart as well since there is no direct way of controlling data displayed in it. (https://developer.salesforce.com/blogs/tech-pubs/2011/09/introducing-visualforce-charting.html)

2) If your requirement is like you will select a user from the picklist and onClick of a button, it will direct to a standard report with filter being applied from VF page) - you can acheive by using getParameters() of PageReference class and get values from picklist option selected.(few changes may be required here as you want to display data in a dashboard only and not in a report). Example - 
User-added image

Let me know your exact requirement based on the above points.

 
Salesforce BlitzSalesforce Blitz
HI,

Thanks for replying.

I need the filter and dashboard on same VF Page:
The dashboard data should change based on user selected above.

Could you share any code if related.
I have written a clas and controller as below:(But is is hard coded)


CONTROLLER:

public with sharing class MyController
 {
  List<String> nameList{get;set;}
  List<selectOption> owner_List{get;set;}
  List<User> ownerList{get;set;}

    public MyController()
    {
         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 string getDashboardHtml()
    {
        String abc='/01Z6100000045Fq';
        PageReference dbPage = new PageReference(abc);
        Blob pageBlob = dbPage.getContent();
        return pageBlob.toString();
    }
    public void UpdateSelectedFirstItem()
    {
    
    }
    
   }




VISUALFORCE PAGE:

<apex:page controller="MyController" showheader="false" sidebar="false" id="page" >
    <apex:form >
        <div id="mainArea">
        <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>
        </div>
        <script>
            document.getElementById('mainArea').innerHTML = '{!dashboardHtml}';
        </script>
    </apex:form>
</apex:page>


Thanks,
Laxman