Managing reports across different folders in Salesforce can become a tedious task, especially if you need to consolidate them for better organization.
This guide will walk you through how to move multiple reports from various folders to a single destination folder using SFDX (Salesforce CLI) and metadata API.
Step 1: Query Reports Based on Criteria
First, you need to identify the reports you wish to move. For example, if you want to target reports that haven’t been used in the last six months, you can run the following SOQL query:
SELECT Id, FolderName, DeveloperName, Name FROM Report WHERE LastRunDate != LAST_N_MONTHS:6
This query will retrieve the Id
, FolderName
, DeveloperName
, and Name
fields of reports that haven’t been run in the last six months. The FolderName
here refers to the name of the folder the report is currently stored in.
Step 2: Retrieve Folder API Names
Once you have the FolderName
, you need to get the Folder API Name
associated with each folder. This is essential because the package.xml
file requires the Folder API Name
, not just the folder’s display name.
You can use the following SOQL query to fetch the folder API names:
SELECT Id, DeveloperName, Name FROM Folder WHERE Name IN ('foldername1', 'foldername2', 'foldername3')
Export this data to Excel alongside the initial report query, ensuring you have both the Folder API Name
(from the DeveloperName
field) and the Report DeveloperName
from the first query.
Step 3: Organize the Report Information
In Excel, merge the Folder API Name
and DeveloperName
(from the reports query) to create the correct report path format using this formula:
=C1 & "/" & D1
Here, C1 is the Folder API Name
, and D1 is the DeveloperName
of the report. Copy the results into a new column (e.g. column F).
Step 4: Set Up the Project for Retrieval
Ensure that your SFDX project is created using the “Create Project with Manifest” option and authorize it with an org. Once your project is ready, navigate to the manifest
folder and open the package.xml
file.
Remove any existing <types>
tags and replace them with:
<types>
<members>*</members> <!-- This will retrieve all reports from the org -->
<name>Report</name>
</types>
Step 5: Modify the Package.xml for Targeted Reports
Now, we only want to retrieve the specific reports from your SOQL query. To do this, go back to the Excel sheet and use the following formula in a new column:
= “<members>” & F1 & “</members>”
This will give you the report paths in the format Salesforce needs, for example:
<types>
<members>folderAPIName/developername</members>
<name>Report</name>
</types>
Copy this data and replace the existing <members>
tag in your package.xml
file with the new values.
Step 6: Retrieve the Reports
Use the following command to retrieve the reports from your Salesforce org:
sfdx force:mdapi:retrieve -r ./mdapi_result -k manifest/package.xml -u YourOrgAlias
This will create a zip file in the mdapi_result
folder.
Step 7: Organize Reports in the New Folder
Log into your Salesforce org and create a new folder for the reports.
Unzip the file retrieved earlier, and you’ll see a reports
folder. Inside the reports
folder, create a new folder and move all the reports from their current folders into this new folder. Delete the old, now-empty folders.
Step 8: Update the Folder Name in Package.xml
Open the package.xml
file located in the mdapi_result
folder, and replace the old folder paths with the newly created folder API name.
After making the changes, save the file.
Step 9: Deploy the Reports to the New Folder
Ensure that the unpackaged
folder inside mdapi_result
contains both the reports folder and the updated package.xml
. Once everything is in place, run the following command to deploy the reports to the new folder in your Salesforce org:
sfdx force:mdapi:deploy -d mdapi_result/unpackaged -u YourOrgAlias -w -1
Step 10: Verify the Changes
Once the deployment is complete, verify that the reports have been moved to the new folder by running an SOQL query or checking manually within the org.
Summary
Managing Salesforce reports across multiple folders can be challenging, especially when aiming for better organization. However, by following the steps outlined above, you can streamline your report management and ensure things run smoothly.
What are your thoughts? Feel free to share them in the comments below!