Schema Metadata

Schema metadata is information about the Force.com database, available to your Apex code dynamically, at runtime. It has many potential uses, such as customizing the behavior of Apex code installed in multiple organizations, driving the construction of dynamic queries, or verifying that the database is configured in a certain way. This section describes the five types of schema metadata (object, field, child relationship, picklist, and record type) and includes code that can be run in the Execute Anonymous view to demonstrate accessing them.


Note

You are limited to a maximum of 100 calls to schema metadata methods. All five types of schema metadata methods contribute equally to the limit.


Object Metadata

Object metadata is information about the database objects in the Force.com organization. It includes custom as well as standard objects. Listing 5.30 provides an example of retrieving object metadata. The metadata of all objects in the database is retrieved, and their names and labels are printed to the debug log.

Listing 5.30 Retrieving Object Metadata


Map<String, Schema.SObjectType> objects = Schema.getGlobalDescribe();
Schema.DescribeSObjectResult objInfo = null;
for (Schema.SObjectType obj : objects.values()) {
  objInfo = obj.getDescribe();
  System.debug(objInfo.getName() + ' [' + objInfo.getLabel() + ']'),
}


Field Metadata

Field metadata provides access to all the attributes of fields you configure on a database object. Listing 5.31 demonstrates how to access field metadata. The fields of the Project__c object are retrieved, including standard and custom fields. The getDescribe method is invoked on each to return its metadata, a Schema.DescribeFieldResult object. The name, label, data type, precision, and scale of each field are displayed in the debug log.

Listing 5.31 Retrieving Field Metadata


Map<String, Schema.SObjectField> fields =
  Schema.SObjectType.Project__c.fields.getMap();
Schema.DescribeFieldResult fieldInfo = null;
for (Schema.SObjectField field : fields.values()) {
  fieldInfo = field.getDescribe();
  System.debug(fieldInfo.getName()
  + ' [' + fieldInfo.getLabel() + '] '
  + fieldInfo.getType().name()
  + '(' + fieldInfo.getPrecision()
  + ', ' + fieldInfo.getScale() + ')'),
}



Tip

If you do not know the type of an object, you can still retrieve its metadata using getSObjectType. For example, if a01i0000000rMq1 is the unique identifier of a Project record, the result of Id.valueOf('a01i0000000rMq1').getSObjectType() can replace Schema.SObjectType.Project__c in the second line of Listing 5.31.


Child Relationship Metadata

Child relationship metadata contains the child’s object type, the relationship name, and an object identifying the field in the child object that relates it to the parent. Listing 5.32 demonstrates the retrieval of child relationship metadata from the Contact object. Compare the results with what you see in the Force.com IDE’s Schema Explorer for the Contact object.

Listing 5.32 Retrieving Child Relationship Metadata


Schema.DescribeSObjectResult res = Contact.SObjectType.getDescribe();
List<Schema.ChildRelationship> relationships = res.getChildRelationships();
for (Schema.ChildRelationship relationship : relationships) {
  System.debug(relationship.getField() + ', ' + relationship.getChildSObject());
}


Picklist Metadata

Picklist metadata provides access to the master list of available picklist values for a picklist or multi-select picklist field. It does not include the assignments of picklist values to record types, nor does it provide any information about the relationship between picklist values in dependent picklists. Listing 5.33 is an example of its use, printing the picklist values of the Skill object’s Type field to the debug log.

Listing 5.33 Retrieving Picklist Metadata


Schema.DescribeFieldResult fieldInfo =
  Schema.SObjectType.Skill__c.fields.Type__c;
List<Schema.PicklistEntry> picklistValues = fieldInfo.getPicklistValues();
for (Schema.PicklistEntry picklistValue : picklistValues) {
  System.debug(picklistValue.getLabel());
}


Record Type Metadata

Record type metadata contains the names and unique identifiers of record types defined on an object. It also indicates the availability of the record type to the current user (isAvailable) and whether the record type is the default record type for the object (isDefaultRecordTypeMapping).

Listing 5.34 provides an example of using record type metadata. It retrieves the record types in the Contact object and prints their names to the debug log.

Listing 5.34 Retrieving Record Type Metadata


Schema.DescribeSObjectResult sobj = Contact.SObjectType.getDescribe();
List<Schema.RecordTypeInfo> recordTypes = sobj.getRecordTypeInfos();
for (Schema.RecordTypeInfo recordType : recordTypes) {
  System.debug(recordType.getName());
}


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.121.8