Advantage of describe calls

    1. Instead of hard coding the picklist values in the Apex coding we can make use of describe call to retrieve the picklist values with the below code.
public List<SelectOption> getAccountType()
{
	List<SelectOption> options = new List<SelectOption>();
	options.add(new SelectOption('','--None--'));
	Schema.DescribeFieldResult fieldResult = account.Type.getDescribe();
	List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
	for( Schema.PicklistEntry f : ple)
		options.add(new SelectOption(f.getLabel(), f.getValue()));
	return options;            
}
    Tip: If you wanted to restrict the values as per your recordtype then you have to assign a recordtypeid to that sobject
    2. When we wanted to retrieve the recordtypes in salesforce what we tend to do is query the recordtype sobject. Instead of querying we can take advantage of describe calls. Here is a small snippet for retrieving the recordtypes.
// Snippet for retrieving the recordtypes in an Opportunity object
Schema.SObjectType.Opportunity.getRecordTypeInfosByName(); 
//Snippet for retrieving the recordtypeid of the specified recordtype name
Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RecordtypeName').getRecordTypeId(); 
Advertisements
This entry was posted in Apex, Salesforce Tips & Tricks, Visualforce. Bookmark the permalink.

One Response to Advantage of describe calls

  1. Reddy says:

    Thanks Srini for your wonderful tips….
    Kandi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s