3

How can i use a different chatter account to post feed items.

This is because I am posting feed elements from apex. But the user doesnt get notified when tagged himself(@mention) in feed.

So i wanted to use other chatter account to post and tag the logged in user, so that the user can get notification.

Below is the code for posting feed:

List<ConnectApi.BatchInput> batchInputs = new List<ConnectApi.BatchInput>();

for(sObject c : [select Name from user where name LIKE '%anurag%']){

     ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
     ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
     ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
     ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

     messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
     //Mention user here
     mentionSegmentInput.id = userinfo.getUserId();
     messageBodyInput.messageSegments.add(mentionSegmentInput);

     textSegmentInput.text = '\n'+' Chatter text here';
     messageBodyInput.messageSegments.add(textSegmentInput);

     feedItemInput.body = messageBodyInput;
     feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
     feedItemInput.subjectId = c.Id;

     ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(feedItemInput);
     batchInputs.add(batchInput);

 }

 ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchinputs);
0

1 Answer 1

2

Currently, both Chatter in Apex and the Chatter REST API do not support impersonation like the SObject API does. You can only post feed items as the current, logged-in user.

This is documented in "Differences Between ConnectApi Classes and Other Apex Classes": http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_connectapi_differences.htm

If you look at the method def:

postFeedElement(communityId, feedElement) Posts a feed element from the context user."

PostFeedElementBatch just does the same thing but in a batch mode. You cannot set the user this way.

We were able to overcome somewhat by using an email service to call this method. You can control who the email service runs under. But it requires you to send an email to that service and may not work like you want in batch.

5
  • Can you include supporting quotation? I don't see how your link supports your claim.
    – Adrian Larson
    Commented Apr 26, 2016 at 17:32
  • was sourced from this question.. developer.salesforce.com/forums/?id=906F00000009AEhIAM Commented Apr 26, 2016 at 19:53
  • Have you looked through the link, though? It does not support the claim. It says you can't use runAs, but that can only be used in tests anyway.
    – Adrian Larson
    Commented Apr 26, 2016 at 19:59
  • If you look at the method def: "postFeedElement(communityId, feedElement) Posts a feed element from the context user." PostFeedElementBatch just does the same thing but in a batch mode. You cannot set the user this way. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Apr 26, 2016 at 20:34
  • We were able to overcome somewhat by using an email service to call this method. You can control who the email service runs under. But it requires you to send an email to that service and may not work like you want in batch. Commented Apr 26, 2016 at 20:35

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