Record Sharing - Apex Managed Sharing and Manual Sharing

Record Sharing
  • OWDs - Organization-Wide Defaults
  • Manual Sharing
  • Sharing Rules - Owner based, criteria-based
  • Permission Set
  • Roles
  • Profiles 
If Territory management is enabled, Account access and opportunity assignment based on the territory can be managed.

OWD : 
    levels of access
  • Public Read/Write/Transfer
  • Public Read/Write
  • Public Read/Only
  • Private         

Apex Sharing:
    This helps to share a record programmatically.
    For each sObject, there is a corresponding sObject__share object. Need to insert records with sharing record id and the user or group id to whom record needs to be shared.
    Need to assign an accessLevel(Read)

Apex with Sharing and Without-sharing:

  Two contexts:
  •   System context is associated with "without-sharing" it ignores the sharing model and has access to everything
  •   Triggers, custom web services, visual force custom controllers and custom extensions and test run in this context,
  •   User Context associated with "With Sharing" returns only records that are associated with the current user.
  •   Anonymous blocks, Visual force standard controllers, and extensions run in this context.
  Even though test classes are run in the system context, we can use system.runAs to emulate user.
  Hence with sharing respects the sharing model and without sharing ignores the sharing model.
  Every trigger by default uses system context so all the records are visible despite the sharing model. If we are planning to use "with sharing", write all the login in another class and mark it as with sharing and call that method from the trigger.

Apex Managed Sharing and Manual Sharing are two different approaches to sharing records in Salesforce. Let's explore each of them:

  1. Manual Sharing: Manual Sharing refers to the process of manually granting or revoking access to individual records in Salesforce. It allows administrators or record owners to explicitly specify which users or groups can access specific records. Manual Sharing can be done on a record-by-record basis and offers granular control over access permissions. It is typically used in situations where sharing needs to be customized for specific records.
  2. Apex Managed Sharing: Apex Managed Sharing is a programmatic approach to sharing records in Salesforce. It allows developers to write Apex code to define complex sharing rules and automate the sharing process. With Apex Managed Sharing, you can programmatically share or revoke access to records based on specific criteria or business logic. It is particularly useful when you need to implement more advanced sharing scenarios that go beyond the capabilities of standard Salesforce sharing settings.

When deciding between Manual Sharing and Apex Managed Sharing, consider the following factors:

  • Complexity: If your sharing requirements are simple and can be managed through standard Salesforce sharing settings, Manual Sharing might be sufficient. However, for more complex sharing scenarios that involve custom logic or dynamic sharing rules, Apex Managed Sharing provides greater flexibility.
  • Automation: Apex Managed Sharing allows you to automate the sharing process based on defined criteria, such as specific field values or record attributes. Manual Sharing, on the other hand, requires manual intervention for each record, which can be time-consuming for large datasets.
  • Maintenance: With Manual Sharing, administrators or record owners need to manually adjust sharing settings as access requirements change. Apex Managed Sharing can streamline the process by automating sharing updates based on predefined rules, reducing the maintenance effort.

In summary, Manual Sharing is suitable for simple sharing requirements and offers granular control on a record-by-record basis. Apex Managed Sharing is more suitable for complex sharing scenarios that require programmatic logic and automation. The choice between the two depends on the specific needs of your Salesforce implementation.

No comments:

Powered by Blogger.