8

In this post i want mention the user. I'm using the connect apex chatter.

I'don't know how mention the user with u.id,and now i'm getting an error:

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object

Please can you take a look to my code?

Thank you in advance.

    Global class RandomPilot implements Schedulable{

        //FOR TEST METHOD
        public static String CRON_EXP = '0 0 0 3 9 ? 2022';

        Global void execute(SchedulableContext sc) 
        {
            // SEARCH BY STATIC RESOURCE NAME
            // Post a feed item that has an @-mention.
            String communityId = null;

            for(User u:lstu)
            {
                // CREATE A FEED_ITEM_TEXT
                String feedItemText= 'Happy Birthday!';

                ConnectApi.FeedItemInput feedItemInput = FeedBodyParser.convertToFeedItemInput(feedItemText);

                // POST FILE_FEED_ITEM
                ConnectApi.NewFileAttachmentInput fileIn = new 
                 ConnectApi.ChatterFeeds.postFeedItem(communityId,ConnectApi.FeedType.Record,fgroup.id, feedItemInput, feedBinary);
            }
        }
    }
2

1 Answer 1

10

In the future context, the UserInfo.getSessionId() evaluates to null.

You'd need to pass in the sessionId from the method invocation to future.

So,

public static void mention(string uid,string RecordId, String sessionId){

And whilst invoking it, pass in the Session Id

mention(u.id,post.id, mention(u.id,post.id,UserInfo.getSessionId() ); 

I'd still urge you to implement with Connect in Apex rather than this Chatter REST API Callout.

How to mention with Connect

Copy the FeedBodyParser Utility Class from http://developer.force.com/cookbook/recipe/connect-in-apex-pilot

Then to post a mention simply use

ConnectApi.FeedItemInput feedItemInput = FeedBodyParser.convertToFeedItemInput('Hello @[Ritesh Aswaney](user:005i0000000nEVV)');      
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.News, 'me', feedItemInput, null);

(You're going to end up creating two posts, one via your insert and one via the REST API - whereas I'd imagine you intend creating just the one)

10
  • Thank you very much!before choosing of use callouts I tried connect but it seemed complicated. Can you tell me which steps i need to do to implement the solution with Connect?1)i have created the helper classes:FeedFormatter,CommentInfo. which is in the example the code that show how create a post with all informations like body,createdby?...i'm using this example blogs.developerforce.com/developer-relations/2013/02/… Thanks again,you are very kind.
    – Enry
    Jun 10, 2013 at 17:57
  • @Enry edited my answer to include how to mention with Connect. Jun 10, 2013 at 18:14
  • Thank you very much! i am going to try your solution.If i will have any problems i will let you know.Thanks for the support :)
    – Enry
    Jun 10, 2013 at 18:35
  • Sure, no problem :) Jun 10, 2013 at 18:36
  • 1
    caution there is no bulkified way to implement chatter mention, if you implement this in for loop with lots of records, you are likely to hit limits - salesforce.stackexchange.com/questions/12484/… Jun 11, 2013 at 5:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .