Sobject
: Any one the objects created in the salesforce to store the data using
declarative format is called subject
Ex:
Account ,Customer__c, Lead,Contact
Account a; //a is instance of sobject Account
Contact
con;// con is instance of subject Contact
IF
we want to know instace belongs to which object
we can use method getSobjectType();
Public Sobject getSobjectType() : This method will
specify instace of subject belongs to which subject.
Ex:
Account a=new
Account() ;
System.debug(a.getSobjectType()) ;// Account
Contact c=new Contact();
System.debug(c.getSobjectType()) ; //contact
Account.SobjectType
will specify that it is an instance of subject Account.
Account.SobjectType a;
System.debug(a.getSobjectType()) ;//
Account;
Class :Schema.SobjectType ;
Schema
.SobjectType : This will specify the subject type we are referring to
1.getDescribe()
: This method will return the description about subject on which we have called
Synatax
: public
Schema.DescribeSobjectResult
getDescribe()
Ex
: Schema.DescribeSobjectResult
result=Account.SobjectType.getDescribe();
2.newSobject()
: This will construt new subject of the type we have specified
Public Sobject newSobject()
Ex: Account a=Account.SobjectType.newSobject();
Sobject s=Contact.SobjectType.newSobject();
3.newSobjectId(Id)
This
method is used to create new subject with referring to the existing record
( just like clone of record)
When
you write a soql query we are getting
the reference to the existing records
Ex: Account
a=[select id ,name ,Inudstry from Account where name=’sam’ limit 1];
a.name,a.industry
Simillarly we can create a reference
of subject using the schema;
Account a=new Account(Name=’aaa’);
Insert a;
Account acc=Account.SobjectType(a.id); / /this
will create a new Account object similar
to
Subject
a ;
4.newSobject(Id,Boolean)
:
This
will create a new subject with given recordType and default values
Public
Sobject newSobject(id recordTypeId, Boolean default)
getGlobalDescribe()
: This method is defined in the schema
namespace which is used to fetch the all the subjects with their corresponding
name in the form of map
Syntax
:public Map<String,Schema.SobjectType>
getGlobalDescribe()
Map<String,Schema.SobjectType>
mymap ;
Where
key is string which is the name of the
subject.
Value
is Schema.SobjectType which will store the sobject Type
Map<String,Schema.SobjectType> objMap=schema.getGlobalDescribe();
Fetch
all the objects names from Map
Set<String>
objectNames=objMap.keySet();
Q:
How to get the description about the subject.
Ex
1:
Schema.SobjectType
s=Account.sobjectType;
Schema.DescribeSobjectResult
result=s.getDescribe();
Ex
2:
Schema.DescribeSobjectResult
result=Contact.SobjectType.getDescribe();
Scneario
: Create a picklist field in the vf page with list of all the subjects in your
org and when you select the subject
corresponding description of the object should be displayed


Apex
Class :
public
class SchemaExample {
public Map<String,Schema.SObjectType>
objMap {set;get;}
public List<SelectOption> options
{set;get;}
public String selected {set;get;}
public String result {Set;get;}
public SchemaExample(){
objMap=Schema.getGlobalDescribe();
Set<String> keys=objMap.keySet();
options=new List<SelectOption>();
for(String s:keys){
SelectOption op=new
SelectOption(s,s);
options.add(op);
}
}
public
void getDesc(){
Schema.SobjectType
s=objMap.get(selected);
Schema.DescribeSobjectResult
res=s.getDescribe();
result=''+res;
}
}
VF
page:
<apex:page
controller="SchemaExample">
<apex:form>
<apex:selectList
value="{!selected}" size="1">
<apex:selectOptions
value="{!options}" />
<apex:actionSupport event="onchange"
action="{!getDesc}" reRender="one"/>
</apex:selectList>
<apex:outputLabel
value="{!result}" id="one"/>
</apex:form>
</apex:page>
How
to fetch list of fields from the object
1.Get
the description of the subject
Schema.DescribeSobjectResult result=Account.SobjectType.getDescribe();
Map<String,Schema.SobjectField> fldMap=result.fields.getMap();
This
map contains key as field name and Value as field
Scenario
: Create a piclist which will display list of customobjects as picklist option
when we select any one of the object it has to display list of corresponding
fields in the object .


public
class SchemaExampl2 {
public Map<String,Schema.SObjectType>
objMap{set;get;}
public
Map<String,Schema.SObjectField> fldMap{Set;get;}
public List<selectOption>
objoptions{set;get;}
public List<SelectOption>
fldoptions{set;get;}
public String selected {set;get;}
public SchemaExampl2(){
objMap=schema.getGlobalDescribe();
Set<String> keys=objMap.keySet();
objoptions=new
List<selectOption>();
fldoptions=new
List<Selectoption>();
fldoptions=new
List<selectOption>();
selectOption s1=new
SelectOption('none','-none-');
fldoptions.add(s1);
for(string s:keys){
Schema.DescribeSobjectResult
res=objMap.get(s).getDescribe();
if(res.isCustom()){
SelectOption
op=new SelectOption(s,s);
objoptions.add(op);
}
}
}
public void getDesc(){
Schema.DescribeSobjectResult
res=objMap.get(selected).getDescribe();
fldMap=res.fields.getMap();
for(String s:fldMap.keySet()){
SelectOption op=new SelectOption(s,s);
fldoptions.add(op);
}
}
}
<apex:page
controller="SchemaExampl2" >
<apex:form>
<apex:pageBlock
title="Schema">
<apex:pageBlockSection
title="SchemaExample">
<apex:selectList
value="{!selected}" size="1">
<apex:selectOptions
value="{!objOptions}" />
<apex:actionSupport
event="onchange" action="{!getDesc}"
reRender="one"/>
</apex:selectList>
<apex:selectList
size="1" id="one">
<apex:selectOptions
value="{!fldoptions}" />
</apex:selectList>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
How
to create a new picklist field referring to the selected picklist field from
theobject.
Class
:
public
class SchemaExample3 {
public List<SelectOption> options
{set;get;}
public String fldType{Set;get;}
public SchemaExample3(){
options=new
List<selectOption>();
Schema.DescribeFieldResult
result=Account.Industry.getDescribe();
fldType=''+result.getType();
List<Schema.PicklistEntry>
opt=result.getPicklistValues();
for(Schema.PicklistEntry
p:opt){
SelectOption
op=new SelectOption(p.getValue(),p.getLabel());
options.add(op);
}
}
}
<apex:page
controller="SchemaExample3" >
<apex:form>{!fldType}
<apex:selectList
size="1">
<apex:selectOptions
value="{!options}" />
</apex:selectList>
</apex:form>
</apex:page>
No comments:
Post a Comment