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
lightdreamlightdream 

Determining a field Data type

Hi Salesforce,

 

I'd like to be able to determine the datatype of a field name dynamic when user Passes  the field Name. i.e., when user selects AccountNumber then we need to get the data type as Text, and when he again selects IsPartner on Account object then we need to get CheckBox as data type.

 

I think we can achive this with Describe Fields on the object and then match the name listed with a Map of the DescribeFieldResults.

 

As i am new to coding can any one give me the approch for this scenario with a sample code.

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
Avi646Avi646

You can use the following code to determine the field type for a particualr object say Account

private string returnFieldType(String apiName){
	Map<String, Schema.SObjectField> fieldDescribeMap = Schema.SObjectType.Account.fields.getMap();
	return fieldDescribeMap.get(apiName).getDescribe().getType()+'';
}

 I guess this function will help you out :)

 

 

All Answers

Avi646Avi646

You can use the following code to determine the field type for a particualr object say Account

private string returnFieldType(String apiName){
	Map<String, Schema.SObjectField> fieldDescribeMap = Schema.SObjectType.Account.fields.getMap();
	return fieldDescribeMap.get(apiName).getDescribe().getType()+'';
}

 I guess this function will help you out :)

 

 

This was selected as the best answer
lightdreamlightdream

Hi

 

First of all thanks for the reply. And this code has returned me with an Error

 

Error: Compile Error: unexpected token: 'String' at line 1 column 8

Thank You.

Avi646Avi646

The code is working fine. i guess you forgot to notice that i gave you a method that has to be called to get the type of the field.