How to get All Date of Birth Fields in Salesforce?

How to get All Date of Birth Fields in Salesforce?

Sample Code:

Set < String > listsObjs = new Set < String > { 'Account', 'Lead' };      
Map < String, Schema.SObjectType > globalDescription = Schema.getGlobalDescribe();   

for ( String obj : listsObjs ) {  
    
    Schema.sObjectType objType = globalDescription.get( obj );  
    Schema.DescribeSObjectResult r1 = objType.getDescribe();   
    Map<String , Schema.SObjectField > mapFieldList = r1.fields.getMap();    
    
    for ( Schema.SObjectField field : mapFieldList.values() ) {    
        
        Schema.DescribeFieldResult fieldResult = field.getDescribe();    
        String fieldLabel = fieldResult.getLabel().toLowerCase();  
        Schema.DisplayType fielddataType = fieldResult.getType(); 
        
        if ( 
            ( fielddataType == Schema.DisplayType.Date || fielddataType == Schema.DisplayType.DateTime ) &&   
            ( fieldLabel.contains( 'dob' ) || fieldLabel.contains( 'date of birth' )   
             || fieldLabel.contains( 'dateofbirth' ) || fieldLabel.contains( 'birthday' ) )   
            && ! fieldResult.isCalculated() 
        ) {   
            
            System.debug( 'Field Label is ' + fieldLabel );            
            System.debug ( objType + '.' + fieldResult.getName() );  
            
        }  
        
    }  
    
}  

Output:

Leave a Reply