I am new to Apex, and I was hoping that I could get help. My goal is to auto subscribe a user to chatter on parent object. For example, I have two object, Story, and Story Task. Story is the parent object and Story task is the child. Story has the chatter feed enabled, Story Task does not. What I am trying to achieve is that if an Owner__c is populated on Story_Task__c then update the parent object Story__c chatter feed by subscribing the Story Task's Owner to the Story's chatter feed.

Here is something I have created, but not exactly sure how I can assign the owner to update the parent's chatter feed. Any pointers would be greatly appreciated. Thanks

trigger StoryTaskOwnerChatterSubscription on Story_Task__c (after insert, after undelete) {
        List<EntitySubscription> stEntitySubList = new List<EntitySubscription>();

            for(Story_Task__c stt:trigger.new){
                List<Story__c> story = new List<Story__c>();

                List<Story_Task__c> stask =[Select SubscriberId From Story_Task__c Where id =:story.Id];

                if(task.Owner__c != Null){
                    EntitySubscription stEntity = new EntitySubscription();
                    stEntity.ParentId = stask.Id;
                    stEntity.SubscriberId = stask.Owner__c;


            insert stEntitySubList;
  • Does the trigger you have work? How does the behavior you observe differ from that you expect?
    – Adrian Larson
    Mar 20 '17 at 15:13
  • Not it does not. I essentially get a message saying that I can not perform this action as Story_Task__c does not have chatter feed enabled. This makes me think that my code is trying to assign the owner to the Story_Task_c chatter feed instead of Story's feed. That is why I am thinking that I am not specifying correctly that I want to update the parent object Story's entity subscription.
    – Ivo Dimov
    Mar 20 '17 at 15:17
  • Please include your error message here verbatim. Error message text makes your posts significantly more specific and well defined.
    – Adrian Larson
    Mar 20 '17 at 15:24

First of all, don't query in a loop. Never.

Second, if you're trying to subscribe someone to the parent Story__c feed, you need to set that as the ParentId on the EntitySubscription record.

List<EntitySubscription> subscriptions = new List<EntitySubscription>();
for (Story_Task__c task : trigger.new)
    subscriptions.add(new EntitySubscription(
        ParentId = task.Story__c, SubscriberId = task.Owner__c
insert subscriptions;

Note that instantiating an SObject record via name=value pairs in the constructor is significantly faster than instantiating it and then setting each field individually.

You might need to add de-duplication to your approach, but I'm not entirely sure on that.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.