Skip to content

Instantly share code, notes, and snippets.

@sfdcale
Created July 16, 2018 18:40
Show Gist options
  • Save sfdcale/835a52683e790204ef3c37e4a5b6d6c0 to your computer and use it in GitHub Desktop.
Save sfdcale/835a52683e790204ef3c37e4a5b6d6c0 to your computer and use it in GitHub Desktop.
Salesforce script to retrieve all Dashboards and their folder names
//First query all DashboardDolder in the org.
Map<Id,Folder> folderMap = new Map<Id,Folder>([SELECT Id,
DeveloperName,
NameSpacePrefix
FROM Folder
WHERE
Type = 'Dashboard']);
//Query all Dashboards in the system.
List<Dashboard> dashboardList = [SELECT FolderId,
DeveloperName,
NameSpacePrefix
FROM Dashboard
USING SCOPE everything];
List<String> DashboardNames = new List<String>();
for(Dashboard DashboardObj: dashboardList){
String DashboardName;
//If a Dashboard is inside the folder then we should get the folder name also.
//If Dashboard is in public folders then we should prefix with "unfiled$public"
//We should also check for Managed package Dashboards. If namspace is not blank then prepend foldername and dashboardname followed
//by '__'
if(folderMap.containsKey(DashboardObj.FolderId)){
if(String.isNotBlank(folderMap.get(DashboardObj.FolderId).NameSpacePrefix)){
DashboardName = folderMap.get(DashboardObj.FolderId).NameSpacePrefix + '__' + folderMap.get(DashboardObj.FolderId).DeveloperName;
}else{
DashboardName = folderMap.get(DashboardObj.FolderId).DeveloperName;
}
if(String.isNotBlank(DashboardObj.NameSpacePrefix)){
DashboardName = DashboardName + '/' + DashboardObj.NameSpacePrefix + '__' + DashboardObj.DeveloperName;
}else{
DashboardName = DashboardName + '/' + DashboardObj.DeveloperName;
}
DashboardNames.add(DashboardName);
}else{
if(String.isNotBlank(DashboardObj.NameSpacePrefix)){
DashboardName = 'unfiled$public/' + DashboardObj.NameSpacePrefix + '__' + DashboardObj.DeveloperName;
}else{
DashboardName = 'unfiled$public/' + DashboardObj.DeveloperName;
}
DashboardNames.add(DashboardName);
}
}
System.debug(JSON.serialize(DashboardNames));
//Once all Dashboard names are printed, we can prepare package file and retrieve Dashboards from Org.
@sfdcale
Copy link
Author

sfdcale commented Jul 16, 2018

I still use mavensmate, though it is officially not supported anymore, and it does not retrieve all the dashboards present in the org. So I use the above code to print dashboard name, including its folder name, and prepare a package.xml file and then use in ANT migration tool to retrieve all dashboards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment