Wednesday, 13 November 2013

Dynamic Apex: Get Field Type

Dynamic Apex allows users to access the metadata information about sObject and fields.
The requirement was to get the field type of a sObject.

Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.Name;

system.debug('## Field Type: '+f.getSObjectField().getDescribe().getType());
//Returns: ## Field Type: STRING

Example of other available methods that can be accessed from f.getSObjectField().getDescribe()
  • getByteLength=765;
  • getCalculatedFormula=null;
  • getController=null;
  • getDefaultValue=null;
  • getDefaultValueFormula=null;
  • getDigits=0;
  • getInlineHelpText=null;
  • getLabel=Account Name;
  • getLength=255;
  • getLocalName=Name;
  • getName=Name;
  • getPrecision=0;
  • getRelationshipName=null;
  • getRelationshipOrder=null;
  • getScale=0;
  • getSoapType=STRING;
  • getSobjectField=Name;
  • getType=STRING;
  • isAccessible=true;
  • isAutoNumber=false;
  • isCalculated=false;
  • isCascadeDelete=false;
  • isCaseSensitive=false;
  • isCreateable=true;
  • isCustom=false;
  • isDefaultedOnCreate=false;
  • isDependentPicklist=false;
  • isDeprecatedAndHidden=false;
  • isDisplayLocationInDecimal=false;
  • isExternalId=false;
  • isFilterable=true;
  • isGroupable=true;
  • isHtmlFormatted=false;
  • isIdLookup=false;
  • isNameField=true;
  • isNamePointing=false;
  • isNillable=true;
  • isPermissionable=false;
  • isRestrictedDelete=false;
  • isSortable=true;
  • isUnique=false;
  • isUpdateable=true;
  • isWriteRequiresMasterRead=false;

Refer to salesforce documentation for further information 

No comments:

Post a Comment