function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EdCodeEdCode 

Alternative to the "Sharing" standard button on Page Layout

Hi,
I need to know why is a Account record visible by a User.
I usually leverage the standard "Sharing" button provided by Salesforce Classic.
But, my client has a Visualforce page associated with Account instead of Page Layout, so I cannot use this "Sharing" button.
Also, when going to Lightning, the button is not available in Lightning yet.

When I SOQL query the AccountShare obj. and query the columns Account.Name, AccountAccessLevel, RowCause, UserOrGroup.Name, the information returned by this query is "system context", not "User Context" which is what I really need.

What alternative solution can you think of?

Thank you
Best Answer chosen by EdCode
AnudeepAnudeep (Salesforce Developers) 
Hi TedXYZ7, 

'Sharing' button is not available in Lightning Experience as per https://help.salesforce.com/articleView?id=000339349&type=1&mode=1

For workarounds, you can refer https://salesforcediaries.com/2019/11/06/sharing-button-in-lightning-experience/

The behavior your are seeing with SOQL query the AccountShare obj is working as designed

a) Standard controllers are executed using permission level = user mode (respect the permissions, field-level security) and sharing level = user mode (sharing rules of the current user are enforced)

b) Custom controllers and extensions are executed using permission level = system mode (in which the object and field-level permissions of the current users are ignored). The optional "sharing" clause used will define the sharing level, that's it, if "with sharing" is selected sharing level = user mode (sharing rules of the current user are enforced), otherwise sharing rules for the user will be ignored.

c) In the context of System.runAs(), permission level = user mode is forced and sharing level = user mode (outside of the context of a controller or in the context of a controller if without sharing is not specified) and system mode (in the context of a controller if without sharing is specified).

Example:
-----------
Test:

@istest
private class TestingUserContext {
public static testmethod void test1() { User u = [select id from user where id = '005200000012QNO']; system.debug([select count() from account]);
System.runas(u) {
system.debug([select count() from account]); UserMode um = new UserMode(); system.debug(um.myCount); } } }

Controller:

public without sharing class UserMode { public Integer myCount { get { Integer i = [select count() from contact]; return i; } set; } }

Debug logs:

09:26:03.496 (496272000)|CODE_UNIT_STARTED|[EXTERNAL]|01p20000000MFFQ|TestingUserContext.test1
...
09:26:03.986 (986055000)|SOQL_EXECUTE_BEGIN|[5]|Aggregations:0|select count() from account
09:26:03.989 (989357000)|SOQL_EXECUTE_END|[5]|Rows:45 // ##### sharing level = system mode on line 5
09:26:04.104 (1104790000)|USER_DEBUG|[5]|DEBUG|45
09:26:04.304 (1304631000)|SOQL_EXECUTE_BEGIN|[7]|Aggregations:0|select count() from account
09:26:04.417 (1417404000)|SOQL_EXECUTE_END|[7]|Rows:2 // ##### sharing level = user mode (system.runas()) on line 6
09:26:04.417 (1417692000)|USER_DEBUG|[7]|DEBUG|2
09:26:04.428 (1428437000)|METHOD_ENTRY|[1]|01p20000000MFFV|UserMode.UserMode()
...
09:26:04.437 (1437482000)|SOQL_EXECUTE_BEGIN|[4]|Aggregations:0|select count() from contact
09:26:04.491 (1491438000)|SOQL_EXECUTE_END|[4]|Rows:34 // ##### sharing level = system mode (without sharing used)

Anudeep