Showing posts with label Dynamic Apex. Show all posts
Showing posts with label Dynamic Apex. Show all posts

Sunday, April 5, 2015

Get the Object Type by using sObject Describe call in Apex

public AccountControllerExtension(ApexPages.StandardController stdController){

// Get the record of sObject (e.g. Account, Contact etc.) by using the method "getRecord()" of StandardController
    sObject sObjectRecord = stdController.getRecord();

// Get the object type specifically by using Apex sObject Describe call
// In this case it will be Account
    String objectType = sObjectRecord.getSObjectType().getDescribe().getName();  

// Verify the Object Type
System.Debug('>>Object Type<<'+objectType);

}

Get the Picklist Values by using sObject Describe call in Apex

// Contains all Account.Industry values
Set<String> IndustryValues = new Set<String>();

// Get Account.Industry values from the field on Account object
Schema.DescribeFieldResult accountIndustry = Account.Industry.getDescribe();

List<Schema.PicklistEntry> accountIndustryValues = accountIndustry.getPicklistValues();        
for(Schema.PicklistEntry industryValue: accountIndustryValues){
IndustryValues.add(industryValue.getValue());
}

System.Debug('>>Account Industry Values<<'+IndustryValues);

Result should be as follows

>>Account Industry Values<<{Agriculture, Apparel, Banking, Biotechnology, Chemicals, Communications, Construction, Consulting, Education, Electronics, ...}