SharingRules

Represents the base container for sharing rules, which can be criteria-based, ownership-based, territory-based, or for guest user access. SharingRules enables you to share records with a set of users, using rules that specify the access level for the target user group.

For more information, see “Sharing Rules” in Salesforce Help.

In API version 33.0 and later, retrieving, deleting, or deploying of all sharing rules in an organization is available. Wildcard support is also available. You can’t retrieve, delete, or deploy manual sharing rules or sharing rules by their type (owner, criteria-based, territory, or guest user).

Parent Type 

This type extends Metadata and inherits its fields.

File Suffix and Directory Location 

In API version 33.0 and later, components are stored in the sharingRules folder and their file name matches the object name with the suffix .sharingRules. Criteria-based, owner-based, territory-based, and guest user sharing rules are all contained in a object.sharingRule file.

Before API version 33.0, SharingRules components are stored in their corresponding object directory and the file name matches the object name. For example, the accountSharingRules directory contains an Account.sharingRules file for account sharing rules. SharingRules for custom objects are stored in the customObjectSharingRules directory, which contains files with the .sharingRules extension such as ObjA__c.sharingRules, where ObjA refers to the developer name of a custom object type.

Version 

Introduced in API version 24.0.

SharingRules components are available in API version 24.0 and later, but these components are no longer available in API version 33.0 and later: AccountSharingRules, CampaignSharingRules, CaseSharingRules, ContactSharingRules, LeadSharingRules, OpportunitySharingRules, AccountTerritorySharingRules, CustomObjectSharingRules, UserSharingRules.

In API version 33.0 and later, use SharingCriteriaRule, SharingOwnerRule and SharingTerritoryRule.

Special Access Rules 

As of Spring ’20 and later, only users with the View Setup and Configuration permission can access this object, and only users with the Manage Sharing permission can edit this object.

Fields 

sharingCriteriaRules 

  • Type: SharingCriteriaRule[]
  • Required: No
  • Introduced: API version 33.0
  • Description: An array of criteria-based sharing rules.

sharingGuestRules 

  • Type: SharingGuestRule[]
  • Required: No
  • Introduced: API version 47.0
  • Description: An array of guest user sharing rules.

sharingOwnerRules 

  • Type: SharingOwnerRule[]
  • Required: No
  • Introduced: API version 33.0
  • Description: An array of ownership-based sharing rules.

sharingTerritoryRules 

  • Type: SharingTerritoryRule[]
  • Required: No
  • Introduced: API version 33.0
  • Description: An array of territory-based sharing rules.

AccountSharingRules Type 

Represents the sharing rules for accounts. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: AccountCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: AccountOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

AccountTerritorySharingRules Type 

Represents the sharing rules for account territories in the original territory management feature. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

rules 

  • Type: AccountTerritorySharingRule (enumeration of type string)[]
  • Required: No
  • Description: List that defines user membership-based rules. The list of acceptable values for the sharedFrom fields are:
  • Possible values:
    • territory
    • territoryAndSubordinates

CampaignSharingRules Type 

Represents the sharing rules for campaigns. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: CampaignCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: CampaignOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

CaseSharingRules Type 

Represents the sharing rules for cases. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: CaseCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: CaseOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

ContactSharingRules Type 

Represents the sharing rules for contacts. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: ContactCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: ContactOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

CustomObjectSharingRules Type 

Represents the sharing rules for custom objects. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: CustomObjectCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: CustomObjectOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

LeadSharingRules Type 

Represents the sharing rules for leads. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: LeadCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: LeadOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

OpportunitySharingRules Type 

Represents the sharing rules for opportunities. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: OpportunityCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

ownerRules 

  • Type: OpportunityOwnerSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

SharingCriteriaRule Type 

Defines a criteria-based sharing rule.

Parent Type 

This type extends SharingBaseRule and inherits its fields.

Version 

Introduced in API version 33.0.

Fields 

booleanFilter 

  • Type: string
  • Required: No
  • Description: Advanced filter conditions that are specified for the sharing rule.

criteriaItems 

  • Type: FilterItem[]
  • Required: No
  • Description: An array of the boolean criteria (conditions) for the sharing rule.

