How to share records with permission sets in Salesforce?

In Salesforce, sharing records with users who have specific Permission Sets is not a direct process, as Permission Sets are primarily designed to grant object and field-level access rather than to control record-level access.
However, you can achieve record sharing with users having specific Permission Sets by combining several features of Salesforce.
Here’s a general approach:
- Create a Public Group or Queue: First, create a Public Group or Queue and add users to this group. Ensure that the users in this group are the ones with the specific Permission Set you’re targeting.
- Use Apex Managed Sharing (If Necessary): If you need more granular control over sharing and the built-in sharing settings (like Role Hierarchy, Sharing Rules, etc.) are not sufficient, you can use Apex Managed Sharing. Write an Apex class to share records with the Public Group or Queue. This class can query users based on the Permission Set assignment and then share records accordingly.
- Create Sharing Rules: Use Criteria-Based Sharing Rules to share records with the Public Group or Queue. This does not directly relate to the Permission Set but will share the records with the group of users you’ve assembled.
- Regularly Update the Group Members: If users are frequently added or removed from the Permission Set, you might need to maintain the Public Group or Queue membership to keep it in sync. This can be done manually or through automation (e.g., using an Apex trigger or scheduled job).
- Test and Validate: After setting up, thoroughly test the sharing configuration to ensure that it meets your requirements and that only intended users have access to the shared records.
It’s important to note that this approach is somewhat indirect. Salesforce does not natively support sharing based directly on Permission Sets because Permission Sets are meant to grant permissions at a broader level than individual record access. The method above is a workaround that leverages the combination of existing Salesforce features to approximate the desired outcome.