Time definitely flies and I wish you wonderful times in 2009!
Part of our daily job is to make life easier for others by developing new applications in force.com platform. But sometimes it's not bad to spend some time for ourselves to make our own life a little easier, better and smoother.
In this article I will present a Visualforce Component that would list the record types of an Object in the platform.
Imagine, the Account object has two record types in the force.com platform (Record Types are created by the users based on what these objects represent on their business).
Account Record types:
- Customer
- Partner
The solution as to how you can show this to the user is rather simple, but here I actually took the time to create a re-usable component, so you and I won't need to rewrite the code next time!
Here is the Component's Tags:
<apex:component controller="RecordTypeListCon">
<apex:attribute name="sObjectType" description="" type="String" required="true" default="Account" assignTo="{!sObjectType}"></apex:attribute>
<apex:attribute name="value" description="" type="String" required="true"></apex:attribute>
<apex:selectList value="{!value}" size="1">
<apex:selectOptions value="{!items}"></apex:selectOptions>
</apex:selectList>
</apex:component>
- sObjectType: values such as "Account", "Contact", generally the name of your object.
- value: you can capture the result of user's selection by using the attribute in your Vsualforce Controller.
And here goes the code of the component's Controller:
public class RecordTypeListCon {
private List<SelectOption> items;
// property that reads the value from the Component attribute
public string sObjectType
{
get;
set;
}
public List<SelectOption> getItems() {
List<SelectOption> items = new List<SelectOption>();
//default value
items.add(new SelectOption('','--Select Record Type --'));
//query force.com database to get the record type of the requested object.
for(RecordType rt: [select id,name from recordtype where sobjecttype=:sObjectType]) {
items.add(new SelectOption(rt.id,rt.name));
}
return items;
}
}
<c:RecordTypeList value="{!lookupValue}" sObjectType="Account"></c:RecordTypeList>
Enjoy!