---
schema_version: "1"
id: 1625762b8d37b6384c16b007e2822f09a78f18a683627c554b269db9ceccb26e
title: Recalculate Apex Managed Sharing
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/platform/264-0-0/security_apex_sharing_recalc.md
canonical_url: https://help.salesforce.com/s/articleView?id=platform.security_apex_sharing_recalc.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 40be93339aecac885a3d044d710c53be1ea4663b127c9011329992b64c3ea5f9
description: Developers can write batch Apex classes that recalculate the Apex managed sharing for a specific custom object.
products:
  - Salesforce Platform
release: "264"
---
# Recalculate Apex Managed Sharing

Developers can write batch Apex classes that recalculate the Apex managed sharing for a specific custom object.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: Salesforce Classic</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>Professional</strong>, <strong>Enterprise</strong>, <strong>Performance</strong>, <strong>Unlimited</strong>, <strong>Developer</strong>, and <strong>Database.com</strong> Editions</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d37095e79">User
              Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d37095e79 ">To associate an Apex managed sharing recalculation class:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d37095e79 ">Author Apex</td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d37095e79 ">To run an Apex managed sharing recalculation:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d37095e79 ">
              <p>Author Apex</p>
              <p>OR</p>
              <p style="margin-bottom:0;">Manage Sharing</p>
            </td>

          </tr>
        </tbody>
      </table>

![Important](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-platform-extend_code-264-0-0-production-enus/789bc523-706b-4ae4-8fe9-be6322e38b6b/images/icon_important.png)

Important When packaging custom objects, be aware that associated Apex sharing recalculations are also included and may prevent the package from installing.

Developers can write batch Apex classes that recalculate the Apex managed sharing for a specific custom object. You can associate these classes with a custom object on its detail page, and execute them if a locking issue prevents Apex from granting access to a user as defined by the application’s logic. Apex sharing recalculations are also useful for resolving visibility issues due to coding errors. For example, if a developer corrects a coding error that prevented users from accessing records they should see, the correction might only affect records created after the code update. To ensure the correction applies to existing records as well, the developer can run an Apex sharing recalculation to validate sharing on all records.

You can run Apex sharing recalculations from a custom object's detail page. You can also run them programmatically using the `Database.executeBatch` method. In addition, Salesforce automatically runs Apex recalculation classes defined for a custom object every time a custom object's organization wide sharing default access level is updated.

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-platform-extend_code-264-0-0-production-enus/789bc523-706b-4ae4-8fe9-be6322e38b6b/images/icon_note.png)

Note

Salesforce automatically recalculates sharing for all records on an object when its organization-wide sharing default access level changes. The recalculation includes access granted by sharing rules. In addition, all types of sharing are removed if the access they grant is redundant. For example, the manual sharing which grants Read Only access to a user is deleted when the object’s sharing model is changed from Private to Public Read Only.

1. From the management settings for the custom object, go to Apex Sharing Recalculations.
2. Choose the Apex class that recalculates the Apex sharing for this object. The class you choose must implement the `Database.Batchable` interface. You cannot associate the same Apex class multiple times with the same custom object.
3. Click **Save**.

For information on creating Apex managed sharing and recalculation classes, see the [Apex Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_security_sharing_chapter.htm).

To associate an Apex managed sharing recalculation class with a custom object:

To run an Apex sharing recalculation, from the management settings for a custom object, go to Apex Sharing Recalculation, and then click **New**.

When working with Apex sharing recalculations, note the following.

- The Apex code that extends the sharing recalculation can process a maximum of five million records. If this Apex code affects more than five million records, the job fails immediately.
- You can monitor the status of Apex sharing recalculations in the Apex job queue.
- You can associate a maximum of five Apex sharing recalculations per custom object.
- You cannot associate Apex sharing recalculations with standard objects.

#### See Also

- [Create Apex Sharing Reasons](https://help.salesforce.com/s/articleView?id=platform.security_apex_sharing_reasons.htm&release=264.0.0&type=5 "When creating Apex managed sharing, create Apex sharing reasons for individual custom objects to indicate why sharing was implemented.")
- [Find Object Management Settings](https://help.salesforce.com/s/articleView?id=platform.extend_click_find_objectmgmt_parent.htm&type=5&language=en_US)