• Who We Are
    • About Us
    • Factor - D
    • Our Networks
    • Accolades
  • What We Do

    SERVICES

    • Consulting / Implementation
    • Migration
    • Development

    OUR SOLUTIONS

    • GUMU Integration
    • BPortaly
    • Sales Commission
    • Shipping
    • IOT

    COMPREHENSIVE SERVICES FOR

    ERP

    • Acumatica
    • Sage Intacct
    • Sage X3
    • Sage 300
    • Sage 100
    • Sage 500

    CRM

    • Salesforce
    • D365 CRM
    • Sage CRM
    • Creatio
    •  

    Other Solutions

    • E-commerce
    • EDI
    • POS
    •  
    •  
  • Resources
    • Case Study
    • Blogs
    • Videos
    • Ebooks
    • News
  • +1 888 221 6661
Contact us
Career |
  • Africa
  • Middle East

Salesforce.com - Tips, Tricks and Components

Welcome to the treasure trove of Greytrix’s Salesforce knowledge. This page is dedicated to providing you with insightful articles, expert tips, and the latest updates on Salesforce CRM. Whether you want to enhance your Salesforce experience or want to stay updated on industry trends, our blog has you covered. In addition, we also offer cutting-edge solutions for Salesforce and are committed to delivering unparalleled support and innovation for Salesforce users. Explore and enhance your Salesforce experience with Greytrix.

How to Programmatically Share and Unshare Case Records in Salesforce Using Apex

By Greytrix | July 24, 2025
0 Comment

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 to Manual 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

  • Enhancing Salesforce Security: How to Implement My Domain Login Restrictions
  • Public Access Settings in Salesforce
  • Grant Login Access to Salesforce Admin or Support in Salesforce Lightning Interface

Category: Salesforce Services Tags: Apex, Apex Sharing, Case Access Control, Case records, Case Sharing, CaseShare object, manual sharing, manually unshare, Org-Wide Defaults, OWD, Role Hierarchies, Role Hierarchy, role hierarchy unshare, salesforce apex, Salesforce Permissions, Salesforce Record Access, Salesforce security, Salesforce Service Cloud, Share Case Records in Salesforce, Sharing Rules, Unshare Case Records Salesforce
Post navigation
← How AI and Automation in Salesforce are Streamlining Supply Chain Workflows How to Assign the GUMU™ Integration License and Permission Set to a User in Salesforce →

Greytrix Websites and Blogs

  • Greytrix
  • GUMU™ Cloud
  • Greytrix Africa
  • Sage X3 – Tips, Tricks and Components
  • Sage 100 & 500 ERP Tips and Tricks
  • Sage 300 – Tips, Tricks and Components
  • Sage CRM – Tips, Tricks and Components

GUMU™ Salesforce Sage Integration

https://www.youtube.com/watch?v=X9Dg4uwGT-0

Categories

  • GUMU™ Features
  • GUMU™ Salesforce
  • GUMU™ Tricks & Tips
  • Salesforce Services
  • Uncategorized
sage enterprise management services

Recent Posts

  • How to Assign the GUMU™ Integration License and Permission Set to a User in Salesforce
  • How to Programmatically Share and Unshare Case Records in Salesforce Using Apex
  • How AI and Automation in Salesforce are Streamlining Supply Chain Workflows
  • Secure API Integrations in Salesforce Using Named Credentials
  • Sort Sales Orders by Most Recent Date on ERP Real-Time Page in Salesforce for Sage X3

Popular blogs

  • Thinking of enhancing your business processes? Get the best ERP – CRM connector on Salesforce AppExchange!
  • GUMU™ for Salesforce – Sage ERP Integration
  • Leverage real-time enterprise data for better insights of your Customers with Salesforce – Sage 300 integration
  • GUMU™ Integration for Salesforce with Sage 300

Archives

Awards

Premier Partner of the Year Premier Partner of the Year
Platinum Partner of the Year Platinum Partner of the Year
The CEO Story The CEO Story
Gold Partner pf the Year Gold Partner pf the Year
Top Excellence Distributor Top Excellence Distributor
  • na.sales@greytrix.com
  • +1 888 221 6661
  • B301, 3rd Floor, Everest Nivara Infotech Park, MIDC, Turbhe,
    Navi Mumbai 400 705. India

Our Global Locations >>

Who We Are

  • About Us
  • Factor - D
  • Our Network
  • Accolades
  • Career
  •  
  •  

Services

  • Consultation & Implementation
  • Migration
  • Development

Solutions

  • GUMUTM Integration
  • BPortaly
  • Sales Commission
  • Shipping
  • IOT
  •  
  •  

ERP

  • Acumatica
  • Sage Intacct
  • Sage X3
  • Sage 300
  • Sage 100
  • Sage 500

CRM

  • Salesforce
  • D365 CRM
  • Sage CRM
  • Creatio
  •  
  •  
  •  

Other Solutions

  • E-commerce
  • EDI
  • POS

Resources

  • Case Study
  • Blogs
  • Videos
  • E-books
  • News
  •  
  •  

Connect

  • Contact Us
  • Privacy Policy
  • Terms Of Use
  •  

© 2025. Greytrix, All Rights Reserved