wrapper class and force.com sites

Posted by in apex, salesforce, visualforce

Force.com Sites was announced last week at Dreamforce ’08 and what a game changing announcement it was.  Here’s my foray into Sites development, with nifty sample code (please note the table of opportunity data below is a live Salesforce.com Visualforce page from my development environment embedded into this post):

 
Controller

public class OpportunityList_Controller {
	public class MyOpportunity { 
	    Opportunity o;
	    public MyOpportunity(Opportunity opp) { o = opp; }
	    public Opportunity getOpportunity(){ return o; }
	    	    
	    public String getColor()  { 
		    if (o.Probability < 25) return '#2A73F0';
		    else if (o.Probability >= 25 &amp;amp;&amp;amp; o.Probability <= 75) return '#E0B60D';
		    else if (o.Probability > 75) return '#FF0000';
		    else return '#000000';
	    }
    }
    
    public List<MyOpportunity> results = new List<MyOpportunity>{};

	public List<MyOpportunity> getOpportunities() {
		results.clear();
        for (Opportunity o : [Select Id, Name, StageName, Closedate, Probability 
From Opportunity where StageName != 'Closed Won' order by Name limit 20]) {
        	results.add(new MyOpportunity(o));	
        }
		return results;
    }
}

Visualforce Page

<apex:page controller="OpportunityList_Controller" 
tabStyle="Opportunity" showHeader="false" sidebar="false" >
	<apex:form >  
		<apex:pageBlock title="" id="pageBlock">
			<apex:pageMessages ></apex:pageMessages>
			<apex:pageBlockTable value="{!opportunities}" var="o" 
rendered="{!NOT(ISNULL(opportunities))}">
				<apex:column >
					<apex:facet name="header">Opportunity Name</apex:facet>
					<div style="color:{!o.color};font-weight:bold;text-decoration:underline;cursor:hand;cursor:pointer;">
					{!o.Opportunity.Name}
					</div> 
				</apex:column>
				<apex:column value="{!o.Opportunity.Probability}"></apex:column> 
				<apex:column value="{!o.Opportunity.StageName}"></apex:column> 
				<apex:column value="{!o.Opportunity.CloseDate}"></apex:column>
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>
</apex:page>