function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AsitAsit 

Post a message From Salesforce to multiple users' wall or to any Page using Facebook Toolkit?

Using facebook toolkit, I can successfully post any thing on my own wall in facebook from my salesforce account.

But is it possible to post something onto a page or some other's account? 

How can this integration be done?

 

Pls help!

 

Regards,

Asit

Navatar_DbSupNavatar_DbSup

Hi,

However you obtain the access token, accessing the API follows the same pattern. You can retrieve most Facebook Graph API objects by calling the relevant constructor with the access token and an id (for example, me) or connection (for example, me/friends) and an optional map of API parameters.

 

So, to retrieve the User object for a user with Facebook ID 1111111111:

 

1

FacebookUser user = new FacebookUser(access_token, '1111111111');

and to retrieve a list of friends, including their hometowns, for the currently authenticated user:

 

1

Map<String,String> params = new Map<string,string>{'fields' => 'id,name,hometown'};

2

FacebookUsers friends = new FacebookUsers(access_token, 'me/friends', params);

Note that your app is limited to the data to which the authenticated user and other users have granted access.

 

Once your app has retrieved a Graph API object, it can manipulate it in Apex or Visualforce using its Apex properties. Here a Visualforce page iterated through the friends object obtained above:

 

1

<apex:pageBlockTable value="{!friends.data}" var="friend">

2

    <apex:column value="{!friend.id}" headerValue="Id"/>

3

    <apex:column value="{!friend.name}" headerValue="Name"/>

4

    <apex:column value="{!friend.hometown.name}" headerValue="Hometown"/>

5

</apex:pageBlockTable>

You can see many similar examples in the sample pages and controllers:

 

FacebookSamplePage

FacebookSampleController

FacebookTestUser

FacebookTestUserController

Writing a Simple Facebook Application

 

Let's write a simple Facebook app on Force.com that uses the toolkit to show users information on their friends. Many Facebook apps are games or quizzes that let uses see their friends scores, but, to keep things simple, we'll simply ask users to enter their favorite number, and show them a list of their friends, ranked by their favorite numbers.

 

First, we'll create a custom object in Force.com to hold our users' Facebook IDs, names, and favorite numbers.

 

Go to Setup | App Setup | Create | Objects and click New Custom Object.

Give it the label Facebook Favorite, plural Facebook Favorites, and change the record name to Facebook Name, leaving the data type as Text.

Click Save, then click New in the Custom Fields & Relationships section.

Click Text then Next.

Enter a Field Label of Facebook Name, with a length of 100 and click Required and Unique - every record must have a unique Facebook ID.

Click Next, then Next, then Save & New, accepting the defaults.

Click Number then Next.

Enter a Field Label of Favorite Number and click Required - every record must have a Favorite Number.

Click Next, then Next, then Save, accepting the defaults.

Now we have a simple data model, we'll implement a Visualforce page and controller that will allow users to enter their favorite number and see their friends' favorites.

 

The following steps use the declarative browser interface; you can use the Force.com IDE if you prefer.

 

Go to Setup | App Setup | Develop | Apex Classes and click New.

Paste in the following code and click Save:

global with sharing class FacebookFavoriteController

extends FacebookLoginController {

    public static String getAccessToken() {

        return FacebookToken.getAccessToken();

    }

 

    public FacebookUser me {

        get {

            // Can't set up 'me' in the controller constructor, since the

            // superclass 'login' method won't have been called!

            if (me == null) {

                String accessToken = getAccessToken();

 

                // If accessToken is null, it's likely that the page's action

                // method has not yet been called, so we haven't been to FB to

                // get an access token yet. If this is the case, we can just

                // leave 'me' as null, since the redirect will happen before

                // HTML is sent back.

                if (accessToken != null) {

                    me = new FacebookUser(accessToken, 'me');                   

                }

            }

 

            return me;

        } set;

    }

 

    public Integer favorite {

        get {

            if (favorite == null) {

                String accessToken = getAccessToken();

 

                if (accessToken != null) {

                    List<Facebook_Favorite__c> favorites =

                        [SELECT Favorite_Number__c

                            FROM Facebook_Favorite__c

                            WHERE Facebook_ID__c = :me.id];

 

                    favorite = (favorites.size() == 0) ?

                        null :

                        Integer.valueOf(favorites[0].Favorite_Number__c);

                }

            }

 

            return favorite;

        } set;

    }

 

    private FacebookUsers friends {

        get {

            if (friends == null) {

                String accessToken = getAccessToken();

 

                if (accessToken != null) {

                    Map<String, String> params =

                        new Map<String, String>{'fields' => 'id,name'};

                    friends = new FacebookUsers(accessToken, 'me/friends', params);

                }

            }

 

            return friends;

        } set;

    }

 

    public List<Facebook_Favorite__c> friendsFavorites {

        get {

            if (friendsFavorites == null) {

                String accessToken = getAccessToken();

 

                if (accessToken != null) {

                    List<String> friendIDs = new List<String>();

 

                    // Show my own favorite number, if I have one

                    friendIDs.add(me.id);   

 

                    for (FacebookUser friend : friends.data) {

                        friendIDs.add(friend.id);

                    }

 

                    friendsFavorites = [SELECT Name, Facebook_ID__c, Favorite_Number__c

                        FROM Facebook_Favorite__c

                        WHERE Facebook_ID__c IN :friendIDs

                        ORDER BY Favorite_Number__c DESC];

                }

            }

 

            return friendsFavorites;

        } set;

    }

 

    public PageReference save(){

        if (me == null) {

            throw new FacebookException('Cannot save without a Facebook user!');

        }

 

        List<Facebook_Favorite__c> favorites =

            [SELECT Favorite_Number__c

                FROM Facebook_Favorite__c

                WHERE Facebook_ID__c = :me.id];

 

        if (favorites.size() == 0) {

            Facebook_Favorite__c newFavorite = new Facebook_Favorite__c(Name = me.name,

                Facebook_ID__c = me.id,

                Favorite_Number__c = favorite);

            insert newFavorite;

        } else {

            favorites[0].Favorite_Number__c = favorite;

            update favorites[0];

        }

 

        // Force the favorites list to be refreshed so I see my own favorite

        friendsFavorites = null;

 

        return null;

    }

 

    public PageReference refreshFriendsFavorites() {

        // Force the friends list to be refreshed

        friends = null;

 

        // Force the favorites list to be refreshed

        friendsFavorites = null;

 

        return null;

    }   

}

 

Note the pattern established in the controller for initializing the me, favorite, friends and friendsFavorites properties. We cannot initialize the properties in the controller's constructor, since it will be executed before the user has a chance to login to Facebook. Each property implements its get accessor to only initialize the property.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

praveenreddypraveenreddy

Hi  this reply is ok but what i am asked exctly  how to reply of the comment for particular usar(commenting an action)

 

praveen.

AmitSfdcAmitSfdc

Hi  asit, 

 

I have configured the facebook toolkit and after that i can able to retrive post from my facebook page/account. but i am unable to post reply comment against the post i retrived . i have a controller calss and a visualforce page..can u please help on me that...

 

Advance thatnks 

Amit


Asit wrote:

Using facebook toolkit, I can successfully post any thing on my own wall in facebook from my salesforce account.

But is it possible to post something onto a page or some other's account? 

How can this integration be done?

 

Pls help!

 

Regards,

Asit




Kalyan Babu DKalyan Babu D
hi, hw can i get access token.I want to post one url on fb.Can you please tell me the procedure exactly.I am new to salesforce.Please help me out.Thanks in advance