Using Sites to easily publish data out of Salesforce
Last Updated on Wednesday, 24 June 2009 12:09 Written by Steve Wednesday, 24 June 2009 10:45
I 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.
Nice post. The only downside of this is that iFRAME heights are static and if your list is longer than the height, there will be a scrollbar inside your page. HTML needs a tag so the contents could be pulled and placed on your main page.
That is indeed a limitation of IFRAME. Creative placement of the IFRAME could help with that growth issue, if the project is a very simple one.
Another thing about IFRAME is that the containing page doesn’t know anything about the contents of the IFRAME page, so if you have search capabilities on your website, it won’t find these sponsors by name.
With a more sophisticated implementation, you could use sites to publish the same data in XML or JSON which your website could consume and even store locally, and then you’d have much more control.
Hi Steve, this is great stuff and the instructions are very clear, except there is something I keep missing. I am assuming the custom controller has to be created in a non-production environment. I have chosen to do it in my sandbox. I have my controller code done. Since the code is so short, do I need to test it? From everything I have read testing is required. How would I go about writing a test for your controller code… and finally the big question that I can’t seem to get an answer to: How do I go about making my controller code available to my production environment. I am sure I must be missing something very simple, but I can’t seem to find these answers. Thanks again Steve.
Thanks Gary!
Here is a good resource for information on writing tests for Salesforce.com.
I guess this beyond my abilities for the time being. To bad, everything else really looked so easy and straightforward. After looking at the document you suggested, I could not get my head around the test to use for the custom controller. Thanks anyway. I’m sure I’ll be able to understand this eventually.
[...] gokubi.com » Blog Archive » Using Sites to easily publish data out of Salesforce [...]