Chatter Feed Migration in salesforce

Chatter Feed Migration in salesforce

This Blog gives you a brief understanding of how to post a chatter feed from child records feed to parent Record feed with “@mention” tag in Salesforce. This describes that when ­ever we post some information on the Child record feed it should auto-populate on the parent record feed.

A mention is a “@” character followed by a user or group name. 

For Example If we have two contacts related with the Account, enter the feed in contact1 as” @k kumar  Hello Absyz1″ and contact2 as” @K KK Hello absyz2 ” these two feeds are populated in Related Account as Shown in the below figure.

Account Feed

For achieving this we are following these two steps.

1.Get the feed from child Records with @mention

Reference Links for Feed item: These links helps us to get the Feed item with the reference of feed elements. From Contacts Feed, we will get the “@mention” and “text” from the feed elements that are under the feed item body. Using the ConnectApi.MessageSegmenttype, we will get the mention user id and text message that was entered in the feed item post on the child record.

https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_responses_feed_item.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connectapi_input_messageSegmentInput.htm

2.Posting the Feed to Parent Records with @mention.

For posting the feed item with @mention in a parent record feed we use below link for Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectapi_examples_post_feed_element_mention.html

Here,Feed Item Input request body includes a body property that is a Message Body Input request body. Where the Message Body Input request body includes a message Segments property that has one Message Segment Input: Text request body and one Message Segment Input: Mention request body.

Here below code is developed to achieve the functionality.

Trigger Handler:

[sourcecode language=”java”]

public class ContactFeedItemHandler { public static Boolean isFirstTime = true; public static void contactFeedItemmethod(list feedList){ mapaccidmap =new map(); setcontactid = new set(); for(FeedItem fi:feedList){ contactid.add(fi.ParentId); } for(contact c :[select id,AccountId from contact where id in :contactid]){ accidmap.put(c.id,c.AccountId); } String Id; String temp; Integer x = 0; for(FeedItem fitem:feedList){ for(ConnectApi.FeedElement f:ConnectApi.ChatterFeeds.getFeedElementsFromFeed(Network.getNetworkId(), ConnectApi.FeedType.Record,fitem.ParentId ).elements){ if (f instanceof ConnectApi.FeedItem) { for(ConnectApi.MessageSegment fb:f.body.messageSegments){ if(x==0) { if (fb.type == ConnectApi.MessageSegmentType.Text) { temp=((ConnectApi.TextSegment)fb).text; system.debug(‘Input Text::’+temp); x=x+1; } if(fb.type == ConnectApi.MessageSegmentType.Mention) { Id=((ConnectApi.MentionSegment)fb).record.id; system.debug(‘Input Id::’+Id); } } } } } } ContactFeedmet(Id,temp,accidmap,feedList); } public static void ContactFeedmet(String Id ,String temp, mapaccidmap,list feedList){ //Id=id1; //temp=temp1; for(FeedItem fi:feedList){ 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(); mentionSegmentInput.id = Id; // Set @mention messageBodyInput.messageSegments.add(mentionSegmentInput); textSegmentInput.text = temp; messageBodyInput.messageSegments.add(textSegmentInput); feedItemInput.body = messageBodyInput; feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem; feedItemInput.subjectId = accidmap.get(fi.ParentId);//UserInfo.getUserId(); ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput); } } }

[/sourcecode]

contactFeedItemmethod() method is help to get account ids related to contacts and using with ConnectApi.FeedElement will get the feed item elements like body,mention,text segment and messageSegments. messageSegments contains type of segments like mention, link, hashtag and Text etc. contactFeedItemmethod() here we are using variables like Id & temp. Where, Id is used to get the @mention user id and temp is used to get the text from the feed item body of child. We are using connect Api to post the feed item. contactFeedmet() method is used to post the chatter feed into parent record. Here we will map the Id to the mentionSegmentInput and temp to textSegmentInput.

accidmap.get(fi.ParentId) is used to map the parent Record Id to subjectId.

Trigger :

[sourcecode language=”java”] trigger ContactFeedItemTrigger on FeedItem (after insert) { if(Trigger.isInsert && Trigger.isAfter){ if(ContactFeedItemHandler.isFirstTime) { ContactFeedItemHandler.isFirstTime = false; ContactFeedItemHandler.contactFeedItemmethod(trigger.new); } } } [/sourcecode]

OutPut:

Here, we achieved how to post the related contacts feed into Parent account feed.

Please refer Screenshots:

Contact 1 with feed item

contact2 with the feed item

Account with related contacts feed items.

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
salesforce for real estate
Salesforce for Real Estate: How Salesforce CRM Can Help Realtors
salesforce consumer goods cloud store setup
A Detailed Look into Salesforce Consumer Goods Cloud Store Setup
Scroll to Top