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
Anas AlamourAnas Alamour 

SOQL query to fetch new fields created this year with data types

I have requirment to run query to fetch new created fields this year in my org with the data type for each field. I run the following query , but the data type is not uncluded in the result : 
SELECT id, DeveloperName, TableEnumOrId ,CreatedDate , CreatedById FROM CustomField WHERE CreatedDate = this_Year  order by CreatedDate DESC 

Any idea how to fetch the field data type in single query or using Apex code ? 

Thanks.
VinayVinay (Salesforce Developers) 
Hi Anas,

You can get the all the standard and custom objects fields data types using the getGlobalDescribe, getDescribe, getType.
 
String objType=’Account’;
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType leadSchema = schemaMap.get(objType);
Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

for (String fieldName: fieldMap.keySet()) {

    //get all the fields label for Account Object
    String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();

    //get data types for each fields
    Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
    if(fielddataType == Schema.DisplayType.TextArea) {
        /* build your logic if the Field data type is TextArea */
    }

    if(fielddataType == Schema.DisplayType.String) {
        /* build your logic if the Field data type is String */
    }

    if(fielddataType == Schema.DisplayType.Integer) {
        /* build your logic if the Field data type is Integer */
    }

    if(fielddataType == Schema.DisplayType.DateTime) {
        /* build your logic if the Field data type is DateTime */
    }

}

Reference:
https://salesforce.stackexchange.com/questions/15619/soql-query-by-data-type
https://salesforce.stackexchange.com/questions/264194/fetch-all-fields-whose-data-type-is-phone

Please mark as Best Answer if above information was helpful.

Thanks,​​​​​​
Anas AlamourAnas Alamour
Hi Vinay, 

Thank you for your response on my question. Based on your provided apex code, i could write the follwing apex code and get all the custom picklist fields for all objects ( standard and custom ) : 
Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map <String,Schema.SObjectField> fieldMap;
for (String key: schemaMap.keyset()){
system.debug('The Object Name ='+ schemaMap.get(key));
fieldMap = schemaMap.get(key).getDescribe().fields.getMap();
for (String fieldName : fieldMap.keySet()) {
    DescribeFieldResult dfr = fieldMap.get(fieldName).getDescribe();
    if (dfr.getType() == Schema.DisplayType.picklist && dfr.isCustom()) 
    {
        System.debug('The Custom Picklist field Name ='+fieldName);
    }
}
}

Just my remaining challange now is finding those fields created this year. I didn't find any method in DescribeFieldResult Class that returns the Createddate or LastModifieddate : 

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_fields_describe.htm
https://ideas.salesforce.com/s/idea/a0B8W00000GdbhrUAB/createddate-and-lastmodifieddate-fields-in-apex-describe-methods 

Please advise. 

Thanks.