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
WilmerWilmer 

how to validate a field type in Apexcode?

Hi, I´d like to know if there´s a way to check by using ApexCode the field type. i.e: we want to know if the requested field value in a dynamic query is a number, string, boolean, date, datetime, etc.

Select field1__c from MyCustomObject__c

so in this example, I'd like to know what field type is field1__c.

or, lets suppose we got different kinds of values into a string field (only for customization convenience) and then, I query that field and want to establish if that value is a number, date, datetime, etc only by using ApexCode. I didn´t find any IsNumber or IsBoolean methods in Apex documentation.

Thanks,

Wilmer
Best Answer chosen by Admin (Salesforce Developers) 
mavsmavs

Hey try this..

 

After a long research i found this and is working perfect

 

Look at Dynamic APEX in the manual

 

 

Map<String, Schema.sObjectField> M = Schema.SObjectType.Account.fields.getMap();

schema.DescribeFieldResult F = m.get(Field API Name).getDescribe();

String fieldtype = F.getType().name();

All Answers

WilmerWilmer
Well, no one replied this case. Now I consider it is possible by using Dinamic SOQL and DescribeObject in ApexCode.
mavsmavs

Hey try this..

 

After a long research i found this and is working perfect

 

Look at Dynamic APEX in the manual

 

 

Map<String, Schema.sObjectField> M = Schema.SObjectType.Account.fields.getMap();

schema.DescribeFieldResult F = m.get(Field API Name).getDescribe();

String fieldtype = F.getType().name();

This was selected as the best answer