Monday, 18 May 2015

Overview

The describing an object and all its component is possible with two ways API and APEX context. The following are the APEX context describe calls, which i found very useful.

Following are the different describe call that we would be using in real time. All these Calls are shown in flow and out come of one describe call is used in the other calls. 

Get ObjectType object in apex:

Way 1 :

Schema.sObjectType sobjType = sObject.getSObjectType()

Way 2 :

/*Get sObjectType object of all the object present in the organization. The resultant map (ObjectName -> SObjectType). This is very helpful. */

  Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();

/*Get the desired sObjectType from the resultant map. */

       Schema.sObjectType sobjType = globalDescription.get('objectAPIName');
  
Link :
                sObejct.
        Schema

Way 3 :

If we have the id of the record then,

   Id tempId='01I1b0000004KHK';

Schema.sObjectType sobjType tempId.getSObjectType(); 

Describe the sObject :


/*sobjType is just a token to get the details of the Object. We need to describe to  get details of the object.*/
Schema.DescribeSObjectResult sObjectDescResult = sobjType.getDescribe();


Link :
DescribeSObjectResult


Describe a  field details of the sObjectType :

/* Get the map of the containing token for all the fields of the Object.*/
        Map<String , Schema.SObjectField> mapFieldList = sObjectDescResult.fields.getMap();

      /*Get the required field's SObjectField  from the map of all fields.*/

  Schema.SObjectField sObjectfield = mapFieldList.get('fieldAPIName);

/*SObjectField is just a token to the Field. To get more details of the fields "describe the field".*/
Schema.DescribeFieldResult dfr= sObjectfield.getDescribe();

Links :
 DescribeFieldResult


Describe Related List of a object type


    List<Schema.ChildRelationship> clientRelations = sObjectDescResult.getChildRelationships();


/*Get the field which build this relationship ie.., field in the child records that bind with this parent. for example to get the Label of the field.*/
String fieldLabel= clientRelation.getField().getDescribe().getLabel();

/*Relationship Name  of the related List */
String relationshipName = clientRelation.getRelationshipName();

Link :
 ChildRelationship