Skip to content

Instantly share code, notes, and snippets.

@greenstork
Created July 18, 2013 20:35
Show Gist options
  • Save greenstork/6032843 to your computer and use it in GitHub Desktop.
Save greenstork/6032843 to your computer and use it in GitHub Desktop.
Visualforce page and controller for surfacing an RSS feed of Questions from Salesforce.
<apex:page controller="PUBQandARSSController" contentType="text/xml" showHeader="false" sidebar="false" cache="false"><?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Power of Us Hub Questions Feed</title>
<description>These are the 50 most recently asked questions on the Power of Us Hub</description>
<link>http://powerofus.salesforcefoundation.org</link>
<apex:repeat value="{!questions}" var="q">
<item>
<title>{!q.title}</title>
<description>{!q.description}</description>
<user>{!q.creator}</user>
<link>{!q.link}</link>
</item>
</apex:repeat>
</channel>
</rss>
</apex:page>
public without sharing class PUBQandARSSController {
public List<PUBQuestion> questions { get; private set; }
public PUBQandARSSController() {
this.questions = new List <PUBQuestion>();
List <Question> questionlist = [SELECT Id, Title, CreatorName, Body FROM Question ORDER BY CreatedDate DESC LIMIT 50];
for (Question singleQuestion : questionlist) {
questions.add(new PUBQuestion(singleQuestion.Title, singleQuestion.Body, singleQuestion.CreatorName, 'https://powerofus.force.com/_ui/chatter/service/ChatterAnswersUi#!/feedtype=SINGLE_QUESTION_DETAIL&id=' + singleQuestion.Id));
}
}
public class PUBQuestion {
public String title { get; private set; }
public String description { get; private set; }
public String creator { get; private set; }
public String link { get; private set; }
public PUBQuestion(String title, String description, String creator, String link) {
this.title = title != null ? title.escapeXml() : null;
this.description = description != null ? description.escapeXml() : null;
this.creator = creator != null ? creator.escapeXml(): null;
this.link = link != null ? link.escapeXml(): null;
}
}
/****************************************
* TESTS *
****************************************/
static testMethod void testFeed() {
List<Question> sampleQuestions = new List<Question>();
Community c = [SELECT id FROM Community WHERE IsActive = true LIMIT 1];
for (integer i = 0;i < 50; i++) {
Question q = new Question(
Title='Why would anyone use the 1-to-1 model?',
Body='I\'m confounded',
CommunityId = c.id
);
sampleQuestions.add(q);
}
insert sampleQuestions;
PUBQandARSSController controller = new PUBQandARSSController();
system.assertEquals(50,controller.questions.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment