In projects many time we need to check, user has access to read data or update data of any object’s field. We can check field access using UserFieldAccess object.
In this post, i have create one lightning web component to show object and it’s fields in combox box. Another combox box is added to show users in Salesforce Org. Based on input selected, it will show field access in lightning table.
If only user and object is selected then it will show user access of all fields. If user, object and field name is selected then it will show access of specific field.
To get field access below step need to follow
- Get DurableId from UserFieldAccess
- Get field access using above DurableId
- Showing access list in Lightning web component
1. Get DurableId from UserFieldAccess:
First we have to obtain the DurableId for fields. It is unique identifier for the field. We always have to check this field as it is changing in every release.
List<UserFieldAccess> lists=[SELECT Durableid FROM UserFieldAccess WHERE FieldDefinition.EntityDefinition.QualifiedApiName=:objectName AND User.Id=:userId];
2. Get field access using above DurableId
Now retrieve user access detail using above retrieved Durableid.
List<UserFieldAccess> accesses=[SELECT DurableId,EntityDefinitionId,FieldDefinition.QualifiedApiName,FieldDefinitionId,Id,IsAccessible,IsCreatable,IsUpdatable,UserId FROM UserFieldAccess WHERE DurableId =:ids];
Complete Apex code :
3. Showing Field Access in Lightning Table:
We have field access list using above apex code now. Let us show that access list in lightning datatable. Let us create new LWC (Lightning Web Component) to show list.
Below LWC component are used to show list
- combo box to show options
- lightning data table to show lis
Complete LWC Code