# Salesforce access review: SOQL pack
# From the Clientell AI Access Review Kit
# https://www.getclientell.com/resources/salesforce-access-review-guide
#
# Last checked: 28 Oct 2026. Query numbers match the guide and the checklist PDF.
#
# How to run:
#   Developer Console > Query Editor, or Workbench, or the Salesforce CLI:
#   sf data query -o <your-org-alias> -q "<query on one line>" --result-format csv > result.csv
# Leave the Tooling API option off.
# Change 'Account' and the 90-day window to fit your scope.
# Every query here only reads. None of them changes anything in your org.
# Save each result with the date, the org ID and who ran it (see access-review-evidence-log.csv).


## A. Access paths: how access reaches a user

# A.1 How each Modify All Data grant arrives (Profile, Regular permission set or Group)
SELECT PermissionSet.Type, COUNT(Id) c FROM PermissionSetAssignment WHERE PermissionSet.PermissionsModifyAllData = true AND Assignee.IsActive = true GROUP BY PermissionSet.Type

# A.2 Muting permission sets (open each one in its permission set group to see what it removes)
SELECT Id, DeveloperName, MasterLabel FROM MutingPermissionSet

# A.3 Org-wide defaults for your key objects
SELECT QualifiedApiName, InternalSharingModel, ExternalSharingModel FROM EntityDefinition WHERE QualifiedApiName IN ('Account', 'Contact', 'Opportunity', 'Case')

# A.4 Why Account records are shared (owner, rule, manual share, team and so on)
SELECT RowCause, COUNT(Id) c FROM AccountShare GROUP BY RowCause

# A.5 Active guest users (Experience Cloud and sites)
SELECT Name, Profile.Name FROM User WHERE IsActive = true AND UserType = 'Guest'


## Step 1. Scope

# 1.1 Active users, the starting population
SELECT COUNT() FROM User WHERE IsActive = true


## Step 2. Users and how they get access

# 2.1 Active users by profile
SELECT Profile.Name, COUNT(Id) c FROM User WHERE IsActive = true GROUP BY Profile.Name

# 2.2 Active users by user type
SELECT UserType, COUNT(Id) c FROM User WHERE IsActive = true GROUP BY UserType

# 2.3 Active users per permission set (not counting profile-owned sets)
SELECT PermissionSet.Name, COUNT(Id) c FROM PermissionSetAssignment WHERE Assignee.IsActive = true AND PermissionSet.IsOwnedByProfile = false GROUP BY PermissionSet.Name


## Step 3. Who can see everything

# 3.1 Permission sets and profiles that carry View All Data
SELECT Id, Name, IsOwnedByProfile, PermissionsViewAllData FROM PermissionSet WHERE PermissionsViewAllData = true

# 3.2 The people behind it
SELECT Assignee.Name, Assignee.Profile.Name, PermissionSet.Name, PermissionSet.IsOwnedByProfile FROM PermissionSetAssignment WHERE PermissionSet.PermissionsViewAllData = true AND Assignee.IsActive = true

# 3.3 How many different people
SELECT COUNT_DISTINCT(AssigneeId) c FROM PermissionSetAssignment WHERE PermissionSet.PermissionsViewAllData = true AND Assignee.IsActive = true

# 3.4 View All Records on one object, from profiles and permission sets
SELECT Parent.Name, Parent.IsOwnedByProfile, Parent.PermissionsViewAllData, SobjectType FROM ObjectPermissions WHERE SobjectType = 'Account' AND PermissionsViewAllRecords = true

# 3.5 People who hold a group that contains a set with View All Data or Modify All Data (looks inside the group)
SELECT Assignee.Name, PermissionSetGroup.DeveloperName FROM PermissionSetAssignment WHERE PermissionSetGroupId IN (SELECT PermissionSetGroupId FROM PermissionSetGroupComponent WHERE PermissionSet.PermissionsModifyAllData = true OR PermissionSet.PermissionsViewAllData = true) AND Assignee.IsActive = true

# 3.6 The same filter on the group's own combined row. Compare with 3.5.
#     Someone in 3.5 but not in 3.6: open the group in Setup and find out why before you record a cause.
SELECT Assignee.Name, PermissionSetGroup.DeveloperName FROM PermissionSetAssignment WHERE PermissionSetGroupId != null AND (PermissionSet.PermissionsModifyAllData = true OR PermissionSet.PermissionsViewAllData = true) AND Assignee.IsActive = true


## Step 4. Who can change or delete everything

# 4.1 Every permission set (and profile) that carries a high-power permission
SELECT Id, Name, IsOwnedByProfile, PermissionsModifyAllData, PermissionsViewAllData, PermissionsBulkApiHardDelete, PermissionsDataExport, PermissionsAuthorApex, PermissionsManageUsers, PermissionsCustomizeApplication FROM PermissionSet WHERE PermissionsModifyAllData = true OR PermissionsViewAllData = true OR PermissionsBulkApiHardDelete = true OR PermissionsDataExport = true OR PermissionsAuthorApex = true OR PermissionsManageUsers = true OR PermissionsCustomizeApplication = true

# 4.2 Who holds Modify All Data, and when they last logged in
#     PermissionSet includes the permission set each profile owns, so this one query covers profiles too.
SELECT Assignee.Name, Assignee.Profile.Name, PermissionSet.Name, PermissionSet.IsOwnedByProfile, Assignee.LastLoginDate FROM PermissionSetAssignment WHERE PermissionSet.PermissionsModifyAllData = true AND Assignee.IsActive = true

# 4.3 How many different people hold Modify All Data
SELECT COUNT_DISTINCT(AssigneeId) c FROM PermissionSetAssignment WHERE PermissionSet.PermissionsModifyAllData = true AND Assignee.IsActive = true

