I have an object called myObject__c and when I create a new field, I want to get the field's name on runtime in apex. How can we do this?
1 Answer
You can use Dynamic Apex.
For example:
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
for(Schema.SObjectField field : fieldMap.values()){
Schema.DescribeFieldResult fieldResult = field.getDescribe();
String label = fieldResult.getLabel();
System.debug('The field label is: ' + label);
}
Please see the Schema Namespace. If you'd like to access other field metadata, use a different method of the DescribeFieldResult class.