Skip to content

Instantly share code, notes, and snippets.

@danic85
Last active August 7, 2017 08:36
Show Gist options
  • Save danic85/a5519c6591bce94cf4e7c7064506c50b to your computer and use it in GitHub Desktop.
Save danic85/a5519c6591bce94cf4e7c7064506c50b to your computer and use it in GitHub Desktop.
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