Created
May 11, 2018 19:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
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