Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lightning FLS. Throw exception if any field is not accessible.
//https://developer.salesforce.com/page/Lightning_Security#Access_Control_in_Apex_Controllers_and_Supporting_Classes
List<String> fields = new List<string>(); // Add your fields to check here
String objectName = ''; //Add your object name here
Map<String,Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap()
for (String fieldToCheck : fields) {
// Check if the user has access to view field
if (!fieldMap.get(fieldToCheck).getDescribe().isAccessible()) {
//also pass error to client
throw new System.NoAccessException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment