Salesforce provides a robust and flexible data-sharing model, including features like Organization-Wide Defaults (OWD), Role Hierarchies, Sharing Rules, and Manual Sharing. While these tools cover most use cases, there are times when precise, programmatic control over record-level access is necessary.
One such scenario is when you need to manually share or revoke access to a specific record especially when UI-based options fall short. For example, once a record is manually shared through the UI, there’s no native option to unshare it. That’s where Apex-based manual sharing comes into play.
In this blog, we’ll demonstrate how to programmatically share and unshare a Case record using Apex. We’ll use a simple example with two users—Test Tim and Test Tom—who belong to the same role hierarchy.
Understanding Manual Sharing with Apex
Manual sharing in Salesforce allows users to grant access to individual records without changing org-wide settings. This is done using share objects standard or custom depending on the object type. For Cases, the relevant object is CaseShare.
The CaseShare
object acts as a junction object that specifies:
- Which record is being shared
- Who it’s being shared with (User or Group)
- Access level (Read/Edit)
- Reason for sharing (RowCause)
To manage these shares programmatically, you can use Apex to insert or delete CaseShare records.
Apex Code Example: Sharing and Unsharing a Case
Here’s a utility class that demonstrates both operations:
public class CaseSharingService {
// Method to manually share a Case with a specific user
public static void shareCase(Id caseId, Id targetUserId) {
CaseShare shareRecord = new CaseShare();
shareRecord.CaseId = caseId;
shareRecord.UserOrGroupId = targetUserId;
shareRecord.CaseAccessLevel = 'Edit';
shareRecord.RowCause = Schema.CaseShare.RowCause.Manual;
insert shareRecord;
}
// Method to unshare a previously shared Case
public static void unshareCase(Id caseId, Id targetUserId) {
List<CaseShare> sharesToDelete = [
SELECT Id FROM CaseShare
WHERE CaseId = :caseId
AND UserOrGroupId = :targetUserId
AND RowCause = :Schema.CaseShare.RowCause.Manual
];
if (!sharesToDelete.isEmpty()) {
delete sharesToDelete;
}
}
}
Note
The RowCause must be set toManual
when sharing. Only manual shares can be deleted programmatically. Shares created via sharing rules or role hierarchy cannot be removed this way.
Conclusion
Manual sharing using Apex provides precise control over who has access to specific Case records making it ideal for custom sharing logic in complex orgs with strict data access policies.
However, Salesforce’s UI lacks an “unshare” option for manual shares. Implementing Apex-based solutions helps maintain data governance, security, and flexibility in dynamic environments.
Whether you’re building custom workflows, integrations, or internal tools, programmatically managing record access ensures your data is shared only when needed and revoked just as easily.
By following the above blog instructions, you will be able to learn “How to Programmatically Share and Unshare Case Records in Salesforce Using Apex”. If you still have queries or any related problems, don’t hesitate to contact us at salesforce@greytrix.com. More details about our integration product are available on our website and Salesforce AppExchange.
We hope you may find this blog resourceful and helpful. However, if you still have concerns and need more help, please contact us at salesforce@greytrix.com.
About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.
Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.
The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.
For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.
Related Posts