Using Sites to easily publish data out of Salesforce
June 24th, 2009I want to show you a quick and easy way to take live data from Salesforce.com and post it on your website using Salesforce Sites. You can see lots of hard-core examples of how to use sites. Those are cool, absolutely no doubt. People are building amazing things.
There also are opportunities for very targeted uses of Sites. With these micro-sites, the investment-to-value ratio can be really high. Here’s an example of one such use case.
The Use Case
A nonprofit wants to have a page on it’s website acknowledging all sponsors who supported events in 2007. They want a simple list of organizations, leaving out any who have asked not to be thanked publicly.
The Data
Sponsorships are tracked as Opportunities connected to the relevant Account. Here’s the Opportunity related list from the relevant Account:

I have created a rollup summary field on the Account to sum up all Opportunities from 2007:

And I’ve created a checkbox to flag Accounts that want to stay anonymous. You can see both fields on the account detail page:

The Display
I want to show a simple list of donors that gave in 2007 and aren’t anonymous. To do this, I need to create a VisualForce page with a simple Apex controller so I can grab the right data, and then display it correctly
Here’s the Apex controller where I’m grabbing the data that I want. You can see from my SOQL statement that I’m only getting Accounts where the rollup field is greater than zero and the anonymous field isn’t checked. I’ve named it donorListing:
public with sharing class donorListing {
List<Account> accounts;
public List<Account> getAccounts(){
accounts = [select name from account WHERE X2007_Giving_Total__c > 0 AND Anonymous_Sponsor__C = false ORDER BY Name];
return accounts;
}
}
Here’s the VF Page where I display the data. I point to the Apex I created via the controller attribute of the page. I also turn off the Header, so that I don’t see all the Salesforce navigation elements. Then I just list out the sponsors.
<apex:page controller="donorListing" showHeader="false">
<apex:dataList value="{!accounts}" var="account" id="theList">
<apex:outputText value="{!account.name}"/>
</apex:dataList>
</apex:page>
And here is what the VF page looks like in my browser when I’m logged in to Salesforce.com:

OK, looks great. But I want it visible to people not logged into Salesforce. To do that, I need to create a Salesforce Site.
The Site
You can create a Salesforce site at Setup | Develop | Sites. Create a site, and give it a name. It will show you the URL for this site. That’s the URL people can hit from anywhere on the web to see your VisualForce pages.
You associate VisualForce pages with a Site on the Site detail page. You’ll see here my donorlist page is a part of this site:

Next, I want to make this page visible to the public–I don’t need people to login to see it. That’s done by modifying the public access settings:

Make sure your pages and classes(classes used by VisualForce pages are added automatically) are associated with the site:

OK, now we’re ready to hit the page at it’s external location. For this example, the URL is:
http://smsdev-developer-edition.na6.force.com/sms/donorlisting
The end product
Now that we can see the page publicly, we can embed it anywhere on the web using something called IFRAME. It’s a part of the HTML that runs the web, and has been around for a long time. Here’s the code for embedding this page in my website:
<iframe src="http://smsdev-developer-edition.na6.force.com/sms/donorlisting" width="100%" height="150" frameborder=0>
The nonprofit can now paste that IFRAME code into it’s website content management system. I’ve done just that in this post–you can see the sites page embedded right here:
When you hit this page, you’ll see the live data. If I get new sponsors or I flag sponsors as anonymous, they’ll appear or disappear accordingly. Just manage your data through your normal process and the public acknowledgement just happens.
Heck, you could even put workflow on the Account that automatically sends an email to the Account when they make this list. That email could contain a link to the acknowledgement page. That’s a pretty nice thank you experience for the sponsor, I think.
The challenge
This is a very simple example meant to get you thinking about what’s possible. What do you want to do with Sites? Come up with your own use cases and implement them in a developer org. As you can see, there isn’t much code involved to get it working. Show your boss what’s possible with an example. Tell them how long it took you to get data shared on your organization’s website. Modify the data in Salesforce.com and then hit refresh in the browser. Astound and amaze! And then build Sites into your business process for quick and easy publishing of your Salesforce.com data.
