anusha(salesforce developer)

Tuesday 24 January 2017

Schema.DescribeSObjectResult in Apex and its usage in visualforce.

Schema.DescribeSObjectResult in Apex and its usage in visualforce.


This Describes SObject methods returns an array of DecribeSobjectResult objects. Where each object has the following properties.

String Name : This is the name of the object.

String Label :  Label text for the tab or an objct.

String LabelPlural : Label text for an object that represents plural version of object name.

String keyPrefix :  Object id's are prefixed with 3 character quotes that specify the type of the object.

Ex: - Account obj as 001, Opportunity object has 006.

When we call the keyPrefix it will return 3 character prefix code for the object which we are called.


Field[] fields: This will return array of the fields associated with an object.

Boolean Custom: Indicated whether the object is a custom object or not.

Boolean Creatable: Indicates whether the object can be created via created method.

Boolean deletable: Indicates whether the object can be deleted or not.

Boolean Mergable: Indicates whether the object can be merged with objects of its type.

Replicatable: Indicates whether the object can be replicatable via getUpdate function or getDeleted.

Boolean Retrievable : Indicates whether the object can be retrieved using retrieve method or not.

Boolean Searchable:- Indicates whether object can be searched via Search method .


Boolean updatable : Checks whether the object can be updatable or not.

To fetch the properties of an object

Schema.DescribeSobjectResult resultObj = Account.SobjectType.getDescribe();

                                              Or
Schema.DescribeSobjectResult result =  Schema.SobjectType.Account();

Example:

Field Example:

 Public class FieldExample{

   public String result {get;set;}

  Public FieldExample() {

 Schema.DescribeSobjectResult resultObj =          Account.SobjectType.getDescribe();
 result = ' ' +resultObj ;

}

}

In the above program 'resultObj' cibtaubs description about the object Account. If you want to know the properties individually we can use.

String getLabel();
String getKeyPrefix();
String getLabelPlural(); etc...



Child Relationship methods for given Object:


List <Schema.ChilRelationship> getChildRelationShips(); 

If an sobject is a parent object we can access the child relationships as well as the child objects using ChildRelationShip object.  This method returns the list of child objects for the given Sobject.


List of Child Records:

Program to display the List of Child objects for a given object

EX:-
public class ChildRelationshipExample {

public List<SelectOption> options;

public List<SelectOption> getOptions(){
 return options;
}

public ChildRelationshipExample(){

options = new List<SelectOption>();
Schema.DescribeSobjectResult r = Account.SobjectType.getDescribe();
List<Schema.childRelationship> c = r.getChildRelationShips();

for(schema.childRelationship x:c){
 String name = ' '+x.getChildSObject();
 SelectOption op = new SelectOption(name,name);
 options.add(op);
}

}


}

VF Page:

<apex:page controller="ChildRelationshipExample">
  <apex:form >
   <apex:selectlist size="1">
     <apex:selectoptions value="{!options}"></apex:selectoptions>
   </apex:selectlist>
  </apex:form>
</apex:page>



No comments:

Post a Comment