Skip to content

Instantly share code, notes, and snippets.

@jongpie
Last active November 30, 2023 12:37
Show Gist options
  • Save jongpie/1cac262a5102736df2a86bbe2704a8b0 to your computer and use it in GitHub Desktop.
Save jongpie/1cac262a5102736df2a86bbe2704a8b0 to your computer and use it in GitHub Desktop.
Generates sfdx commands to retrieve all Salesforce Reports & Dashboards (since wildcards are not supported)
// Get the reports & dashboards
List<Report> reports = [SELECT Id, DeveloperName, FolderName FROM Report ORDER BY FolderName, DeveloperName];
Set<String> folderNames = new Set<String>();
for (Report report : reports) {
folderNames.add(report.FolderName);
}
List<Dashboard> dashboards = [SELECT Id, DeveloperName, FolderName FROM Dashboard ORDER BY FolderName, DeveloperName];
for (Dashboard dashboard : dashboards) {
folderNames.add(dashboard.FolderName);
}
// Get the folder dev names - this assumes the Folder.Name (display name) is unique in the org
Map<String, String> folderDisplayNameToDevName = new Map<String, String>();
for (Folder folder : [SELECT Id, DeveloperName, Name FROM Folder WHERE Name IN :folderNames]) {
folderDisplayNameToDevName.put(folder.Name, folder.DeveloperName);
}
// Build the final list of reports
List<String> qualifiedMetadataNames = new List<String>();
for (Report report : reports) {
String folderDevName = folderDisplayNameToDevName.get(report.FolderName);
qualifiedMetadataNames.add('Report:' + folderDevName + '/' + report.DeveloperName);
}
for (Dashboard dashboard : dashboards) {
String folderDevName = folderDisplayNameToDevName.get(dashboard.FolderName);
qualifiedMetadataNames.add('Dashboard:' + folderDevName + '/' + dashboard.DeveloperName);
}
// Generate the sfdx commands
Integer powershellCommandLengthLimit = 7000; // Apparently, powershell can't run commands that are longer than 8,191 (using 7,000 for some buffer)
List<String> sfdxCommands = new List<String>();
final String baseSFDXRetrieveCommand = 'sfdx force:source:retrieve --metadata ';
String currentSFDXRetrieveCommand = baseSFDXRetrieveCommand;
for (String qualifiedMetadataName : qualifiedMetadataNames) {
// If adding the report name would cause the string to exceed PowerShell's limit, then instead start a new string
if ( (currentSFDXRetrieveCommand + ',' + qualifiedMetadataName).length() >= powershellCommandLengthLimit) {
sfdxCommands.add(currentSFDXRetrieveCommand);
currentSFDXRetrieveCommand = baseSFDXRetrieveCommand;
}
String delimiter = currentSFDXRetrieveCommand == baseSFDXRetrieveCommand ? '' : ',';
currentSFDXRetrieveCommand += delimiter + qualifiedMetadataName;
}
sfdxCommands.add(currentSFDXRetrieveCommand);
// Finally, output the list of sfdx commands - these can then be copy/pasted into a terminal to run
String commandsSummaryInfo = 'Number of SFDX commands to run==' + sfdxCommands.size();
System.debug('\n\n' + commandsSummaryInfo + '\n\n' + String.join(sfdxCommands, '\n\n') + '\n\n\n\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment