How To Find Date Fields From Any Objects Using Apex In Salesforce?
In this blog, you will learn how you can find date fields from any objects using apex in salesforce.
Follow below simple steps
- Go to Setup
- Open Developer Console
- Go To Debug menu
- Select Open Execute Anonymous Window from dropdown
In the below sample code, I am searching Date datatype fields in Account, Contact and Opportunity objects.
Set <String> listsObjs = new Set <String> {'Account', 'Contact', 'Opportunity'};
Map <String, Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
for (String obj : listsObjs){
Schema.sObjectType objType = globalDescription.get( obj );
Schema.DescribeSObjectResult objResult = objType.getDescribe();
Map<String , Schema.SObjectField> mapFieldList = objResult.fields.getMap();
for (Schema.SObjectField objField : mapFieldList.values()){
Schema.DescribeFieldResult fieldResult = objField.getDescribe();
Schema.DisplayType fielddataType = fieldResult.getType();
if (fielddataType == Schema.DisplayType.Date){
System.debug('Field Label : ' + fieldResult.getLabel() + ' , Field Name : '+ objType + '.' + fieldResult.getName());
}
}
}
- Click on Execute
Demo
Follow Us