Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paulroth3d/99c7ead780d625ffd28d00efc604db83 to your computer and use it in GitHub Desktop.
Save paulroth3d/99c7ead780d625ffd28d00efc604db83 to your computer and use it in GitHub Desktop.
The following is a simple way to get all field definitions for a single object
....
public with sharing class ExampleController {
@AuraEnabled
public static Map<String,CustomFieldDefinition> getOpportunityDescribes(){
//-- Through deep binding, it is possible to retrieve the field limitations from apex
//-- and leverage within lightning components.
//-- However, it is often recommended to optimize them to a
SObjectType accountType = Schema.getGlobalDescribe().get('Account');
Map<String,Schema.SObjectField> mfields = accountType.getDescribe().fields.getMap();
Map<String,CustomFieldDefinition> results = new Map<String,CustomFieldDefinition>();
//-- iterate through all mfields and assign to results
return results;
}
class CustomFieldDefinition {
//-- ensure that @AuraEnabled is added to each item to return
@AuraEnabled
public String fieldName;
@AuraEnabled
public Integer maxLength;
public CustomFieldDefinition(Schema.SObjectField fieldDescription){
SObject.DescribeFieldResult describe = fieldDescription.getDescribe();
this.fieldName = describe.getName();
this.maxLength = describe.getLength();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment