Chatter Workbook: Spring '11

Chatter Workbook

Last updated: March 13, 2011
© Copyright 2000-2011 salesforce.com, inc. All rights reserved. Salesforce.com is a registered trademark of salesforce.com, inc., as are other

names and marks. Other marks appearing herein may be trademarks of their respective owners.

.......................................................................................................................................................................19 Step 5: Query Status Updates........13 Step 3: Run the Application................................................................................................................................................................................................................21 i ...........................................................................................................7 Step 1: Create a Visualforce Page..............................................................................................................................................................................................................................................................................................................................................................................................................................Table of Contents Table of Contents About the Chatter Workbook.............................................................................................................................................................................................6 Summary..............................................................................................................16 Tutorial #4: Accessing Chatter From a ............15 Summary...................................................................................................2 Tutorial #1: Orientation and Setup..................................................................................................................................................................................17 Step 3: Create a Login Method...............................................................4 Step 3: Follow Users..........................................................................................................................................12 Step 1: Create a Java Project...........................................5 Step 4: Create a Chatter Group...........................................................................................................................................................................................................................................................................11 Tutorial #3: Accessing Chatter From a Java Client..........................................................................................................................NET Client............................8 Step 3: Display and Query a News Feed.................................................................................................................................................................................................................................................................6 Tutorial #2: Building on the Platform...............................................................9 Step 4: Add an Automated Test....................................................................................................17 Step 1: Download the Partner WSDL................................................................................................................................................................................................................................................................7 Step 2: Add User Status Update Functionality..............................................................................................................14 Step 4: Update Your Status.................................................................................................................................................................................................18 Step 4: Create a Status Update Method.....................................................................................................................................................14 Step 5: Retrieve the News Feed................................................................................12 Step 2: Create a Java Application.....................................................................20 Summary.....................................................................................................................................3 Step 1: Set Up Your Chatter Profile.................17 Step 2: Create a Console Project in Visual Studio............................................3 Step 2: Follow Data.........................................................................................................................................10 Summary...................................................................................................................................................................................................

• Tell Me More. Note: The Force. If you're interested in creating a client that integrates with Chatter..com. except that it's built in Java and integrates remotely with a Force.com environment. there is an optional Tell Me More section.force. • • • Tutorial 1 explores the fundamentals.force. if you're a smell-the-roses type. The application is functionally identical to the application built in Tutorial 2. Before You Begin You will need a Force. move on to the next step. Apple® Safari version 5.com IDE.0. which introduces you to many of the basic underlying concepts. • • • To continue exploring Chatter development. as well as to how you can develop your own applications using Chatter features. 7.x. although it uses Microsoft . Tutorial 2 walks you through building a very simple Chatter application on Force. For a gentle introduction to developing on Force. which is the starting point of any application built with Chatter. visit Developer Force at http://developer. To learn more about Force.com/cheatsheets..NET as the client implementation language. The Chatter Workbook is an introduction to the major components of Chatter. At the end of each step. If you want to complete Tutorial 3. see “Supported Browsers” in the Salesforce online help.. check out the Chatter Cheat Sheet at http://developer. Tutorial 4 is identical to Tutorial 3. 2 . and 8.com and to access a rich set of resources. For more information. However. choose between Tutorial 3 and Tutorial 4. Microsoft® Internet Explorer versions 6.com development environment supports the following browsers: Mozilla Firefox version 3.com Workbook at http://developer. there's a lot of useful information here.com environment that supports both Chatter and Force. groups.6. Tutorial 3 shows how you can build an external Java client that interacts with the Chatter data model.0.x.force. These tutorials are designed to work with a Force.com/workbook.com development.force. allowing you to create applications that transform the way people interact with apps. data. see the companion Force. and each other.0.com Developer Edition environment.0. though we recommend completing Tutorial 1 and Tutorial 2 first.com. feeds. which you can get for free at http://developer.com/join. How is the Workbook Organized? The four tutorials can be completed in any order. or the Force. and comments. If you like to do things quickly. you'll need Microsoft Visual Studio or the free Microsoft Visual Studio Express. such as posts.com. we recommend Eclipse.About the Chatter Workbook About the Chatter Workbook Chatter adds a rich suite of collaboration features to any application built on Force.com. you will need a Java IDE of some kind. You'll learn how to interact with the data model that supports Chatter. If you want to complete Tutorial 4. It walks you through using the standard Chatter application. depending on your language preference.

