Pages

Friday, January 2, 2009

Visualforce Component to show Object Record Types

Happy New Year!

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
In many occasions especially when developing a new wizard you need to first allow the user select what type of record they want to create and then based on that show the correct type of interface to the user.

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>



The Component has a Controller called "RecordTypeListCon" and is named as "RecordTypeList". It declares two attributes as follow:

  • 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;
}
}




Once you create this component in your Salesforce instance all you need to do when need to the selectList of your object's record types is to:


<c:RecordTypeList value="{!lookupValue}" sObjectType="Account"></c:RecordTypeList>




Enjoy!

12 comments:

  1. Hi Sam, I'm New en Visualforce.

    I have a problem with the code posted. The next mistake. Property lookupValue unknown.

    That is what I have to add?

    Thank you.

    Mike.

    ReplyDelete
  2. We are also getting the same error with the unknown property 'lookupvalue'. Can u please send me the executable code.

    ReplyDelete
  3. Hey guys
    When 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.

    ReplyDelete
  4. Sam,

    Thanks for posting this, but the lookValue needs a better explanation.

    Specifically, how would this code work if showing accounts (per your example)?

    Thanks

    ReplyDelete
  5. Ok, so when you create the VF page, make sure this is declared at the top:



    Cheers

    ReplyDelete
  6. Sorry, can you help to see this, I can't use "<>" in type attribute. for example:



    because of type="List", I always got error like "Error: Missing required attribute type in at line 13 column 1".

    thanks a lot!

    ReplyDelete
  7. instead of using "List"use array type, for example if you passing object type "myclass", you can define your attribute as attr="myclass[]"

    ReplyDelete
  8. Above is not wotking.

    ReplyDelete
  9. Sam, how do you write test code for the custom component controller you have there?

    ReplyDelete
  10. Great component! Save me a lot of time. Thanks.

    ReplyDelete
  11. Can we call Action method on the page controller from the component, can you help me with this.

    ReplyDelete


  12. when I assign it says
    javax.faces.FacesException: core.apexpages.exceptions.ApexPagesHandledException: Cannot convert the value of '{!customeObjectList}' to the expected type

    ReplyDelete