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
}
}
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
}
}
@Amit, Thanks for the code snippet. However, there's this one minor correction. In the if statements, instead of "!=", it should be "==". Thanks again.
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 '';
}
}
}
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
Let us know if this will help you
All Answers
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
Let us know if this will help you
Thanks again.
You should have tried.
Schema.DisplayType.Reference
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: