0

is there a way to check if the current user has permission to edit an object (Controlled by Profile and Permission Sets).

This is my scenario:

  1. I have a custom LWC which pulls information from multiple objects. (For simplicity, let's assume ObjectA__c and ObjectB__c)
  2. Only some users (controlled by profile and permission sets) are able to create/edit these objects and their records. For example: only I can edit these records, as only I have the right profile and permission sets assigned.
  3. Other user's opening this component, should not be able to edit any of these fields (This will be controlled by just setting the disabled property in the lightning-input, if I know the current user has no permission to edit, then I will set the disabled property to true for all lightning-inputs)

So this is what I want to do, basically, I want to be able to check in LWC

Whether the current user have permission to edit the objects or not.

I have referred to this stack. Where I can use the UserRecordAccess object, but this will require me to input a recordId. My concern is that, what is the record is shared to this user? Also there's a limitation of only 200 recordIds. Also I tried, executing this query in anonymous apex and printed it in log, I could not seem to find the UserRecordAccess. getting logged:

SELECT Id, Name, UserRecordAccess.HasReadAccess, UserRecordAccess.HasTransferAccess, UserRecordAccess.MaxAccessLevel FROM sObjectName

Query Credits - Marco Pietersen from the same stack hyperlinked above.

Main Question:
Is there a way to only input the Object name and check whether the current user has permission to create/edit this object records or not? Another way of doing this, is of course by letting the user's edit and then in the save logic commit the save with as user and show a toast (error) the users. While, I think it is better to stop them from editing in the first place, is a better design.

Many thanks in advance!

1 Answer 1

1

According to Salesforce security, the current user could only edit a specific set of records (sharing settings could be private so you only edit your owned records), so using only an object name could be not sure that the user will be able to edit the records afterwards.

If you want to check the CRUD settings for the object, you can use object describe from Schema namespace

@AuraEnabled
public static Boolean canEditObject(String objectApiName) {
  return Schema.getGlobalDescribe().get(objectApiName).getDescribe().isUpdateable();
}
1
  • Thanks for the help, when I run as the different user I am getting the expected Boolean value. Many thanks once again!
    – prem22
    Commented Jul 23 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .