How to find Date fields in the objects in Salesforce?

How to find Date fields in the objects in Salesforce?

In the following sample code, I have searched Date data type fields in Account, Lead and Opportunity objects.

Sample Code:

Set < String > listsObjs = new Set < String > { 'Account', 'Lead', '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();    
        String fieldLabel = fieldResult.getLabel().toLowerCase();  
        Schema.DisplayType fielddataType = fieldResult.getType();
        
        if ( fielddataType == Schema.DisplayType.Date ) {   
            
            System.debug( 'Field Label is ' + fieldLabel );            
            System.debug ( objType + '.' + fieldResult.getName() );  
            
        }  
        
    }  
    
}   

Output:

Leave a Reply