0

I have a user case where I want a specific group of Users to automatically LIKE specific posts based on some criteria in a trigger.

Failed thoughts on how to accomplish this:

  • Use DML. I experimented with FeedLike but it doesn't seem to support SOQL queries such as FeedItemn so DML operations to add these and set the CreatedById (or InsertedById) don't seem to be in the cards (or maybe it supports Insert operations and I quite experimenting too early.
  • Use the ConnectApi sych as: ConnectApi.ChatterLike chatterLike = ConnectApi.ChatterFeeds.likeFeedElement(null, '0D5D0000000KuGh');

The problem with this is it runs as the current users context and I would need to look through this code (say for the three users) each time setting the user Context before running this command. This seems possible in Test mode but not in live mode using System.runAs()

  • Making a call to the REST API with something like ForceTK: forcetk.Client.prototype.ajax(/feed-items/feeditemid/likes", callback, null, "POST", null);

The problem here seems to be related once again to the context. In this case I would need to use OAuth but don't plan to have the credentials stored etc... to validate automatically.

I get that this is probably a security feature to avoid abuse however it would be really handy for me to use this. I want to use Auto-Liking as a sort of way to subscribe people to immediate updates on specific Chatter posts. And other than auto Commenting on their behalf (which seems a bit more messy) I can't think of how to make it happen.

Any thoughts on how to accomplish this are much appreciated.

1 Answer 1

3

You need to use DML on the FeedLike sObject. It's not queryable, but it's insertable and you can set the CreatedById field. Here's some sample code:

trigger likeFeedItem on FeedItem (after insert) {
    List<Id> likers = new List<Id>();
    likers.add('ADD_YOUR_LIST_OF_USER_IDS_HERE');

    List<FeedLike> likes = new List<FeedLike>();

    for (FeedItem f : trigger.new) {
        for (Id likerId : likers) {
            FeedLike fl = new FeedLike(feedItemId = f.id);
            fl.createdById = likerId;
            likes.add(fl);
        }
    }
    insert likes;
}
2
  • It's a good answer, but you kind of implicitly condone hard-coded ids. Can you rewrite your sample to follow a more "best-practice" approach?
    – Adrian Larson
    Aug 9, 2016 at 19:04
  • 1
    Thanks for the suggestion. I've generalized things a bit. Can you let me know if that's sufficient, or if you have any other suggestions?
    – alouie
    Aug 10, 2016 at 5:14

You must log in to answer this question.

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