Salesforce creating permission set and giving access to specific users of a menu tab
Here’s a step-by-step guide with Apex and Setup-based code to create a Permission Set in Salesforce and assign it to specific users to grant access to a custom app tab (menu tab).
🧩 Objective
You want to:
- Create a Permission Set programmatically (using Apex).
- Give access to a specific menu tab (Custom Tab or Standard Tab).
- Assign that Permission Set to selected users automatically.
⚙️ Step 1: Understand Key Salesforce Objects
In Salesforce, Permission Sets and their access rights are stored in:
- PermissionSet – metadata about the permission set itself.
- ObjectPermissions, FieldPermissions, TabSetting – specific access configurations.
- PermissionSetAssignment – links a Permission Set to a User.
🧱 Step 2: Create a Permission Set (Apex Code)
You can create a permission set directly from Developer Console, Anonymous Apex, or Apex Class.
// Step 1: Create Permission Set
PermissionSet newPermSet = new PermissionSet(
Name = 'Custom_Menu_Tab_Access',
Label = 'Custom Menu Tab Access',
Description = 'Gives access to specific menu tab for selected users',
LicenseId = null, // Use null for org-wide
IsCustom = true
);
insert newPermSet;
System.debug('Permission Set created: ' + newPermSet.Id);
JavaScript🧾 Step 3: Grant Access to a Specific Menu Tab
Each tab visibility is controlled by the object PermissionSetTabSetting.
You can add a record to make a specific tab Visible or Hidden in the Permission Set.
Example: Grant Access to a Custom Tab
Suppose your custom tab’s developer name is Custom_Feedback_Tab.
// Step 2: Give access to the specific custom tab
PermissionSetTabSetting tabAccess = new PermissionSetTabSetting(
ParentId = newPermSet.Id, // Permission Set created above
Tab = 'Custom_Feedback_Tab', // API name of the custom tab
Visibility = 'Visible' // Other options: Hidden, None
);
insert tabAccess;
System.debug('Tab access granted for tab: ' + tabAccess.Tab);
JavaScript✅ Note: You can find your Tab API name by navigating to:
Setup → Tabs → Custom Tabs → API Name column.
👥 Step 4: Assign Permission Set to Specific Users
Now, let’s assign this new Permission Set to one or more users.
You can query users based on a specific profile, role, or criteria.
Example: Assign to a List of Specific Users
// Step 3: Assign Permission Set to Users
List<User> targetUsers = [
SELECT Id, Name FROM User
WHERE Profile.Name = 'Sales User' // Example condition
LIMIT 3
];
List<PermissionSetAssignment> assignments = new List<PermissionSetAssignment>();
for (User u : targetUsers) {
assignments.add(new PermissionSetAssignment(
AssigneeId = u.Id,
PermissionSetId = newPermSet.Id
));
}
insert assignments;
System.debug('Permission Set assigned to ' + assignments.size() + ' users.');
JavaScript🧪 Step 5: Verify in Salesforce Setup
After running the above Apex code:
- Go to Setup → Permission Sets → Custom Menu Tab Access.
- Under App Permissions → Tab Settings, verify your tab is marked Visible.
- Go to one of the assigned user’s records → Permission Set Assignments → confirm they have the new permission set.
- Log in as that user — they should now see the menu tab.
⚡ Full Combined Code Example
// COMBINED SCRIPT: Create Permission Set + Grant Tab Access + Assign to Users
PermissionSet newPermSet = new PermissionSet(
Name = 'Custom_Menu_Tab_Access',
Label = 'Custom Menu Tab Access',
Description = 'Provides visibility to Custom Menu Tab for select users',
IsCustom = true
);
insert newPermSet;
// Add tab visibility
PermissionSetTabSetting tabAccess = new PermissionSetTabSetting(
ParentId = newPermSet.Id,
Tab = 'Custom_Feedback_Tab', // API Name of the tab
Visibility = 'Visible'
);
insert tabAccess;
// Fetch target users
List<User> targetUsers = [
SELECT Id, Name FROM User
WHERE Email LIKE '%@company.com%' // Example condition
LIMIT 5
];
// Assign permission set
List<PermissionSetAssignment> psAssignments = new List<PermissionSetAssignment>();
for (User u : targetUsers) {
psAssignments.add(new PermissionSetAssignment(
AssigneeId = u.Id,
PermissionSetId = newPermSet.Id
));
}
insert psAssignments;
System.debug('✅ Permission set created and assigned successfully.');
JavaScript🧠 Key Notes
- PermissionSetTabSetting.Visibility options:
'Visible'→ Tab shown to user.'Hidden'→ Tab hidden from user.'None'→ No override (inherits from profile).
- You must have Author Apex and Customize Application permissions to execute this code.
- Use Anonymous Apex or Execute Anonymous Window in Developer Console for one-time runs.
Related 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?