In the top right-hand corner. In this tutorial you will explore the basic Chatter functionality. 2.com. All of these concepts are used in later tutorials. There's no programming involved yet. comments. The first step is to log in. Select the Profile tab. comments. following users and data. 4. including user profiles. Duration: 25 minutes Chatter provides a rich suite of features. Along the way you'll learn about users. your applications can bring people and data closer to each other. just configuration. Click the pencil icon in the About Me area and add your role and interests to the description. Chatter also lets data records play a part in this collaboration—so people not only follow other people. and groups. update your profile. groups.force. You can use these features to add a collaborative and social dimension to your Force. and posts that other people have made on your profile. It displays your posts and record updates.salesforce. In your browser go to https://login. Step 1: Set Up Your Chatter Profile In any collaboration environment. We recommend using a free Developer Edition environment. and create your first status update. Prerequisite Chatter This workbook requires an environment with Chatter enabled. Click Save. and feeds. 3. 3 .com applications. status updates. Enter your username (in the form of an email address) and password. select Salesforce Chatter from the drop-down list of applications. select Update Photo and upload a photograph of yourself. 5. find the Chatter application. You'll also learn the names of the objects that model these features in the Chatter data model. Other people can use this description to determine if they want to follow you. it's important to configure your profile so that other users can easily identify and find you. status updates. feeds. but also data that's important to them. In effect.Tutorial #1: Orientation and Setup Tutorial #1: Orientation and Setup Level: Beginner. go to http://developer. 1. Optionally.com/join. To sign up. posts. 6.

This tab displays the news feed—a list of all posts and updates by users and records you follow. The update is a UserProfileFeed record with a value of UserStatus in the Type field. 2.. In the next step you'll follow some data. 6. 4 . Selecting an object displays its tracked fields. select the Sales application from the drop-down list. Click Share. In Chatter. 3. a LinkPost. Select All Accounts from the view filter. it is a ContentPost. the update appears on your user profile on the Profile tab. If your post contains a file. such as My first chatter post. You can also leave a comment on a post. you have just set up your user profile and made your first status update. in exactly the same manner.Tutorial #1: Orientation and Setup 7. Chatter lets you follow data records. anyone who follows the account sees the post in their Chatter feed. 1. Select the Account object. Each data record in the default user interface now displays the related Chatter posts (called a record feed). When you make a change to this account. Navigate to the Employees field. 4. Tell Me More. you need to follow an account record and modify the Employees field. you're going to follow a data record to see how changes to the record are reflected in your Chatter feed. You can also use the What To Do Next area in the upper right-hand corner of the Chatter or Profile tabs to get more information or perform tasks such as adding your photo or creating a group. When you post on someone else's profile. then click Follow for any account. while also providing an indication of who else is following the data. Click Save. In the top right-hand corner. or groups of which you are a member. Enable the checkbox in the Employees field and click Save. as well as other users. Click the Accounts tab. Before you can follow records. 1. Congratulations. • • • The Profile tab also lists the users that follow you.. 5. indicating that a change was made to the Employees field. Now click Edit to edit the account. as well as any custom object you create. all followers see the change you made. When you modify a tracked field. and change the number to 200.. and in a later step you'll create a user to follow. which is stored in the FeedComment object. the UserProfileFeed record has TextPost as the value in the Type field. Click in the user status field and replace What are you working on? with some text. such as Accounts. • You can follow both standard objects. When you update your status. You'll see a new update. Tell Me More. To see the effects in action. as well as have conversations about them. • Step 2: Follow Data In this step. you need to enable feed tracking. make a new status update. as well as users and data records that you are following. you can follow data. Finally.. 2. click the Chatter tab. To see this. or if it contains a link. this time from the account that you followed. 3. unless you also post a file or a link... Click Your Name ➤ Setup ➤ Customize ➤ Chatter ➤ Feed Tracking.

Chatter posts by records have the value TrackedChanges in the Type field of the record that represents the post.com and admin-mary@anotherdomain. 4. TextPosts. Select Joe Blogs.Tutorial #1: Orientation and Setup • • Chatter always indicates the users and records you follow on your Profile tab.com. Find the post you made. these cover the various types of posts that can be made in Chatter. Click on your name. you could have usernames of chatter-mary@mydomain. Step 3: Follow Users Chatter is a collaborative platform. Together with UserStatus. so you'll need more than one user if you want to collaborate.com/ and log in with your credentials. 4. 3.salesforce.salesforce. you create a new user. Now you're ready to work as Joe Blogs.com/ 2. 3. ContentPosts. the record's name is included in your list of followers. 2. Enter Joe Blogs' username and temporary password. You can have multiple usernames that use the same address. click the Comment link and type some text. Click Your Name ➤ Setup ➤ Manage Users ➤ Users and then click New User. Log in as Joe Blogs: 1. such as adding chatter. • • User License: if an option is presented. 1. Click Follow. 1. and a temporary password. 2. In this step. select Salesforce (not Salesforce Platform) Profile: select System Administrator 3. Click the Profile tab. 5 . Click Save and then click Your Name ➤ Logout. For example. Find your profile (the first one you created) and click Follow.in front of your email address Note: A username must be unique and in the form of an email address. Click the People tab. The list of posts is the UserProfileFeed. Select the Salesforce Chatter application. Navigate to https://login. Click Comment to save. log in as that user. Now you're following Joe as well. You need to have your email address handy as user creation involves receiving an email with new user details. and LinkPosts. 2. 3. Navigate to https://login. that both have the same actual email address. follow the original user and comment on their status update. Select the Chatter app. The page now indicates that Joe Blogs is following you. Click Your Name ➤ Logout. Enter the following data in the fields provided: • • • • • First Name: Joe Last Name: Blogs Alias: jblogs Email: enter a valid email address you can check Username: create a unique email address. Now log back in as yourself and follow Joe: 1. and you can see the comment he left. enter a new password and security questions. Because you now follow a record. At the prompt. by clicking on his name. You'll receive an email indicating the username you selected. 5. Chatter posts can be made by people or by records. or image.

