No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
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 AccountNumber field:
1Schema.SObjectField F = Account.AccountNumber;In the following example, the field token is returned from the field describe result:
1// Get the describe result for the Name field on the Account object
2Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields.Name;
3
4// Verify that the field token is the token for the Name field on an Account object
5System.assert(f.getSObjectField() == Account.Name);
6
7// Get the describe result from the token
8f = f.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:
1Schema.DescribeFieldResult F = Account.AccountNumber.getDescribe();This example uses the fields member variable method:
1Schema.DescribeFieldResult F = 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.
You can only have 100 fields member variable statements in an Apex class or trigger.
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:
1Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();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.