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!
Hi Sam, I'm New en Visualforce.
ReplyDeleteI have a problem with the code posted. The next mistake. Property lookupValue unknown.
That is what I have to add?
Thank you.
Mike.
We are also getting the same error with the unknown property 'lookupvalue'. Can u please send me the executable code.
ReplyDeleteHey guys
ReplyDeleteWhen trying to add the component to Visualforce page, I see that some readers are confused about the "lookupvalue" in the sample above.
You should define a property in your own Visualforce page controller to capture the user's selection, I called that property "lookupValue".
Therefore, lookupValue is part of your code.
A note: the name of the property is entirely your decision, "lookupValue" is only a preference on my code.
Sam,
ReplyDeleteThanks for posting this, but the lookValue needs a better explanation.
Specifically, how would this code work if showing accounts (per your example)?
Thanks
Ok, so when you create the VF page, make sure this is declared at the top:
ReplyDeleteCheers
Sorry, can you help to see this, I can't use "<>" in type attribute. for example:
ReplyDeletebecause of type="List", I always got error like "Error: Missing required attribute type in at line 13 column 1".
thanks a lot!
instead of using "List"use array type, for example if you passing object type "myclass", you can define your attribute as attr="myclass[]"
ReplyDeleteAbove is not wotking.
ReplyDeleteSam, how do you write test code for the custom component controller you have there?
ReplyDeleteGreat component! Save me a lot of time. Thanks.
ReplyDeleteCan we call Action method on the page controller from the component, can you help me with this.
ReplyDelete
ReplyDeletewhen I assign it says
javax.faces.FacesException: core.apexpages.exceptions.ApexPagesHandledException: Cannot convert the value of '{!customeObjectList}' to the expected type