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
ShaikAShaikA 

Trigger provide read access to site user on account object?

Hi All,

I have a requirement to write a trigger to check whether site user have read access to account record or not, if access is not there then i have provide read access to site user.

Please let me know, how to achieve this scenario or share the sample code.

Regards,
Shaik
 
Charisse de BelenCharisse de Belen
To check whether a user has read access to an account record, you can do something like this:
UserRecordAccess ura = [SELECT RecordId, HasReadAccess FROM UserRecordAccess WHERE UserId = :UserInfo.getUserId() AND RecordId = :account.Id];
if(ura.HasReadAccess) {
    // Perform logic here
}
where account is the specific account record you are checking.

To provide read access to the user, you can create a permission set that allows read access and then in the trigger assign the user to that permission set by doing the following:
PermissionSetAssignment psa = new PermissionSetAssignment(PermissionSetId = readAccessPermissionSetId, AssigneeId = UserInfo.getUserId());
insert psa;
ShaikAShaikA
Hi Charusse de Belen,

Thanks for your inputs.

Regards,
Shaik