{"id":2998,"date":"2012-07-27T23:53:29","date_gmt":"2012-07-27T18:23:29","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2998"},"modified":"2020-05-04T19:03:35","modified_gmt":"2020-05-04T23:03:35","slug":"apex-based-sharing-in-salesforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-based-sharing-in-salesforce\/","title":{"rendered":"Apex based record sharing in Salesforce"},"content":{"rendered":"\n<p><em>Last update :<\/em> May 4 2020<\/p>\n\n\n\n<p class=\"justify\">There are situations where the business requirement is too complex and standard sharing rules provided by the Salesforce will not work.<\/p>\n\n\n\n<p class=\"justify\"><strong>Example:<\/strong> On Opportunity, you want to give access to record to some users which are in related list.<br>One way is to manually share the record which will need the interference of opportunity owner. But everyone will love automated solution.<\/p>\n\n\n\n<p class=\"justify\">Apex managed sharing provides developers with the ability to support an application&#8217;s particular sharing requirements programmatically via Apex code.<\/p>\n\n\n\n<p><strong>Before you proceed, few points to note about Apex based sharing<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Share table available only when <strong>Organization Wide Default<\/strong> [OWD] sharing is not Public Read Write<\/li><li>Apex sharing reasons can be created only for custom objects<\/li><li>As <strong>Apex Sharing Reason<\/strong> not available for Standard objects only way to create Apex based sharing for Standard objects are using row cause Manual<\/li><li>As row cause is Manual for Standard objects, <strong>Apex based sharing would be lost once owner is changed<\/strong> [Because its behavior of Manual Sharing]<\/li><li>In case of custom object &amp; custom Apex Sharing Reason, sharing would not be lost when owner changed<\/li><li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/apex_bulk_sharing_creating_with_apex.htm?search_text=apex%20sharing\">Read more in detail from official documentation <\/a><\/li><\/ul>\n\n\n\n<p><strong>Pre requisite:<\/strong><\/p>\n\n\n\n<p class=\"justify\">The object&#8217;s organization-wide default access level must not be set to the most permissive access level. For custom objects, this is Public Read\/Write. For more information, see <a href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_bulk_sharing_understanding.htm#sharing_access_levels\" target=\"_blank\" rel=\"noreferrer noopener\">Access Levels<\/a>. This object is used for creating Apex based sharing.<\/p>\n\n\n\n<p class=\"justify\">The user to which the record going to be shared must have the object level permission. If you are trying to share the record with edit permission but user does not have the edit permission on that object, then it will not work.<\/p>\n\n\n\n<p class=\"justify\">To access sharing programmatically, you must use the share object associated with the standard or custom object for which you want to share. For example, AccountShare is the sharing object for the Account object, ContactShare is the sharing object for the Contact object, and so on. In addition, all custom object sharing objects are named as follows, where MyCustomObject is the name of the custom object: &#8220;MyCustomObject__Share&#8221;.<\/p>\n\n\n\n<p class=\"justify\">Objects on the detail side of a master-detail relationship do not have an associated sharing object. The detail record&#8217;s access is determined by the master&#8217;s sharing object and the relationship&#8217;s sharing setting.<\/p>\n\n\n\n<p>In this article I want to share the custom object &#8220;Test__c&#8221;\u009d with students. Let&#8217;s consider that every &#8220;Test&#8221;\u009d has lookup to the &#8220;Student&#8221;\u009d.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>Sharing Table<\/strong><\/p>\n\n\n\n<p class=\"justify\">All objects that have a default sharing setting of the either &#8220;Private&#8221; or &#8220;Public Read Only&#8221; also have a related &#8220;Share&#8221; object that is similar to an access control list (ACL) found in other platforms. All share objects for custom objects are named as MyCustomObject__Share, where MyCustomObject__c is the name of the related custom object. A share object includes records supporting all three types of sharing: Force.com managed sharing, user managed sharing, and Apex managed sharing.<\/p>\n\n\n\n<p>A custom object&#8217;s share object allows four pieces of information to be defined:<\/p>\n\n\n\n<ul class=\"justify wp-block-list\"><li>The record being shared.<\/li><li>The User or Group with whom the object is being shared.<\/li><li>The permission level being granted to the User or Group.<\/li><li>The reason why the User or Group has been granted sharing access.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Share-Table.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"47\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Share-Table.png?resize=733%2C47&#038;ssl=1\" alt=\"Salesforce Share Table\" class=\"wp-image-4419\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Share-Table.png?w=733&amp;ssl=1 733w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Share-Table.png?resize=300%2C19&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Share-Table.png?resize=624%2C40&amp;ssl=1 624w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/a><figcaption>Salesforce Share Table<\/figcaption><\/figure><\/div>\n\n\n\n<p>This information corresponds with the following fields in a share object:<br>\n<table id=\"tablepress-7\" class=\"tablepress tablepress-id-7\">\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-1\">\n\t<td class=\"column-1\"><div>ParentId<\/div><\/td><td class=\"column-2\"><div>The Id of the record being shared. This field cannot be updated.<\/div><\/td>\n<\/tr>\n<tr class=\"row-2\">\n\t<td class=\"column-1\">UserOrGroupId<\/td><td class=\"column-2\">The Id of the User to whom you are granting access. May also be a Public Group Id, Role Id, or Territory Id. This field cannot be updated.<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">AccessLevel<\/td><td class=\"column-2\">The level of access that the specified User or Group has been granted.<br\/><br\/>  Valid values for Apex managed sharing are: Edit, Read.<br\/><br\/>  This field must be set to an access level that is higher than the organization\u2019s default access level for the parent object. For more information, see <a rel=\"nofollow\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_bulk_sharing_understanding.htm#sharing_access_levels\"> Access Levels.<\/a><br\/><\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">RowCause (aka Sharing Reasons)<\/td><td class=\"column-2\">The reason why the user or group is being granted access. The reason determines the type of sharing, which in turn controls who can alter the sharing record. This field cannot be updated.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-7 from cache --><\/p>\n\n\n\n<p><strong>Apex sharing reason<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"268\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=1024%2C268&#038;ssl=1\" alt=\"Salesforce Apex Sharing Reason\" class=\"wp-image-4416\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=1024%2C268&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=300%2C78&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=624%2C163&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?w=1083&amp;ssl=1 1083w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><figcaption>Salesforce Apex Sharing Reason<\/figcaption><\/figure><\/div>\n\n\n\n<p>Before we can start writing any Apex managed sharing code, we must create an Apex sharing reason. To do that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Click &#8220;Setup | Create | Objects&#8221;.<\/li><li>Select the custom object. (In this case, the &#8220;Test&#8221; Custom object.)<\/li><li>Click New in the Apex Sharing Reasons related list. (If you don&#8217;t see this related list, Apex managed sharing has not been configured for your Org &#8211; please contact your support representative.)<\/li><li>Enter a label for the Apex sharing reason.<\/li><li>Enter a name for the Apex sharing reason.<\/li><li>Click Save.<\/li><\/ul>\n\n\n\n<p>Now let&#8217;s start with Apex code:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/JitendraZaa\/3cd6eb71c61781e3e8b9502ba16d235c.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Working with Apex based sharing in Salesforce<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,24,9],"tags":[337,331],"class_list":["post-2998","post","type-post","status-publish","format-standard","hentry","category-apex","category-force-com","category-salesforce","tag-apex","tag-salesforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":4546,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/system-mode-or-god-mode-in-apex-gotchas\/","url_meta":{"origin":2998,"position":0},"title":"System mode or God mode in Apex &#8211; Gotchas","author":"Jitendra","date":"June 8, 2015","format":false,"excerpt":"Gotchas of System mode or God mode in Apex and its impact by using \"With sharing\" keyword in Salesforce","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce God Mode failing","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-God-Mode-failing.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-God-Mode-failing.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-God-Mode-failing.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6777,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-key-notes\/","url_meta":{"origin":2998,"position":1},"title":"Enterprise Territory Management &#8211; Key Notes","author":"Jitendra","date":"February 11, 2019","format":false,"excerpt":"How to use Apex based sharing to share opportunity using Territories","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Territory Management - Data Model","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2019\/02\/Territory-Management-Data-Model.png?fit=1160%2C760&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":2825,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-7\/","url_meta":{"origin":2998,"position":2},"title":"Salesforce Interview Questions &#8211; Part 7","author":"Jitendra","date":"April 23, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1305,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-3\/","url_meta":{"origin":2998,"position":3},"title":"Salesforce Interview Questions \u2013 Part 3","author":"Jitendra","date":"October 12, 2010","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Visual force, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3427,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-winter-14-new-features\/","url_meta":{"origin":2998,"position":4},"title":"Salesforce Winter 14 &#8211; New Features","author":"Jitendra","date":"August 27, 2013","format":false,"excerpt":"Major Area of Release: Developer Console Canvas HTML5 Support in Visualforce Deployment related features like improved deployment Monitoring New Database methods to get records deleted , updated in specific time Renaming \"Service Cloud Console\"\u009d is renamed to \"Salesforce Console for Service\"\u009d \"Configuration Only\"\u009d sandbox renamed to \"Developer Pro\"\u009d \"Agent Configuration\"\u009d\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Winter 14","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/08\/Win-14-Logo.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2762,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-6\/","url_meta":{"origin":2998,"position":5},"title":"Salesforce Interview Questions &#8211; Part 6","author":"Jitendra","date":"March 10, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce - External Id option while creating field","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2998"}],"version-history":[{"count":8,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2998\/revisions"}],"predecessor-version":[{"id":7069,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2998\/revisions\/7069"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}