Is it possible to query fields by datatype? I need to query all email fields from different objects. Email field names are different on each object and not known in advance. So I am trying to query by using email datatype.

  • 1
    It's not clear what you are asking. Can you post what code you have got so far and what the problem with it is? Are you trying to query data from objects, or metadata about which fields are email fields?
    – Doug B
    Commented Aug 19, 2013 at 11:36
  • Not yet started. I need to get the email field values from different objects. All are custom fields. Each email field having different name. For example: Email_ID__c in one custom object, Email__c in another custom object and so on. My requirement is get the values from Email_ID__c field and Email__c field using single query. Is it possible ?? Or else any other way avail ?? Commented Aug 19, 2013 at 11:53
  • If you want to query data out of multiple unrelated objects in a single query then the answer which ForceDeveloper provided is correct, you use SOSL.
    – Doug B
    Commented Aug 19, 2013 at 11:55

3 Answers 3


Instead of SOQL you can use SOSL to achieve the same, although there are some other criteria too look into.

Ex : Find {[email protected]} in EMAIL FIELDS


  • This is not my requirement. I need to display all email field from more than 25 objects. Email field names are different one. Can we achieve using email datatype ??? Commented Aug 19, 2013 at 7:23

Yes, this is possible with using dynamic Apex. The steps you would take are:

  1. Call getGlobalDescribe for your sObject
  2. Get your object's field mapping
  3. Get the field's type

Here's some basic sample code:

// Get your sObjectType
sObjectType myObjectType = Schema.getGlobalDescribe().get([object name]);

// Get the list of fields
List<Schema.sObjectField> fields = myObjectType.getDescribe().fields.getMap().values();

// Iterate through and check the field types
for(sObjectField> current:fields) {
  if(current.getDescribe().Type == DisplayType.EMAIL) {
    // do something

we 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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .