Sub topic 13
Auditing dashboards with SOQL
This chapter assumes you can write basic SOQL (Salesforce Object Query Language) queries, know what a running user is, and understand that dynamic dashboards run as the logged-in user. You should also have read the chapters on folders and edition limits. Here you will query dashboard definitions to see who each dashboard runs as, count dashboards by running-user type, and spot governance problems.
Why audit with SOQL
The Dashboards tab lets you open one dashboard at a time. That is slow when an org has hundreds of them. Questions such as "Which dashboards run as a single person?" or "How close are we to the dynamic dashboard limit?" need a list, not a click-through.
Dashboards are stored as records you can query. The Dashboard object holds the definition: title, folder, running-user setting, and a few other fields. SOQL lets you filter and count these records the same way you would for Accounts.
Auditing matters for three reasons:
- Data exposure. A dashboard with a specified running user shows every viewer that user's data, even data the viewer could not open in a report.
- Limits. Editions cap the number of dynamic dashboards. You need a count before you build more.
- Ownership. If the running user leaves the company and is deactivated, the dashboard can stop refreshing or show errors.
The Dashboard object fields you need
These fields cover most audits.
| Field | What it holds |
|---|---|
| `Id` | The record ID of the dashboard |
| `Title` | The name shown to users |
| `DeveloperName` | The unique API name |
| `FolderName` | The name of the folder that contains it |
| `Type` | The running-user setting: `SpecifiedUser`, `LoggedInUser`, or `MyTeamUser` |
| `RunningUserId` | The ID of the running user. For a dynamic dashboard this is not a fixed person. |
Check your own org's field list before relying on it, because available fields and values can vary by API version. You can run DESCRIBE-style checks in Developer Console or with sf sobject describe --sobject Dashboard in Salesforce CLI.
The Type values map to the running-user options in the dashboard editor:
| Type value | Editor option | Viewers see |
|---|---|---|
| `SpecifiedUser` | Run as specified user | The data of one chosen user, for every viewer |
| `LoggedInUser` | Run as logged-in user | Each viewer's own data (dynamic dashboard) |
| `MyTeamUser` | Run as me or my team | Data scoped by the role hierarchy |
Where to run the queries
You have several options. Pick the one that fits your habit.
- Developer Console. Open Query Editor, paste the SOQL, and run it. Good for quick checks.
- Salesforce CLI. Run
sf data query --query "..."to get results in a terminal or script. Good for repeatable audits. - Workbench or a REST client. Useful if you already use them for data exploration.
Your user needs access to dashboards to see them. A query returns only the dashboards your user is allowed to see. An admin with broad view permissions gets a more complete list than a standard user.
Listing dynamic dashboards
Start with the question "Which dashboards are dynamic?" Filter on Type.
SELECT Id, Title, DeveloperName, FolderName, Type, RunningUserId
FROM Dashboard
WHERE Type = 'LoggedInUser'Each row is a dashboard that runs as the logged-in user. Review the FolderName column. A dynamic dashboard in a folder that every user can open is the normal design. A dynamic dashboard in a folder shared with only one team may indicate that someone chose the setting without needing it.
You can invert the filter to find the dashboards that carry the most exposure risk:
SELECT Id, Title, FolderName, RunningUserId
FROM Dashboard
WHERE Type = 'SpecifiedUser'For each row, ask whether the running user should see everything the dashboard shows, and whether every viewer of the folder should see it too. If the answer to either is no, the dashboard is a candidate for conversion to a dynamic dashboard or for moving to a narrower folder.
Counting dashboards by running-user type
Aggregate queries give you the totals. Use GROUP BY with COUNT.
SELECT Type, COUNT(Id) total
FROM Dashboard
GROUP BY TypeThe result has one row per type, for example:
| Type | total |
|---|---|
| SpecifiedUser | 142 |
| LoggedInUser | 9 |
| MyTeamUser | 4 |
The numbers above are sample values. Compare the LoggedInUser count with the limit for your edition. Dynamic dashboard allowances differ by edition and can change, so look up the current figure in Salesforce Help before making decisions. If the count is near the limit, you must retire unused dynamic dashboards or redesign before you create new ones.
How the audit connects to the data model
The dashboard definition is only half of the picture. Components pull data from reports, so a full audit also has to look at the report each component reads. The query below shows the kind of grouped totals that an open-pipeline summary report grouped by Stage gives a chart component:
SELECT StageName, SUM(Amount) total
FROM Opportunity
WHERE IsClosed = false
GROUP BY StageNameYou can use this query as a cross-check. If the dashboard bar for Negotiation shows a different total than the query, the cause is usually one of these:
- The running user cannot see some records, so the dashboard total is lower than your admin total.
- The source report has extra filters, such as a date range or an owner filter.
- The dashboard has a dashboard filter applied.
- The dashboard data is stale because it has not been refreshed since the records changed.
Running the query as an administrator and then comparing it with the dashboard separates visibility problems from filter problems. This is the same technique you use in the troubleshooting chapter.
Finding governance problems
With the basic queries in hand, you can ask more pointed questions.
Dashboards run by one person
Group by running user to see how many dashboards depend on each person:
SELECT RunningUserId, COUNT(Id) total
FROM Dashboard
WHERE Type = 'SpecifiedUser'
GROUP BY RunningUserId
ORDER BY COUNT(Id) DESCA single user who runs dozens of dashboards is a risk. If that account is deactivated or loses access to a report folder, many dashboards break at once. Many orgs use a dedicated integration-style user with a stable role instead of a named employee.
Dashboards by folder
SELECT FolderName, Type, COUNT(Id) total
FROM Dashboard
GROUP BY FolderName, Type
ORDER BY FolderNameThis shows where each kind of dashboard lives. A folder that mixes SpecifiedUser and LoggedInUser dashboards is not wrong, but it deserves a look. Viewers of the same folder can see very different data depending on the dashboard they open.
Inactive running users
Join the running user to the User object by using a second query, because RunningUserId is a plain ID in many audit contexts. First collect the IDs, then check them:
SELECT Id, Name, IsActive
FROM User
WHERE IsActive = false
AND Id IN (SELECT RunningUserId FROM Dashboard)Semi-join subqueries like this one depend on the field being a relationship that SOQL accepts in an IN clause. If your org returns an error, run the dashboard query, copy the IDs into a second query, and compare by hand or in a script.
Reading the Metadata API view
SOQL tells you the settings. The Metadata API gives the full definition, including each component and the report it points to. A dashboard retrieved through Metadata API is an XML file with elements such as dashboardType and runningUser. The dashboardType element holds the same kind of value that Type reports in SOQL.
A retrieval workflow with Salesforce CLI looks like this:
sf project retrieve start --metadata "Dashboard:SalesFolder/PipelineByStage"Replace SalesFolder/PipelineByStage with your own folder and dashboard API names. Open the downloaded file and check three things: the running-user element, the report reference of each component, and any filter definitions. Keeping these files in version control gives you a history of when a dashboard changed from specified to dynamic.
Common mistakes
- Counting only your own view. A non-admin query misses private folders. Always audit as an administrator.
- Treating
RunningUserIdas meaningful for dynamic dashboards. For a dynamic dashboard, the effective user is the viewer. Do not read the ID as "who sees the data." - Assuming the limit is fixed. Limits depend on edition and can change between releases. Confirm in current documentation.
- Auditing dashboards but not reports. A dashboard is only as safe as the reports and folders behind it. A viewer with dashboard access still needs the report to be accessible to the running user.
- Running one audit and stopping. Dashboards are created constantly. Schedule the query, for example in a weekly CLI job, and compare counts over time.
Putting it together
A practical audit takes about fifteen minutes:
- Run the count by
Typeand record the totals. - Compare the
LoggedInUsercount with your edition limit. - List
SpecifiedUserdashboards and review each one against its folder audience. - Check that no running user is inactive.
- Retrieve the metadata for any dashboard you plan to change, and keep it in version control.
Exercise
Your org has 150 dashboards. A count by Type returns 140 SpecifiedUser and 10 LoggedInUser. Your edition allows 10 dynamic dashboards, and a team asks for another one. Which query would you run to decide what to retire, and what would you check on each result?
Show answer
Run SELECT Id, Title, DeveloperName, FolderName, Type, RunningUserId FROM Dashboard WHERE Type = 'LoggedInUser' to list the ten existing dynamic dashboards. For each one, check who uses it and whether its folder is still shared with an audience that needs it. Retire or convert an unused one (or one that does not need per-viewer data) before creating the new dynamic dashboard. Confirm the limit for your edition in current Salesforce documentation first.
Exercise
A chart on a dashboard shows 1.2M for the Negotiation stage, but your admin SOQL query on Opportunity grouped by StageName shows 1.9M. Name two likely causes.
Show answer
The dashboard's running user may not see all the records that your administrator account sees, so the total is lower. The source report or a dashboard filter may also exclude records, for example through a date range or owner filter. A stale refresh is a third possible cause.
Exercise
Why can a SOQL audit run by a standard user give counts that are too low?
Show answer
SOQL on Dashboard returns only the dashboards the querying user is allowed to see. Dashboards in private folders or folders not shared with that user are left out, so counts are incomplete. Run the audit as an administrator.