Thursday, 22 June 2017

How to get picklist values of a salesforce field

Picklists provide options to the user to select any value which is applicable for that field. As in the standard page layout picklist will provide those kind of options.

Sometimes we need to retrive the values of the picklist to display them on our visualforce pages to provide the user to select the applicable option, because some picklists restricts to enter some values those are not specified in its design.

To get the values of the picklist just use the below piece of code in your apex class,

public List<SelectOption> getPicklistOptions()
{
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult =
 Opportunity.stagename.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}


The above piece of code retrives the options of the picklist stagename of the standard opportunity object, just replace the name of your sObject and picklist to retrieve those values in your apex class.

To populate those values on visualforce page just place below code in your page,

<apex:selectList id="picklist1" value="{!Opportunity.stagename}"
         size="1" required="true">
  <apex:selectOptions value="{!PicklistOptions}"/>
</apex:selectList>


Where apex is the name of the default namespace used in visualforce page.

How to get the RecordTypeId with Apex code in Salesforce platform

Hello everyone,

As a salesforce developer i have faced some of the situations to reuse the same code to build the salesforce project. In this blog i want include some reusable piece of salesforce code in day to day life activities. 


while writing test classes some times we need to get the record type id of an object in that case just use the below code,

Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();

where
    Account is the name of the object,
    The String 'Development' is the name of the record type.

Just replace the object name with your sObject and put the name of your record type in place of the string 'Development'.

for example to get the recordtypeId of the Opportunity Object having the record type name 'VIP', the code will look like,

Id opprecordtypeId=Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('VIP').getRecordTypeId();