Skip to main content

Salesforce

Sharing Objects in Salesforce

Innovation. Hands Holding Light Bulb For Concept New Idea Concept With Innovation And Inspiration, Innovative Technology

Salesforce’s robust sharing model ensures that users have access to the data they need while maintaining the security and integrity of sensitive information. A key component of this model is the use of Sharing Objects, which control access to individual records. In this blog, we’ll delve into what Sharing Objects are, how to use them effectively and provide practical coding examples to illustrate their power.

Sharing Objects image

Understanding Salesforce Sharing Objects

Sharing Objects are special objects in Salesforce that allow developers to programmatically share records with users or groups, overriding the organization’s sharing settings. Each standard and custom object in Salesforce has an associated Sharing Object, named with the format ObjectName__Share for custom objects and ObjectNameShare for standard objects.

Key Components of Sharing Objects

  1. ParentId: The ID of the record being shared.
  2. UserOrGroupId: The ID of the user or group with whom the record is being shared.
  3. AccessLevel: The level of access granted (Read, Edit).
  4. RowCause: The reason why the sharing exists (Manual, Sharing Rule, etc.).

Usage of Sharing Objects

Sharing Objects are used to extend the standard sharing behavior of Salesforce. They come in handy when you need to:

  • Share records programmatically with users or groups.

  • Automate complex sharing rules.

  • Ensure compliance with specific business requirements.

Use Case Scenarios

  1. Project Management: Share project records with team members based on their roles or assignments.
  2. Customer Support: Share case records with support agents based on their expertise or workload.
  3. Sales: Share lead records with sales representatives based on territory or product specialization.

Coding Examples

Let’s dive into some practical examples to see how Sharing Objects work in action.

Example 1: Sharing a Custom Object Record with a User

Suppose we have a custom object called Project__c, and we want to share a specific project record with a user.

// ID of the Project record to be shared

Id projectId = 'a0B0o00000ZrR7dEAF';




// ID of the User with whom the record is being shared

Id userId = '0050o00000BjjRdAAJ';




// Create a new Project__Share record

Project__Share projectShare = new Project__Share();

projectShare.ParentId = projectId;

projectShare.UserOrGroupId = userId;

projectShare.AccessLevel = 'Edit'; // Access level can be 'Read' or 'Edit'

projectShare.RowCause = Schema.Project__Share.RowCause.Manual;




// Insert the sharing record

try {

    insert projectShare;

    System.debug('Project shared successfully.');

} catch (DmlException e) {

    System.debug('An error occurred while sharing the project: ' + e.getMessage());

}





Example 2: Sharing a Standard Object Record with a Public Group

Let’s share an Account record with a public group.

// ID of the Account record to be shared

Id accountId = '0010o00002Zp6RgAAJ';




// ID of the Public Group with whom the record is being shared

Id groupId = '00G0o00000XyzRtAAJ';




// Create a new AccountShare record

AccountShare accountShare = new AccountShare();

accountShare.ParentId = accountId;

accountShare.UserOrGroupId = groupId;

accountShare.AccessLevel = 'Read'; // Access level can be 'Read' or 'Edit'

accountShare.RowCause = Schema.AccountShare.RowCause.Manual;




// Insert the sharing record

try {

    insert accountShare;

    System.debug('Account shared successfully.');

} catch (DmlException e) {

    System.debug('An error occurred while sharing the account: ' + e.getMessage());

}

Insights and Best Practices

  1. Minimize Sharing Records: Excessive sharing can lead to performance issues. Only share records when necessary.
  2. Use Public Groups: Sharing with public groups instead of individual users can simplify management and improve performance.
  3. Monitor Sharing Usage: Regularly review sharing settings and records to ensure they align with current business needs and security policies.
  4. Understand RowCause Values: Different RowCause values (like Manual, SharingRule, etc.) help identify the reason for sharing, which can be useful for audits and troubleshooting.

Conclusion

Salesforce Sharing Objects are a powerful tool for managing record access in complex scenarios. By understanding how to leverage these objects, you can ensure that your data sharing  is secure and efficient thus meeting your organization’s specific needs. Whether you’re working on a project management app, a customer support system, or a sales platform, mastering Sharing Objects will enhance your ability to create scalable and secure Salesforce applications.

Happy sharing! 🚀

For more knowledge you can also check out the below articles

arget=”_blank” rel=”noopener”>Secure Accessibility with Apex

Sharing Record using Apex | Salesforce guid

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Reena Joseph

Reena Joseph, our Senior Technical Consultant at Perficient, boasts 3.5 years of experience and holds the prestigious 3x Salesforce Certified title. Her trailblazing spirit is evident with 100 badges on Trailheads, showcasing her commitment to continuous learning. Not limited to Salesforce, Reena has also mastered SQL and Programming in HTML5 with JavaScript and CSS3 on Hacker Rank. Beyond certifications, her passion for staying abreast of technological advancements is seen in her avid reading habits. In the dynamic tech world, Reena Joseph stands ready to drive innovation and inspire with her dedication to excellence.

More from this Author

Categories
Follow Us