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
VPrakashVPrakash 

Chatter Trigger to update the parent record feed with the child record feed?

Hi all,

 

I have a requirement where i need to insert the chatter feed on the parent(account) whenever a new chatter feed is inserted in the child(contact or opportunity) record.

 

Can this be done using the chatter Triggers? If so can you provide me the start point? 

 

 

Thanks in advance 

 

VPrakash

cloudcodercloudcoder

Yes definitely.

 

Create a trigger on FeedItem, filter it to execute only on Contact, then retrieve the Account id from the Contact and create a new FeedItem with the parentid = accountid.

 

Just make sure you filter to only execute on contact, otherwise you will get a recursive loop

VPrakashVPrakash

HI Quinton,

 

I tried exactly what you described here but there is no parent chatter feed insert when there is child chatter feed insert. Please look into the following trigger and let me know where I am going wrong. 

 

Parent(Agreement__c)

Child(Agreement_Line_Item__c)

 

trigger updateAgreementFeed on FeedItem (after insert) {

 

 string aliidprefix = Agreement_Line_Item__c.sObjectType.getDescribe().getKeyPrefix();
  feeditem [] agreementfeedtoupdate = new feeditem[]{};
  for ( feeditem feed : trigger.new){
  string aliid = feed.parentid;
  if(aliid.startsWith(aliidprefix)){
  id agreementid = [select Agreement__c from Agreement_Line_Item__c where id=:feed.parentid].Agreement__c;
  feeditem feeds = [SELECT Id, Type,CreatedById, CreatedBy.FirstName, CreatedBy.LastName,ParentId, Parent.Name,feedpost.Body, feedpost.Title, feedpost.LinkUrl,  feedpost.ContentData,feedpost.ContentFileName,
   (SELECT Id, FieldName, OldValue, NewValue FROM FeedTrackedChanges ORDER BY  Id DESC)                         
                          FROM feeditem WHERE id=:feed.id];
  feeditem fd = feeds.clone(false,true);
  fd.parentid = agreementid;
  agreementfeedtoupdate.add(fd); 
  }
  }
  insert agreementfeedtoupdate;
}

 

 

Thanks in advance

 

VPrakash

cloudcodercloudcoder

 

Looking at your code, the logic looks right, although I don't know if cloning a feeditem and related tracked changes as these will reference the original parentid. Your code updates the feeditem.parentid, but not the tracked changes which may cause issues.

 

 

 

VPrakashVPrakash

So should I even update the new feedtrack changes on the new feed item with the old feedtrack changes. You think this would work? Or PLease can you give me suggestions on this issue.

 

Thank you

 

VPrakash

cloudcodercloudcoder

Logically it probably doesn't make sense as a tracked change would reflect a change on your child object, not your parent. Start simple, and build up from there.

 

Also, top of my head, I don't think a feed track change will fire a trigger on FeedItem as they are different objects in the schema.