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
Hemant ReddyHemant Reddy 

Click on any action button should validate whether user has read/write or read access

We have multiple action buttons (images) on a page based on formula fields  . Click on any action button should check whether the current user has edit access to the record . If current user has read only access, display error message, otherwise proceed with existing action button functioanlity . There are several action buttons in a page and same logic should be applied for all . What is the best way to implement this ?
 
Best Answer chosen by Hemant Reddy
RituSharmaRituSharma
On load of your page, run below query to check if user has edit access on the record. Create a boolean variable to store if user has access or now. You may then use this variable further in your logic.

SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE UserId = [single ID] AND RecordId = [single ID]

All Answers

RituSharmaRituSharma
When you say page, I am assuming that you are talking about custom lightning component. You may use getObjectInfo API(https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_object_info). It gives you info about all fields(https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_responses_object_info.htm). Based on your need, you may use the schema of the fields.
Hemant ReddyHemant Reddy
No, it is classic page . If user clicks on any action button in page ( buttons are displayed on record detail using formula fields ), check whether user has read access or read/write access . If user has read access display message "you are not authorized to perform this op", otherwise allow the user to proceed with existing page flow . Since there are many action buttons, I am trying to see whether below is the correct option .
Implement a intermediate page & Controller with below functionality 

Click on any action button in page, redirect to the intermediate page & controler. Intermediate page / controller checks whether user has read or read/write access by querying Share object .
  1.  If user has read only access, display message "You are not authorized to perform the op" and back to the record 
   2. If user has read/write access, redirect to the correct action button URL, which is captured in intermediate page .

 
RituSharmaRituSharma
On load of your page, run below query to check if user has edit access on the record. Create a boolean variable to store if user has access or now. You may then use this variable further in your logic.

SELECT RecordId, HasEditAccess FROM UserRecordAccess WHERE UserId = [single ID] AND RecordId = [single ID]
This was selected as the best answer
Hemant ReddyHemant Reddy
Hi RituSharma,

If we define a variable and set the variable value to true or false based on access on page load , will that variable be accessible for all classes .
In my scenarion, each action button on page goes through separate flow ( apex page and apex classes)
For e.g :  Page has below buttons .
 submit1 button  and on click it calls submitPage1 and submitClass1  apex class
 submit2 button  and on click it calls submitPage2 and submitClass2 apex class
 submit3 button and on click it calls submitPage3 and submitClass3 apex class


 
RituSharmaRituSharma
You may neeed to pass the variable from one class to another.
Hemant ReddyHemant Reddy
No, the classes are not called in sequence . They are independent actions .
As I mentioned, user can click submit1 or submit2 or submit3 buttons and they have independent pages and classes