Using Field Tokens

To access the token for a field, use one of the following methods:

  • Access the static member variable name of an sObject static type, for example, Account.Name.
  • Call the getSObjectField method on a field describe result.

The field token uses the data type Schema.SObjectField.

In the following example, the field token is returned for the Account object's Description field:

Schema.SObjectField fieldToken = Account.Description;

In the following example, the field token is returned from the field describe result:

// Get the describe result for the Name field on the Account object
Schema.DescribeFieldResult dfr = Schema.sObjectType.Account.fields.Name;

// Verify that the field token is the token for the Name field on an Account object
System.assert(dfr.getSObjectField() == Account.Name);

// Get the describe result from the token
dfr = dfr.getSObjectField().getDescribe();

Using Field Describe Results

To access the describe result for a field, use one of the following methods:

  • Call the getDescribe method on a field token.
  • Access the fields member variable of an sObject token with a field member variable (such as Name, BillingCity, and so on.)

The field describe result uses the data type Schema.DescribeFieldResult.

The following example uses the getDescribe method:

Schema.DescribeFieldResult dfr = Account.Description.getDescribe();

This example uses the fields member variable method:

Schema.DescribeFieldResult dfr = Schema.SObjectType.Account.fields.Name;

In the example above, the system uses special parsing to validate that the final member variable (Name) is valid for the specified sObject at compile time. When the parser finds the fields member variable, it looks backwards to find the name of the sObject (Account) and validates that the field name following the fields member variable is legitimate. The fields member variable only works when used in this manner.

Note

Note

You should not use the fields member variable without also using either a field member variable name or the getMap method. For more information on getMap, see the next section.

For more information about the methods available with a field describe result, see DescribeFieldResult Class.

Accessing All Field Describe Results for an sObject

Use the field describe result's getMap method to return a map that represents the relationship between all the field names (keys) and the field tokens (values) for an sObject.

The following example generates a map that can be used to access a field by name:

Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
Note

Note

The value type of this map is not a field describe result. Using the describe results would take too many system resources. Instead, it is a map of tokens that you can use to find the appropriate field. After you determine the field, generate the describe result for it.

The map has the following characteristics:

  • It is dynamic, that is, it is generated at runtime on the fields for that sObject.
  • All field names are case insensitive.
  • The keys use namespaces as required.
  • The keys reflect whether the field is a custom object.

For example, if the code block that generates the map is in namespace N1, and a field is also in N1, the key in the map is represented as MyField__c. However, if the code block is in namespace N1, and the field is in namespace N2, the key is N2__MyField__c.

In addition, standard fields have no namespace prefix.

Field Describe Considerations

Note the following when describing fields.

  • A field describe that’s executed from within an installed managed package returns Chatter fields even if Chatter is not enabled in the installing organization. This is not true if the field describe is executed from a class that’s not within an installed managed package.
  • When you describe sObjects and their fields from within an Apex class, custom fields of new field types are returned regardless of the API version that the class is saved in. If a field type, such as the geolocation field type, is available only in a recent API version, components of a geolocation field are returned even if the class is saved in an earlier API version.
Previous
Next