{"id":11254,"date":"2025-07-24T07:09:16","date_gmt":"2025-07-24T07:09:16","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=11254"},"modified":"2025-09-01T06:13:45","modified_gmt":"2025-09-01T06:13:45","slug":"how-to-programmatically-share-and-unshare-case-records-in-salesforce-using-apex","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2025\/07\/24\/how-to-programmatically-share-and-unshare-case-records-in-salesforce-using-apex\/","title":{"rendered":"How to Programmatically Share and Unshare Case Records in Salesforce Using Apex"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>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\u2019s <strong>no native option to unshare it<\/strong>. That\u2019s where <strong>Apex-based manual sharing<\/strong> comes into play.<\/p>\n\n\n\n<p>In this blog, we\u2019ll demonstrate how to programmatically share and unshare a Case record using Apex. We\u2019ll use a simple example with two users\u2014<strong>Test Tim<\/strong> and <strong>Test Tom<\/strong>\u2014who belong to the same role hierarchy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Understanding Manual Sharing with Apex<\/mark><\/strong><\/h2>\n\n\n\n<p>Manual sharing in Salesforce allows users to grant access to individual records without changing org-wide settings. This is done using <strong>share objects<\/strong> standard or custom depending on the object type. For Cases, the relevant object is CaseShare.<\/p>\n\n\n\n<p>The <code>CaseShare<\/code> object acts as a <strong>junction object<\/strong> that specifies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Which record<\/strong> is being shared<\/li>\n\n\n\n<li><strong>Who<\/strong> it&#8217;s being shared with (User or Group)<\/li>\n\n\n\n<li><strong>Access level<\/strong> (Read\/Edit)<\/li>\n\n\n\n<li><strong>Reason<\/strong> for sharing (RowCause)<\/li>\n<\/ul>\n\n\n\n<p>To manage these shares programmatically, you can use Apex to <strong>insert<\/strong> or <strong>delete<\/strong> CaseShare records.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">Apex Code Example: Sharing and Unsharing a Case<\/mark><\/strong><\/h2>\n\n\n\n<p>Here\u2019s a utility class that demonstrates both operations:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CaseSharingService {\n\n    \/\/ Method to manually share a Case with a specific user\n    public static void shareCase(Id caseId, Id targetUserId) {\n        CaseShare shareRecord = new CaseShare();\n        shareRecord.CaseId = caseId;\n        shareRecord.UserOrGroupId = targetUserId;\n        shareRecord.CaseAccessLevel = 'Edit';\n        shareRecord.RowCause = Schema.CaseShare.RowCause.Manual;\n\n        insert shareRecord;\n    }\n\n\n    \/\/ Method to unshare a previously shared Case\n    public static void unshareCase(Id caseId, Id targetUserId) {\n        List&lt;CaseShare&gt; sharesToDelete = &#91;\n            SELECT Id FROM CaseShare\n            WHERE CaseId = :caseId\nAND UserOrGroupId = :targetUserId\n            AND RowCause = :Schema.CaseShare.RowCause.Manual\n        ];\n\n        if (!sharesToDelete.isEmpty()) {\n            delete sharesToDelete;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note<\/p>\n<cite>The RowCause must be set to <code>Manual<\/code> when sharing. Only manual shares can be deleted programmatically. Shares created via sharing rules or role hierarchy cannot be removed this way.<\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>Conclusion<\/strong><\/mark><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>However, Salesforce\u2019s UI lacks an &#8220;unshare&#8221; option for manual shares. Implementing Apex-based solutions helps maintain <strong>data governance<\/strong>, <strong>security<\/strong>, and <strong>flexibility<\/strong> in dynamic environments.<\/p>\n\n\n\n<p>Whether you&#8217;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.<\/p>\n\n\n\n<p>By following the above blog instructions, you will be able to learn <strong>\u201c<em>How to Programmatically Share and Unshare Case Records in Salesforce Using Apex\u201d<\/em><\/strong>. If you still have queries or any related problems, don\u2019t hesitate to contact us at <a href=\"mailto:salesforce@greytrix.com\" target=\"_blank\" rel=\"noreferrer noopener\">salesforce@greytrix.com<\/a>. More details about our integration product are available on <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/\" data-type=\"link\" data-id=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/\" target=\"_blank\" rel=\"noreferrer noopener\">our website<\/a> and <a href=\"https:\/\/appexchange.salesforce.com\/listingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noreferrer noopener\">Salesforce AppExchange<\/a>.<\/p>\n\n\n\n<p>We hope you may find this blog resourceful and helpful. However, if you still have concerns and need more help, please contact us at <a href=\"mailto:salesforce@greytrix.com\" target=\"_blank\" rel=\"noreferrer noopener\">salesforce@greytrix.com<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 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.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, 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&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; 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.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2025\/01\/24\/enhancing-salesforce-security-how-to-implement-my-domain-login-restrictions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Enhancing Salesforce Security: How to Implement My Domain Login Restrictions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2016\/08\/08\/public-access-settings-in-salesforce\/\" target=\"_blank\" rel=\"noreferrer noopener\">Public Access Settings in Salesforce<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2023\/07\/20\/grant-login-access-to-salesforce-admin-or-support\/\">Grant Login Access to Salesforce Admin or Support in Salesforce Lightning Interface<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2025\/07\/24\/how-to-programmatically-share-and-unshare-case-records-in-salesforce-using-apex\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[13,2200,2207,2193,2199,2198,2189,2190,2194,2195,2197,2201,2192,588,878,2203,1984,2204,2206,2196,2205],"class_list":["post-11254","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-apex","tag-apex-sharing","tag-case-access-control","tag-case-records","tag-case-sharing","tag-caseshare-object","tag-manual-sharing","tag-manually-unshare","tag-org-wide-defaults","tag-owd","tag-role-hierarchies","tag-role-hierarchy","tag-role-hierarchy-unshare","tag-salesforce-apex","tag-salesforce-permissions","tag-salesforce-record-access","tag-salesforce-security","tag-salesforce-service-cloud","tag-share-case-records-in-salesforce","tag-sharing-rules","tag-unshare-case-records-salesforce"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/11254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=11254"}],"version-history":[{"count":10,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/11254\/revisions"}],"predecessor-version":[{"id":11274,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/11254\/revisions\/11274"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=11254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=11254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=11254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}