or hidden. and create groups. Summary In this tutorial. people often form groups to share knowledge or accomplish a task. You can toggle email notifications by going to Your Name ➤ Setup ➤ Customize ➤ Chatter ➤ Settings and modifying the Email Notification Settings. status updates of people you follow.we call these views feeds. Now post an update to the group.Tutorial #1: Orientation and Setup Tell Me More. you create and join a Chatter group. A group has a list of updates that are made to the group. Step 4: Create a Chatter Group In a collaboration environment.. members must be explicitly asked to join before they can see the group. called a record feed. the Home tab displays your updates. anyone can view the updates and join... follow users. • • • Updates to any group are represented by a CollaborationGroupFeed record. 5. leave comments. This basic Chatter functionality introduced you to the different types of posts that you can create in Chatter. and optionally. You'll see an email from Chatter informing you that Joe Blogs left a comment.. In public groups.. Click New Group.. In this step. In private groups. Click the Groups tab. or tracked changes made to the record. 6 . Your Profile tab lists posts or tracked changes that you've made. Each group member sees an update from the group in their Chatter feed. this is called the NewsFeed. or by clicking Follow when viewing their profile. or that target you. and groups that you are a member of. While you worked through this first tutorial. Each record of an object also displays a feed of posts to that record. This is the UserProfileFeed. Finally. Click Add Members and add Joe Blogs to the group. 4. In hidden groups. updates to records you follow. which is a news feed. stored in the CollaborationGroupFeed. private. Enter a name for the group. Groups can be public. a description and click Save. you must explicitly approve members before they can join. Everyone in the group (you and Joe Blogs) sees that update. you learned how to create a Chatter profile. 2. Tell Me More. you might have noticed a number of different views on all the posts . 1. 3. In the field that displays “Share with Group Name” type My first group update! and click Share. • • You can follow a user by looking them up in the People tab.

Click the Create Page NewsFeedExample link to create the new page. Click Your Name ➤ Setup ➤ My Personal Information ➤ Personal Information ➤ Edit. you'll create a custom user interface using Visualforce.salesforce. For example. 3. or enhance your own applications with Chatter features. 7 . In this tutorial. After you create the page. go back to the URL. you can extend the Chatter functionality. and build a Visualforce page that lets you programmatically update your status and display it.salesforce. the new URL is https://na1. as well as display an input box for updating your status. Before you create the page.” Note: Unsupported browsers do not show this error message. Step 1: Create a Visualforce Page In this step. Duration: 30 minutes Chatter is built on the Force. the NewsFeed and status updates in particular. You can create the page by clicking Your Name ➤ Setup ➤ Develop ➤ Pages and clicking New. simplifying development. 5.com platform. which embeds a page editor into Visualforce pages. Click NewsFeedExample in the bottom left corner of the browser. you take some of the concepts from the previous tutorial. add an attribute sidebar="false" to the apex:page tag. 1. When you click Save. if your current URL is https://na1. By the end of the tutorial. While you're there.com/apex/NewsFeedExample. You don't really want the heading of the page to say “Congratulations. In your browser.com/005A0000000hFv5. 4. The Page Editor tab displays the code and a preview of the new page (which has some default text). You receive an error message: “Page NewsFeedExample does not exist. As a result. Select the Development Mode checkbox and click Save. notice that the page has dynamically updated. 6. Your code should be as follows: <apex:page sidebar="false"> <h1>News Feed</h1> </apex:page> 7. add the text /apex/NewsFeedExample to the current URL. 2.” so change the contents of the heading tag to News Feed and remove both the comments and the contents of the page. Visualforce is a framework that allows developers to create custom pages that are hosted natively on the Force.com platform.Tutorial #2: Building on the Platform Tutorial #2: Building on the Platform Level: Intermediate. you will create a page to list your news feed. you should enable Development Mode.

as well as an update button. If the Page Editor isn't open on your Visualforce page. Just after the closing </h1> tag. 2.status'. click Your Name ➤ Setup ➤ Develop ➤ Apex Classes . <apex:page controller="NewsFeedController" sidebar="false"> 3. that is. To create the class. Click Create Apex method 'NewsFeedController. • • Notice that the code for the page looks like standard HTML. For example. together with an input text area and a button: <apex:form> <apex:inputText value="{!status}"/> <apex:commandButton value="Update" action="{!go}" reRender="recent"/> </apex:form> 5... When you click Save. your information in Salesforce)—exposing data that the view might want to display. In our case.. 4. 1. which exposes some functionality to the page. By now you might have noticed that the page has been updated with a text entry box. The editor informs you that you don't have a method called go. Step 2: Add User Status Update Functionality The Model-View-Controller (MVC) design pattern used by Force. click NewsFeedExample in the bottom left corner. this functionality isn't working yet: you've only updated the page. and offers to create it for you. In MVC. as well a comprehensive component reference that documents how each of the Visualforce components work. you modify the Visualforce page to include a text area and button. the view (the Visualforce page) interacts with a controller. However.com makes it easy to separate the view and its styling from the underlying database and logic. Click Save. In the class. and click New. the controller is an Apex class. In this step. you may be using an unsupported browser. The editor informs you that you don't have a property called 'status' and offers to create it for you. add a form. the editor notes that the NewsFeedController class doesn't exist. add the text NewsFeedController { }. A controller also typically interacts with the model (the database.Tutorial #2: Building on the Platform Tell Me More. the controller might contain the logic that executes when clicking a button. Click Create Apex class 'public class NewsFeedController'. That's because a Visualforce page combines HTML tags with Visualforce-specific tags.go'. The built-in page editor includes tag completion. and add logic to update your Chatter status with the value of the text area after the button is clicked. Select Create Apex property 'NewsFeedController. and then return to the URL of your Visualforce page. 6. 8 . Modify your code to include a NewsFeedController custom controller by editing the <apex:page> tag. Note: If this message doesn't appear.

update u.getUserId().. Step 3: Display and Query a News Feed Currently. } 3. a NewsFeedController tab now displays. ignoring comments and so on. } } When a value is entered into the text area. The way to update someone's Chatter status is to update the CurrentStatus field of their User record. and then update the User record in the database.Tutorial #2: Building on the Platform Next to NewsFeedExample. you must navigate to the Profile tab to verify your user status update. set.getUserId()]. 1. you'll see the generated code below: public class NewsFeedController { public PageReference go() { return null. return null. You'll do something similar. as well as posts by those that you follow. add the following: public List<NewsFeed> getRecentStatusUpdates() { } 9 . return null. Be sure to click Save. you indicate that no new page displays. let's display your status on the page after each update.CurrentStatus = status. } 2. Add a new getRecentStatusUpdates() method to the NewsFeedController that you created in the previous step. 1. u. Modify the contents of the go() method to query the User object and locate the currently logged-in user with UserInfo. • • The Visualforce PageReference type. Instead. public PageReference go() { User u = [SELECT id. When you click the Update button. but limit it to only display status updates. By returning null. When you click NewsFeedController. The Chatter feed on the Home tab as well as the Chatter tab displays your posts.getUserId()]. Tell Me More. determines the page that displays after the method completes. Now set its CurrentStatus field with the value found in the status property. CurrentStatus FROM User WHERE id = :UserInfo. CurrentStatus FROM User WHERE id = :UserInfo. You want this method to update the user's status with the value of the field. This creates a new post of type UserStatus. used in the go() method. the status property that you just created is assigned the value. After the first line in the controller.. That's the next and final task. } public String status { get. the Visualforce page calls the go() method on the controller.. The editor has created a lot of the code for you. public PageReference go() { User u = [SELECT id.

• The <apex:dataList> component iterates over a list. it grabs the body of the NewsFeed record. Body FROM NewsFeed WHERE Type='UserStatus' ORDER BY CreatedDate DESC LIMIT 10]. the body field holds the text that was posted as part of the user status update. You might have noticed that the command button you added earlier has an attribute reRender="recent". The Visualforce page doesn't output all the values that are retrieved from the query. As an optional exercise. Enter the following between the outputPanel tags: <apex:dataList value="{!recentStatusUpdates}" var="update"> <apex:outputText value="{!update. This ensures that an AJAX page update is made when the button is clicked—refreshing a block on the page identified by the "recent" identifier. 2. Add the test method as follows: 1. In this step. it assigns each item (an instance of NewsFeed) to a variable named update. insert the following: <apex:outputPanel id="recent"> </apex:outputPanel> 5. } 3.name.. Click Your Name ➤ Setup ➤ Develop ➤ Apex Classes.body}"/> </apex:dataList> 6.. NewsFeedController and click Edit. Now use a query to retrieve the list of NewsFeed items that are UserStatus updates. Click NewsFeedExample to show the panel displaying your Visualforce markup. assigning each item in the list to a variable.Name are ignored.. You'll see the page dynamically list the most recent status updates beneath the query box.Tutorial #2: Building on the Platform 2. Click on the name of your class. In the code shown. In this case. CreatedBy. The code now returns a list of NewsFeed items that are UserStatus updates. the most recent being first: public List<NewsFeed> getRecentStatusUpdates() { List <NewsFeed> aNewsFeed = [SELECT Type. 10 . For example. The <apex:outputText> component outputs values. • Step 4: Add an Automated Test While you know the NewsFeedController works. 4. return aNewsFeed. you'll add a single test method to the controller to ensure that it behaves as expected. automated tests are required before you can deploy the code to a production environment. Underneath the line containing <apex:commandButton>. 7. Tell Me More. So far so good. extend the Visualforce page to display these fields as well. but now modify the Visualforce page to display them. Click Save. Go ahead and put your list within that block. CreatedDate and CreatedBy. Enter some text and click the Update button. For user status updates. CreatedDate. You'll want to order the posts by date.

status. Return the list of status updates. h. } 9.assert(after. and that it must be called during testing. after[0].assert(after. instantiate a new instance of your controller class: NewsFeedController h = new NewsFeedController(). 10. h. and that the most recent update corresponds to the status update you just made. letting you create applications that manipulate or query the data related to the Chatter functionality.force. 11 . Verify your finished code looks like the following and then click Save. Create a new method before the final closing brace: @isTest static void testUpdates() { } 4. @isTest static void testUpdates() { HelloWorldController h = new HelloWorldController(). System.assertEquals(h. System.assertEquals(h.com.. System. The @isTest annotation indicates that the method is a test method.getRecentStatusUpdates(). 8.status. Click Run All Tests. 7. List<NewsFeed> after = h. h.. Tell Me More.size() > 0 ).go(). your tests verify both positive and negative results for many different use cases. 5. System. 6. List<NewsFeed> after = h..status = 'hi'. and that its code coverage is 100%. after[0]. Assign the status property and then call the go() method to post the update. or extending it using the examples from the Chatter Cheat Sheet at http://developer. This was a very simple example.getRecentStatusUpdates(). Now you need to test that the retrieved list has at least one element. • • Testing is a critical part of code development. You can imagine embedding some of this logic in your own applications. Between the curly braces. h.go(). Summary The data model behind Chatter can be accessed from an application on Force.size() > 0 ). In this tutorial you created logic that updated a user's status and queried their news feed. your screen indicates that you ran one test.Tutorial #2: Building on the Platform 3. You know the most recent post is first because of the ORDER BY in the original query. If all goes well.body). In a real-world scenario. Click Save.status = 'hi'.com/cheatsheets.body).

6) installed on your computer. 3. update your status. 1. 2. partner-18.com exposes a number of APIs. This means that if you write an application that uses the partner WSDL. Select both libraries and click Open.com/p/sfdc-wsc/downloads/list. Click the Libraries tab. A WSDL (Web Services Description Language) describes a Web service end point. Runtime Libraries The Force.com Web Services API that lets you access and manipulate data records from outside the platform. The Chatter data model. Now you need to modify your project to add the two Java libraries listed in the Prerequisites section to the build path. described in Tutorial 1 and used in Tutorial 2. 2.com Web Services API from Java. 5.6. The Java application will authenticate with the Force. Click OK. and Java JDK 6 (1. 3. you create a new Java project in the IDE and configure your Build Path to use the new WSC libraries. Click Finish. Prerequisites Java You will need to have Eclipse or the Force. In this tutorial. one of which is the Force. This tutorial is similar to Tutorial 4. you will learn how to use the Force. can also be accessed in this manner. and display the latest items from your news feed. 4. Data objects in your Force.google. using any language that supports SOAP-based Web services.com Web Services API has two flavors of WSDL: • The Partner WSDL is generic. Step 1: Create a Java Project In this step. Tell Me More. as you 12 .com Web services. click New ➤ Project ➤ Java Project.NET to accomplish the same task. In Eclipse. which uses .jar—A Web service client for accessing Force. Ensure that the JRE selected is 1.. such as Chatter Tutorial. Some knowledge of Java is presumed. Click Add External JARs and browse to the location.. Enter a project name.jar—A pre-generated Java library using WSC and the Partner WSDL..com Web Service Connector (WSC) is an open source code-generation tool and runtime library for accessing Force. The tutorial instructions assume these libraries are on your desktop.com platform. 4. right click the project you just created and choose Project ➤ Properties ➤ Java Build Path.com environment are not reflected in any concrete way— they all share a generic "sObject" description. The Force. Duration: 30 minutes Force.com Web Services from Java.Tutorial #3: Accessing Chatter From a Java Client Tutorial #3: Accessing Chatter From a Java Client Level: Advanced. 1. There are two libraries required: • • wsc-18.com IDE. In the navigation pane. Download them from http://code.

13 . config. static final String USERNAME = "YOUR-SALESFORCE-USERNAME".*. and as a result. Enter a package name of com. Enter a class name of ChatterMain and select the checkbox to create a main() method placeholder.ws. this makes the code less generic and portable. and uses it to establish a new connection to the platform. static final String PASSWORD = "YOUR-SALESFORCE-PASSWORD-AND-TOKEN". Of course. connection = Connector. 7.soap.partner. any data object you have. java.setPassword(PASSWORD).sforce. com. com.Tutorial #3: Accessing Chatter From a Java Client • are doing in this tutorial.sobject.chatter.soap.sforce. add the static class members to hold your Force.getUserFullName()). com.soap. The Enterprise WSDL is strongly typed.sobject.*. add the import statements for the required libraries: import import import import com.partner. public static void main(String[] args).newConnection(config). import import import import com. but typically more readable. such as FooBar. config. Click Finish.setUsername(USERNAME).soap. do the following: 1.*.*. Click File ➤ New ➤ Class. Verify that your code looks like the following and then click Save.*.getUserInfo().*.partner. After the class declaration. java. //displayNewsFeed().sforce.chatter. package com. 4. The new class opens in the editor. System.com login credentials and connection object from the Web Services Connector (WSC). You might need to append your security token to the end of the password. static PartnerConnection connection. this means that the WSDL is specific to your environment (someone else might not have that object).sforce. } 8. com. You'll write those methods in later steps and then uncomment the method calls here.developerforce. Replace the main() method in the class with the following code.com environment. In this WSDL. To begin creating this application.com credentials. After the package declaration. 6. given the correct permissions. FooBar.sforce.io. //updateStatus().sforce. Step 2: Create a Java Application Your application consists of a single class that contains all of the application’s functionality. username and password.io.partner. will be reflected as an object in Java. public static void main(String[] args) throws Exception { ConnectorConfig config = new ConnectorConfig().*. The code has two method calls that are commented out. it will work within anyone's Force.ws. This method creates a new PartnerConnection object (from the WSC library) with your Force.out. 5.*. 3.println("Hello " + connection. 2.developerforce.

//displayNewsFeed(). public static void main(String[] args) throws Exception { ConnectorConfig config = new ConnectorConfig(). static PartnerConnection connection.. If the application connects successfully you see. 2. config. make sure your username and password are correct.getUserFullName()).println("Hello " + connection. config. "Hello" followed by your full name.. To obtain your security token. inserting and updating records.setPassword(PASSWORD). you modify the application to update your status with a new method..setUsername(USERNAME). updateStatus().Tutorial #3: Accessing Chatter From a Java Client public class ChatterMain { static final String USERNAME = "jmountjoy@devchatteradmin. as well as retrieving metadata about objects in your Force. If they are.out. log into your organization using a Web browser.com environment.com environment. If the application connects to your organization successfully. The connection object itself is critical to any further interaction. If you receive an INVALID_LOGIN exception. System. } } Tell Me More.getUserInfo(). connection = Connector. the first thing you do when interacting with the platform is establish a connection with a Force. Click the Console tab at the bottom.. you will see your user's full name printed out. Step 4: Update Your Status In this step. //updateStatus(). 1. First.newConnection(config). and contains all the methods you need to interact with the platform. 1. static final String PASSWORD = "password1Ud4VCrSU5YtYf7vbg7NaT1u9j". Force.. Typically.org". Step 3: Run the Application Now it is time to test your application. Tell Me More. The code in this step uses the getUserInfo() method to extract information about the authenticated user. Other methods exist for querying.. Click Run ➤ Run As ➤ Java Application. create the updateStatus() method: private static void updateStatus() throws Exception { } 14 . you might need to append a security token to your password. and navigate to Your Name ➤ Setup ➤ My Personal Information ➤ Reset My Security Token.com has a number of built-in security measures— one of which ensures that you need additional security when logging in from a new or untrusted location. You will receive an email that contains the string to append to your password. 3.

in the main() method. Create the user sObject to hold the new status update: SObject user = new SObject().println("User status successfully updated. } else { System. your Chatter status will be updated. 3. System. user. 5.getUserId()).isSuccess()) { System. 8.out.update(new SObject[] { user }). provide its Id. As a result.out.println("What are you working on?"). you have to use a generic type called SObject to represent all of the actual objects you have in your Force. user.setId(connection. Call the update() method to update the record: SaveResult[] results = connection. } 6. This time you'll be prompted to write something.getUserInfo(). 4.setType("User"). This is exactly what you did in Tutorial 2 to update the user status. ask the user for a status update: InputStreamReader converter = new InputStreamReader(System.in the code above this is done with the setType() method call. Step 5: Retrieve the News Feed In this step. You effectively create a User object. Because you're using the Partner WSDL..println("Error updating user status: " + results[0].Tutorial #3: Accessing Chatter From a Java Client 2. After pressing Enter.setField("CurrentStatus".").in).. BufferedReader in = new BufferedReader(converter).out. 7. Uncomment updateStatus(). 15 .. Run the application by following Step 3: Run the Application on page 14.getErrors()[0]. Tell Me More. you query your NewsFeed and display the results. Between the curly braces. and set a field on that object called CurrentStatus. user.getMessage()). you have to tell the SObject what type it represents .com environment. Check for errors: if (!results[0]. in.readLine()).

or consult the Chatter cheat sheet to find queries to run against the Chatter data model.Tutorial #3: Accessing Chatter From a Java Client 1. you can navigate through the generic object and retrieve the data you require. Run the application again and you should see a list of status updates: Tell Me More. Body FROM NewsFeed WHERE Type= 'UserStatus' ORDER BY CreatedDate DESC.com platform.. If there are any results.. and performs a query. the code iterates over the records that were returned. After executing the query. Your Java application authenticates with the Force. CreatedBy.com environment."). As a result.println("Querying for status updates. updates a record (your User record.getSize() > 0) { for (SObject s : queryResults. all record results are of the generic type sObject.getChild("NewsFeed").name. printing out the body of each feed post. By using the getChild() and getField() methods. • Summary In this tutorial. • The code performs a query on the NewsFeed object—it's the same query that you used in Tutorial #2: Building on the Platform on page 7.out.println(s. if (queryResults. ID DESC LIMIT 10"). Add a new displayNewsFeed() method. QueryResult queryResults = connection. Because you're using the Partner WSDL.getField("Body") ). Now uncomment the displayNewsFeed() call in the main() method. } } } 2. 16 . you wrote a Java client application that integrates with the Force..getRecords()) { System. You can explore the other methods on the connection object to learn what other actions you can perform. which contains your user status). CreatedDate..query ("SELECT Id. a QueryResults object is instantiated with the results of the query.. private static void displayNewsFeed() throws Exception { System. Type. 3. Java can't know which fields are in the object.out.

NET Client Tutorial #4: Accessing Chatter From a . 2. Step 2: Create a Console Project in Visual Studio In this step you create a new C# Console Application and then add the Partner WSDL as a reference. described in Tutorial 1 and demonstrated in Tutorial 2. click Add Web Reference. Enter the path to the Partner WSDL. and displays the latest items from your news feed. username and password. In the Web reference name field. In the Solution Explorer. In a Web browser. enter PartnerWSDL and click Add Reference. For more information on the WSDL. 1. updates your status. see the Tell Me More. This means that if you write an application that uses the partner WSDL. Name the project ChatterConsole and click OK. section in Tutorial #3: Accessing Chatter From a Java Client. Step 1: Download the Partner WSDL Before you begin development. you create a . Click Advanced.. 3. 4. such as the Force. Step 1: Create a Java Project on page 12. data objects in your Force.. Save the file to your desktop.com Web Services API. as you are doing in this tutorial. The Chatter data model. 8.com environment are not reflected in any concrete way— they all share a generic "sObject" description. In the Service Reference Settings dialog. 3.com exposes a number of APIs. that lets you access and manipulate data records from outside the platform using any language that supports SOAP-based Web services.Tutorial #4: Accessing Chatter From a .. 7. In the Partner WSDL. can also be accessed in this manner.. Click Your Name ➤ Setup ➤ Develop ➤ Generate Partner WSDL. it will work within anyone's Force. 1.com environment. In this tutorial.com platform. 17 . which uses Java to accomplish the same task. right-click References and select Add Service Reference. choose Console Application. Prerequisites Visual Studio Familiarity with Microsoft's Visual Studio IDE is presumed. log into your Force.NET Client Level: Advanced. Select File ➤ New Project From the templates. This tutorial is similar to Tutorial 3.NET client that authenticates with the Force.com environment. 5. Tell Me More. given the correct permissions. Duration: 30 minutes Force. 6.. you need to download the Partner WSDL.. 2.

To use the Partner WSDL. // chatProgram. private bool login() 4.WriteLine(e. Console. In the Main() that was automatically generated. This is a good place to pause and make sure that everything works as expected. 2. If the login fails. Console. private static string PASSWORD = "YOUR-SALESFORCE-PASSWORD-AND-TOKEN". Add the following lines: private SforceService binding.login(USERNAME. Next create the service object: binding = new SforceService().sessionId.Url = lr. append it to your password. try { Console.WriteLine("\n\nHit enter to exit.SessionHeaderValue = new SessionHeader().. and if successful.login()) { Console.. do the following: 1.serverUrl.ReadLine(). trap and report the error. LoginResult lr. binding. You'll also enter your username and password so you don't have to keep re-entering it every time you run the program. allowing you to leisurely examine the results of your work: Program chatProgram = new Program(). If you have a security token. PASSWORD).NET Client Step 3: Create a Login Method Your application consists of a single class that contains all of the application’s functionality. add two lines at the end of your Main() so that the application has to wait for input to close. // chatProgram. Try the login.Tutorial #4: Accessing Chatter From a . } catch (Exception e){ // report the fault to the console Console. add the following using statement: using ChatterConsole. binding. Next.updateStatus(). private static string USERNAME = "YOUR-SALESFORCE-USERNAME".sessionId = lr. insert the following code to instantiate the class and then run the login.WriteLine("Now logging in. 5.SessionHeaderValue.").. binding. if (chatProgram."). } 6. To begin creating this application. Also.Message).queryForUpdates(). lr = binding.Write("Success!"). 18 .. set up the header and session ID. go on to bind the SforceService object to the correct endpoint.PartnerWSDL. you need an SforceService object to use in all the methods created in this tutorial. Add a method signature for the login() method. 3. return false. return true.

"). user.type = "User".. the first thing you do when interacting with the platform is establish a connection with a Force."). private void updateStatus() { } 2.Write("I couldn't log in. as well as retrieving metadata about objects in your Force. user.Xml. Construct an sObject. If you had trouble connecting. } 7.CreateElement("CurrentStatus"). There should be a message in your console stating "Success!" Tell Me More.. you create a method that asks the user to update their status after logging in. Console.XmlElement[] { statusElement }.. Now build and run your project. System. • Typically.Xml.Tutorial #4: Accessing Chatter From a .Id = binding.ReadLine()... string newStatus = Console.getUserInfo().WriteLine("\n\nHit enter to exit. section in Tutorial #3: Accessing Chatter From a Java Client.Xml. Now create an XML element to store the CurrentStatus value. statusElement. user. 4. System. Then add the element to the user object. 6. 1.. Now have Force.XmlDocument doc = new System. Other methods exist for querying. 3. • Step 4: Create a Status Update Method In this step.XmlDocument(). and contains all the methods you need to interact with the platform.update(new sObject[] { user }).com environment..userId. 19 . Start by asking the user for a status update: Console. The connection object itself is critical to any further interaction. see the Tell Me More.InnerText = newStatus.XmlElement statusElement = doc.NET Client } else { Console. Console.ReadLine(). Step 3: Run the Application on page 14.Xml..Any = new System.com update the record: SaveResult[] results = binding. and give it the correct ID and type: sObject user = new sObject().Write("\n\nWhat are you working on?\n"). Create a new method for the status update as follows.com environment. 5. inserting and updating records.

NET Client 7. 20 . you query your NewsFeed and display the results. CreatedDate.. When you run the program again.WriteLine("Status updated. Finally.\n").success) Console. Body " + "FROM NewsFeed WHERE Type='UserStatus' " "ORDER BY CreatedDate DESC.com environment and navigating to your Profile tab in the Chatter application. When constructing an XmlElement. 10.Length.WriteLine("Error updating: " + results[j]. 9.Tutorial #4: Accessing Chatter From a . Tell Me More.name. send the results to the console: for (int j = 0. Create another method for the query. ID DESC LIMIT 5"). notice the output in the console. construct the query and retrieve the results using SforceService.. QueryResult qr = binding. Step 5: Query Status Updates In this step. 1. You can verify your status by logging into your Force. The Java code in Tutorial 3 does this slightly differently because the WSC library that it uses hides a lot of this low-level manipulation. First. else Console. } 8.query ("SELECT Id.. add in the call to updateStatus() after a successful login. give it the name of the field and fill in its value. In your Main() method. j++) { if (results[j]. private void queryForUpdates() 2. CreatedBy.message + "\n"). Remember to make the call to this method from your Main() in the successful login code block.errors[0]. The Any field of the sObject is an array of XmlElements containing information about the fields belonging to the object. Type. j < results.

At this point.records[index]..WriteLine("Id: " + qr.InnerText). 21 . • The code performs a query on the NewsFeed object—it's the same query that you used in Tutorial #2: Building on the Platform on page 7. the included fields are stacked in the array returned by the Any field of the SObject. all records returned are of the generic type SObject. a QueryResult object is instantiated with the results of the query.NET application that integrates with the Force.Id + " . You can explore the other methods on the connection object to learn what other actions you can perform. the innerText of the XmlElement at the 4th array index (beginning at 0): Any[4].WriteLine("\nQuerying for status updates:"). After executing the query.InnerText. In this case. index < qr. the code iterates over the records that were returned.body.size > 0){ Console.. you're ready to build and run it. index++){ Console. • Summary In this tutorial. for (int index = 0.NET Client 3. } } 4.records.com environment. and performs a query. printing out the body of the feed post. updates a record (your User record.Any[4].records[index].com platform. As a result.Length. which contains your user status). If there are any results. Because you're using the Parter WSDL. Now iterate through the results to the console: if (qr. So you need to look at the query you originally made to know which array element to reference in order to retrieve the text string for FeedPost. Your application authenticates with the Force.. you wrote a . press F5 and go! You should see your last five status updates in the console window." + qr. or consult the Chatter cheat sheet to find other queries to run against the Chatter data model.Tutorial #4: Accessing Chatter From a . Tell Me More.

Sign up to vote on this title
UsefulNot useful