includeRecordsOwnedByAll 

  • Type: boolean

  • Required: Yes

  • Description: Required. Indicates whether records owned by users who can’t have an assigned role are included in the records shared (true) or not (false). Examples of users who can’t have an assigned role are high-volume users and system users such as automated process users.or Salesforce system users.

    You can’t edit this field after the sharing rule is created.

SharingGuestRule Type 

Defines a guest user sharing rule. :::note For SharingGuestRule, the accessLevel field can be set only to Read. :::

Parent Type 

This type extends SharingBaseRule and inherits its fields.

Version 

Introduced in API version 47.0.

For SharingGuestRule, the accessLevel field can be set only to Read .

Note

Fields 

booleanFilter 

  • Type: string
  • Required: No
  • Introduced: API version 48.0
  • Description: Advanced filter conditions that are specified for the sharing rule.

criteriaItems 

  • Type: FilterItem[]
  • Required: No
  • Introduced: API version 48.0
  • Description: An array of the boolean criteria (conditions) for the sharing rule.

includeHVUOwnedRecords 

  • Type: boolean
  • Required: Yes
  • Introduced: API version 52.0
  • Description: Required. Indicates whether records owned by high-volume community or site users are included in the records shared (true) or not (false). By default, only records owned by authenticated users, guest users, and queues are included in sharing rules. This field has a default value of false. You can’t edit this field after the sharing rule is created.

SharingOwnerRule Type 

Defines an ownership-based sharing rule.

Parent Type 

This type extends SharingBaseRule and inherits its fields.

Version 

Introduced in API version 33.0.

Fields 

sharedFrom 

  • Type: SharedTo

  • Required: Yes

  • Description: Required. Specifies the record owners.

    If you’re using Sales Territories, use modelName.territoryName for the shared-to and shared-from territory and territoryAndSubordinates values on the SharedTo type, where:

    • modelName equals the name of the active territory model in the API.
    • territoryName equals the territory’s name in the API.

SharingTerritoryRule Type 

Defines a territory-based sharing rule.

Parent Type 

This type extends SharingOwnerRule and inherits its fields.

Version 

Introduced in API version 33.0.

UserSharingRules Type 

Represents the sharing rules for users. With user sharing rules, you can share members of a group with members of another group. It extends the SharingRules metadata type and inherits its fullName field. Only available in API version 32.0 and earlier.

Fields 

criteriaBasedRules 

  • Type: UserCriteriaBasedSharingRule[]
  • Required: No
  • Description: List that defines user criteria-based rules.

membershipRules 

  • Type: UserMembershipSharingRule[]
  • Required: No
  • Description: List that defines user membership-based rules.

Declarative Metadata Sample Definition 

For retrieving sharing rules, see package.xml sample at SharingRules.

The following sample XML definition represents a criteria-based sharing rule in API version 33.0.

1<?xml version="1.0" encoding="UTF-8"?>
2<SharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
3    <sharingCriteriaRules>
4        <fullName>AccountCriteriaShareWithCEO</fullName>
5        <accessLevel>Edit</accessLevel>
6        <accountSettings>
7            <caseAccessLevel>Read</caseAccessLevel>
8            <contactAccessLevel>Edit</contactAccessLevel>
9            <opportunityAccessLevel>Edit</opportunityAccessLevel>
10        </accountSettings>
11        <criteriaItems>
12            <field>Name</field>
13            <operation>startsWith</operation>
14            <value>Test</value>
15        </criteriaItems>
16        <description>my account criteria rule description</description>
17        <label>AccountCriteriaShareWithCEO</label>
18        <sharedTo>
19            <role>CEO</role>
20        </sharedTo>
21    </sharingCriteriaRules>
22</SharingRules>

The following sample XML definition represents an ownership-based sharing rule in API version 33.0.

1<?xml version="1.0" encoding="UTF-8"?>
2<SharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
3    <sharingOwnerRules>
4        <fullName>MyCase</fullName>
5        <accessLevel>Edit</accessLevel>
6        <description>my case test owner sharing rule desc</description>
7        <label>MyCase</label>
8        <sharedFrom>
9            <role>COO</role>
10        </sharedFrom>
11        <sharedTo>
12            <role>CEO</role>
13        </sharedTo>
14    </sharingOwnerRules>
15</SharingRules>

The following sample XML definition represents a territory-based sharing rule in API version 33.0.

1<?xml version="1.0" encoding="UTF-8"?>
2<SharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
3    <sharingTerritoryRules>
4        <fullName>MyAccountTerritoryRule</fullName>
5        <accessLevel>Read</accessLevel>
6        <accountSettings>
7            <caseAccessLevel>None</caseAccessLevel>
8            <contactAccessLevel>Read</contactAccessLevel>
9            <opportunityAccessLevel>None</opportunityAccessLevel>
10        </accountSettings>
11        <description>MyAccountTerritoryRule desc</description>
12        <label>MyAccountTerritoryRule</label>
13        <sharedFrom>
14            <territory>My_territory</territory>
15        </sharedFrom>
16        <sharedTo>
17            <role>CEO</role>
18        </sharedTo>
19    </sharingTerritoryRules>
20</SharingRules>

The following is the definition of two account owner-based sharing rules in API version 32.0 and earlier. The file name corresponds to Account.sharingRules under the accountSharingRules directory. In this definition, ownerRules corresponds to AccountOwnerSharingRule.

1<?xml version="1.0" encoding="UTF-8"?>
2<AccountSharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
3    <ownerRules>
4        <fullName>G1Dev_G2New</fullName>
5        <sharedFrom>
6            <group>G1Dev</group>
7        </sharedFrom>
8        <sharedTo>
9            <group>G2New</group>
10        </sharedTo>
11        <accountAccessLevel>Read</caseAccessLevel>
12        <caseAccessLevel>None</caseAccessLevel>
13        <contactAccessLevel>Read</contactAccessLevel>
14        <name>G1Dev_G2New</name>
15        <opportunityAccessLevel>Edit</opportunityAccessLevel>
16    </ownerRules>
17    <ownerRules>
18        <fullName>G2New_R1New</fullName>
19        <sharedFrom>
20            <group>G2New</group>
21        </sharedFrom>
22        <sharedTo>
23            <roleAndSubordinates>R1New</roleAndSubordinates>
24        </sharedTo>
25        <accountAccessLevel>Edit</accountAccessLevel>
26        <caseAccessLevel>Read</caseAccessLevel>
27        <contactAccessLevel>Edit</contactAccessLevel>
28        <name>G2New_R1New</name>
29        <opportunityAccessLevel>None</opportunityAccessLevel>
30    </ownerRules>
31</AccountSharingRules>

The following is the definition of a user criteria-based sharing rule and a user membership-based sharing rule in API version 32.0 and earlier. The file name corresponds to User.sharingRules under the userSharingRules directory.

1<?xml version="1.0" encoding="UTF-8"?>
2<UserSharingRules xmlns="http://soap.sforce.com/2006/04/metadata">
3    <criteriaBasedRules>
4        <fullName>shareUsers2</fullName>
5        <sharedTo>
6            <group>Asia_Division</group>
7        </sharedTo>
8        <criteriaItems>
9            <field>FirstName</field>
10            <operation>equals</operation>
11            <value>John</value>
12        </criteriaItems>
13        <name>shareUsers2</name>
14        <userAccessLevel>Read</userAccessLevel>
15    </criteriaBasedRules>
16    <membershipRules>
17        <fullName>shareUsers1</fullName>
18        <sharedTo>
19            <group>South_America_Division</group>
20        </sharedTo>
21        <sharedFrom>
22            <group>Asia_Division</group>
23        </sharedFrom>
24        <name>shareUsers1</name>
25        <userAccessLevel>Read</userAccessLevel>
26    </membershipRules>
27</UserSharingRules>

The following shows a sample package.xml file.

1<?xml version="1.0" encoding="UTF-8"?>
2<Package xmlns="http://soap.sforce.com/2006/04/metadata">
3    <types>
4        <members>ObjA__c.*</members>
5        <name>SharingCriteriaRule</name>
6    </types>
7    <types>
8        <members>ObjA__c.*</members>
9        <name>SharingOwnerRule</name>
10    </types>
11    <version>68.0</version>
12</Package>

Wildcard Support in the Manifest File 

This metadata type supports the wildcard character * (asterisk) in the package.xml manifest file. For information about using the manifest file, see Deploying and Retrieving Metadata with the Zip File.