+ Start a Discussion
ManidManid 

How to use the schema.displaytype? can anyone explain simple example use all methods

Best Answer chosen by Manid
Amit Chaudhary 8Amit Chaudhary 8
DisplayType Enum (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_enum_Schema_DisplayType.htm)
A Schema.DisplayType enum value is returned by the field describe result's getType method

Here is sample code for same
How to get field data types for each fields in Salesforce objects
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
}

}


Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
DisplayType Enum (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_enum_Schema_DisplayType.htm)
A Schema.DisplayType enum value is returned by the field describe result's getType method

Here is sample code for same
How to get field data types for each fields in Salesforce objects
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
}

}


Let us know if this will help you
 
This was selected as the best answer
Siddharth Birari 6Siddharth Birari 6
@Amit, Thanks for the code snippet. However, there's this one minor correction. In the if statements, instead of "!=", it should be "==".
Thanks again.
Linga_RaminLinga_Ramin
How to fetch all the Lookup fields of an Object(Account) using Schema.DisplayType?
piyush kumar 70piyush kumar 70
Hi @Linga_Ramin,

You should have tried.
Schema.DisplayType.Reference
 
Jonathan Sandoval 1Jonathan Sandoval 1

Best response saved me a lot of extra thinking, though I approached the problem using Switch cases, which shouldn't be a big of a difference, here is my snippet:

 

public static String castTypeToString(Schema.DisplayType inputType){
        System.debug(inputType);
        switch on inputType{
            when STRING{
                return 'STRING';
            }
            when BOOLEAN{
                return 'CHECKBOX';
            }
            when PICKLIST{
                return 'PICKLIST';
            }
            When INTEGER{
                return 'INTEGER';
            }
            When DOUBLE{
                return 'DOUBLE';
            }
            When PERCENT{
                return 'PERCENT';
            }
            When ID{
                return 'ID';
            }
            When DATE{
                return 'DATE';
            }
            When DATETIME{
                return 'DATETIME';
            }
            When TIME{
                return 'TIME';
            }
            When URL{
                return 'URL';
            }
            When EMAIL{
                return 'EMAIL';
            }
            When PHONE{
                return 'PHONE';
            }
            When LONG{
                return 'LONG';
            }
            When MULTIPICKLIST{
                return 'MULTIPICKLIST';
            }
            When ENCRYPTEDSTRING{
                return 'ENCRYPTEDSTRING';
            }
            when else{
                return '';
            }
        }
    }