function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sunil BagulSunil Bagul 

Field label in Apex

i want to find the label for field and i am using this code to find that,it is returning me label but for Type picklist lable is Type but it is returning Account Type,any help just to get label as Type?

String type='Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(type);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

for (String fieldName: fieldMap.keySet()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
 
System.debug(fieldMap.get(fieldName).getDescribe().getLabel());//It provides to get the object fields label.
}
Magesh Mani YadavMagesh Mani Yadav
Hi Sunil,
I believe the standard type field always returns label as 'ObjectType Type' . I tried for Case and opportunity and it returns label as "Case Type"
 and 'Opportunity Type'.
if you want to override this then either you have to hardcode label as 'Type' If the filedname=='type' or you need to do string manipulation like this
 
String type='Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(type);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

for (String fieldName: fieldMap.keySet()) {
System.debug('##Field API Name='+fieldName);// list of all field API name
if(fieldName=='type'){
System.debug(fieldMap.get(fieldName).getDescribe().getLabel().removeStartIgnoreCase(type).trim());
}else{ 
System.debug(fieldMap.get(fieldName).getDescribe().getLabel());//It provides to get the object fields label.
}

}


 
Sunil BagulSunil Bagul
thanks,I can use manipulation ,this code is very specific to one object but in actual requirment i have to handle for all objects
but in fields section it shows label as Type only