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
dipu2dipu2 

How do we get the field type of a named field?

Is there a shorter way to get the field type of a named field. Field name is given to me. I want to know if it is a reference field.

String fieldName = 'BaseProductId__c';
System.assert(Schema.DisplayType.REFERENCE == Product2.sObjectType.getDescribe().fields.getMap().get(fieldName).getDescribe().getType());

 

Navatar_DbSupNavatar_DbSup

Hi,

     We can only use describe sobject to get field data type.i dont think there is another way around.below is the sample code to get field data type in generic.

 

public Map<String, Schema.SObjectField> LeadsFieldNames{get;set;}
LeadsFieldNames = Product2.getSObjectType().getDescribe().fields.getMap();
for(Schema.SObjectField sfield : LeadsFieldNames.Values())
{
    schema.describefieldresult dfield = sfield.getDescribe();
    string ftype=dfield.getType();
}

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.