Created
July 16, 2018 18:40
-
-
Save sfdcale/835a52683e790204ef3c37e4a5b6d6c0 to your computer and use it in GitHub Desktop.
Salesforce script to retrieve all Dashboards and their folder names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.