Retrieve Field Permissions for All Fields in an Object
public with sharing class FieldPermissionsChecker {
@AuraEnabled
public static Map getFieldPermissions(String objectName) {
Map fieldAccessMap = new Map();
// Get object schema
Schema.SObjectType sObjectType = Schema.getGlobalDescribe().get(objectName);
if (sObjectType == null) {
throw new AuraHandledException('Object not found: ' + objectName);
}
// Get field map and iterate through each field
Map fields = sObjectType.getDescribe().fields.getMap();
for (String fieldName : fields.keySet()) {
Schema.DescribeFieldResult fieldDescribe = fields.get(fieldName).getDescribe();
// Determine access level: "edit" if updateable, "read" if only accessible
if (fieldDescribe.isUpdateable()) {
fieldAccessMap.put(fieldName, 'edit');
} else if (fieldDescribe.isAccessible()) {
fieldAccessMap.put(fieldName, 'read');
}
}
return fieldAccessMap;
}
}
import { LightningElement, api, track, wire } from 'lwc';
import getFieldPermissions from '@salesforce/apex/FieldPermissionsChecker.getFieldPermissions';
export default class FieldPermissionsLWC extends LightningElement {
@api objectName; // e.g., 'Account'
@track fieldPermissionsMap = {};
connectedCallback() {
this.loadFieldPermissions();
}
loadFieldPermissions() {
getFieldPermissions({ objectName: this.objectName })
.then(result => {
this.fieldPermissionsMap = result;
console.log('Field Permissions:', this.fieldPermissionsMap);
})
.catch(error => {
console.error('Error fetching field permissions:', error);
});
}
isFieldEditable(fieldName) {
return this.fieldPermissionsMap[fieldName] === 'edit';
}
isFieldReadable(fieldName) {
return this.fieldPermissionsMap[fieldName] === 'read' || this.isFieldEditable(fieldName);
}
}
Retrieve Field Permissions for All Fields in an Object
Reviewed by dasfrogpractice
on
05:04
Rating:
No comments: