Salesforce how to give access to list view to profile or role level users
List View access to users in Salesforce using Profiles, Permission Sets, Roles, and Apex code (Metadata API).
✅ Salesforce — How to Give Access to a List View to Profile or Role-Level Users (with Code)
🚀 Understanding List View Access in Salesforce
In Salesforce, List Views are stored as ListView metadata. They can be restricted using:
✔ 1. “Who can see this list view” Options
When creating a List View, you can choose:
- Only I can see this list view
- All users can see this list view
- Share list view with groups/roles/roles & subordinates
Salesforce does not provide direct Profile-based sharing from UI.
But you can share List Views with:
- Public Groups
- Roles
- Roles and Subordinates
- All Internal Users
- All Partner Users
If you want Profile-based access → create a Public Group with that Profile’s users.
⭐ METHOD 1 — Add Access Using UI (Role / Public Group)
Steps:
- Go to Object → List Views
- Edit the List View
- In “Who sees this list view?”
- Select Share with
- Add:
- Role
- Role and Subordinates
- Public Group (can include a Profile using auto-membership rule)
⭐ METHOD 2 — Give Access via Metadata API (Apex Code)
⚠ Apex cannot directly update ListViews.
But you can call Metadata API or Tooling API to update ListView sharing.
Below is a working example using Apex + Metadata API.
✔ Apex Code to Grant Access to a List View (Metadata API)
1. Create a Remote Site Setting
Setup → Security → Remote Site Settings
Add: https://yourInstance.salesforce.com
2. Apex Code: Update List View Sharing
public class ListViewSharingService {
public static void shareListViewWithGroup() {
MetadataService.MetadataPort service = createService();
// Retrieve existing List View
MetadataService.ListView listView =
(MetadataService.ListView) service.readMetadata(
'ListView',
new String[] { 'Account.My_Custom_List_View' }
).getRecords()[0];
// Add group/role access
MetadataService.SharedTo st = new MetadataService.SharedTo();
st.group = new String[] { 'Group:My_Public_Group' };
st.role = new String[] { 'Role:Sales_Executive' };
listView.sharedTo = st;
// Update Metadata
MetadataService.AsyncResult[] results =
service.updateMetadata(new MetadataService.Metadata[] { listView });
System.debug('List View Sharing Updated: ' + results[0].id);
}
private static MetadataService.MetadataPort createService() {
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
return service;
}
}
JavaScript✔ How This Code Works
| Part | Purpose |
|---|---|
readMetadata() | Pulls the existing List View definition |
sharedTo | Defines who can access the list view |
updateMetadata() | Saves new visibility settings |
⭐ METHOD 3 — Assign Access Using a Public Group + Profile
Because Salesforce does not allow direct Profile-to-listview access, do this:
Step 1: Create Public Group
- Add all users of that Profile to this group
OR - Use Auto-Add Condition: "Users with Profile X" (Enterprise+)
Step 2: Share List View with the Public Group
(Through UI or Metadata API)
This is the recommended Salesforce standard approach.
⭐ METHOD 4 — Tooling API Query to Find List View
List<ListView> views = [
SELECT Id, Name, SobjectType, DeveloperName
FROM ListView
WHERE SobjectType = 'Account'
];
JavaScriptUseful to get DeveloperName for Metadata API call.
📝 Example: Create a List View Programmatically and Share It
MetadataService.ListView lv = new MetadataService.ListView();
lv.fullName = 'Account.My_New_List_View';
lv.label = 'My New List View';
lv.filterScope = 'Everything';
lv.query = 'SELECT Id, Name FROM Account LIMIT 50';
MetadataService.SharedTo st = new MetadataService.SharedTo();
st.role = new String[]{'Role:SalesManager'};
lv.sharedTo = st;
service.createMetadata(new MetadataService.Metadata[]{lv});
JavaScriptRelated Posts

How to Automatically create a follow-up Task when a Lead is converted

How You need to update a related child record whenever a parent record’s status changes, but only if the status is “Closed Won.” How would you design this in Apex?