# 4.4 Who can delete permanently (Bulk API Hard Delete)
SELECT Assignee.Name, Assignee.Profile.Name, PermissionSet.Name, PermissionSet.IsOwnedByProfile FROM PermissionSetAssignment WHERE PermissionSet.PermissionsBulkApiHardDelete = true AND Assignee.IsActive = true

# 4.5 Modify All Records on one object, from profiles and permission sets
#     Parent.PermissionsModifyAllData = true rows get it from Modify All Data (already in 4.2).
#     The rows with false are object-level grants you must review here.
SELECT Parent.Name, Parent.IsOwnedByProfile, Parent.PermissionsModifyAllData, SobjectType FROM ObjectPermissions WHERE SobjectType = 'Account' AND PermissionsModifyAllRecords = true

# 4.6 Modify All Records, by object, not explained by Modify All Data (profiles and permission sets)
SELECT SobjectType, COUNT(Id) c FROM ObjectPermissions WHERE PermissionsModifyAllRecords = true AND Parent.PermissionsModifyAllData = false GROUP BY SobjectType


## Step 5. Who can export

# 5.1 Who can export reports
SELECT Assignee.Name, Assignee.Profile.Name, PermissionSet.Name, PermissionSet.IsOwnedByProfile FROM PermissionSetAssignment WHERE PermissionSet.PermissionsExportReport = true AND Assignee.IsActive = true

# 5.2 Who can run the Weekly Data Export
SELECT Assignee.Name, Assignee.Profile.Name, PermissionSet.Name, PermissionSet.IsOwnedByProfile FROM PermissionSetAssignment WHERE PermissionSet.PermissionsDataExport = true AND Assignee.IsActive = true

# 5.3 API Enabled, counted by profile
SELECT Assignee.Profile.Name, COUNT_DISTINCT(AssigneeId) c FROM PermissionSetAssignment WHERE PermissionSet.PermissionsApiEnabled = true AND Assignee.IsActive = true GROUP BY Assignee.Profile.Name


## Step 6. Stale and integration users

# 6.1 Active standard users with no login in 90 days, or never
SELECT Name, Username, Profile.Name, LastLoginDate FROM User WHERE IsActive = true AND UserType = 'Standard' AND (LastLoginDate < LAST_N_DAYS:90 OR LastLoginDate = null) ORDER BY LastLoginDate

# 6.2 How many have never logged in
SELECT COUNT() FROM User WHERE IsActive = true AND UserType = 'Standard' AND LastLoginDate = null

# 6.3 Modify All Data holders who have not logged in for 90 days
SELECT Assignee.Name, Assignee.LastLoginDate, PermissionSet.Name FROM PermissionSetAssignment WHERE PermissionSet.PermissionsModifyAllData = true AND Assignee.IsActive = true AND (Assignee.LastLoginDate < LAST_N_DAYS:90 OR Assignee.LastLoginDate = null)

# 6.4 Integration users: active users on a profile limited to the API
SELECT Name, Profile.Name, LastLoginDate FROM User WHERE IsActive = true AND UserType = 'Standard' AND Profile.PermissionsApiUserOnly = true

# 6.5 Which profiles are limited to the API
SELECT Id, Name FROM Profile WHERE PermissionsApiUserOnly = true


## Step 7. The evidence trail
#  Setup Audit Trail keeps 180 days. Save a copy before it ages out.

# 7.1 How many setup changes in the last 30 days (use COUNT(), not COUNT(Id), on this object)
SELECT COUNT() FROM SetupAuditTrail WHERE CreatedDate = LAST_N_DAYS:30

# 7.2 Permission set changes in the last 30 days
SELECT CreatedDate, CreatedBy.Name, Action, Section, Display FROM SetupAuditTrail WHERE CreatedDate = LAST_N_DAYS:30 AND Action LIKE 'permSet%' ORDER BY CreatedDate DESC

# 7.3 Profile changes in the last 90 days
SELECT CreatedDate, CreatedBy.Name, Action, Section, Display FROM SetupAuditTrail WHERE CreatedDate = LAST_N_DAYS:90 AND (Action LIKE 'profile%' OR Action = 'deletedprofile') ORDER BY CreatedDate DESC

# 7.4 Users created and deactivated in the last 90 days
SELECT CreatedDate, CreatedBy.Name, Action, Section, Display FROM SetupAuditTrail WHERE CreatedDate = LAST_N_DAYS:90 AND Action IN ('createduser', 'deactivateduser') ORDER BY CreatedDate DESC

# 7.5 Logins in the last 30 days, by type
SELECT LoginType, COUNT(Id) c FROM LoginHistory WHERE LoginTime = LAST_N_DAYS:30 GROUP BY LoginType

# 7.6 The latest 20 logins, with status
SELECT UserId, LoginTime, LoginType, Status, SourceIp FROM LoginHistory WHERE LoginTime = LAST_N_DAYS:7 ORDER BY LoginTime DESC LIMIT 20


## Before you remove access (undo file)

# U.1 Export the assignments you plan to remove. Keep AssigneeId and PermissionSetId.
#     To restore, insert the same rows into PermissionSetAssignment with Data Loader.
#     For a row that came from a group, insert AssigneeId with PermissionSetGroupId instead.
#     Change the permission filter to match what you are removing.
SELECT Id, AssigneeId, Assignee.Username, PermissionSetId, PermissionSet.Name, PermissionSetGroupId FROM PermissionSetAssignment WHERE Assignee.IsActive = true AND PermissionSet.IsOwnedByProfile = false AND PermissionSet.PermissionsModifyAllData = true
