apex

wrapper class and force.com sites

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>

 

Banter

2 comments for “wrapper class and force.com sites”

  1. Great code, Joe. I particularly like that you used multiple colors. I wonder if one could pass values to the controller through using a Visualforce component, letting the user choose what each of the colors would be. Think it’s possible?
    David Schach
    X-Squared On Demand

    Posted by David Schach | November 14, 2008, 2:35 am
  2. @David: I’m sure it can be done; I’ll give it a whirl this weekend.

    Posted by joe | November 14, 2008, 11:24 am

Post a comment