0

I have written before insert trigger that concatenates Case Subject to the Chatter post when it is posted from the case record. But when I do that, @ mentions are getting stripped from the chatter post and the post is getting posted as a normal text, because of which the @mentions aren't getting any notifications.

Below is the apex helper class that I have written for the above functionality.

public with sharing class FeedItemHelper {

    Map<String, String> caseSubMap = new Map<String, String>();
    Set<String> chatterParentIdList = new Set<String>();

    public FeedItemHelper() {}

    public void caseSubjectConcat(List<FeedItem> feedItemList) {
        
        for(FeedItem feedPar : feedItemList) {
            chatterParentIdList.add(feedPar.ParentId);
        }
        
        List<Case> caseList = [SELECT Id, 
                                    Subject, 
                                    CaseNumber 
                                    FROM Case 
                                    WHERE Id In :chatterParentIdList];
        
        List<FeedItem> caseFeedItems = [SELECT Id,
                                            body,
                                            ParentId
                                            FROM FeedItem
                                            WHERE Parent.Type = 'Case'];

        if(caseList.size() > 0) {
            for(Case caseOne : caseList) {
                caseSubMap.put(caseOne.Id, caseOne.CaseNumber + ' : ' +caseOne.Subject);
            }
        }

        for(FeedItem feedOne : caseFeedItems) {
            feedOne.body = caseSubMap.get(feedOne.ParentId) + ' : ' + feedOne.body;
        }
    }
}

Can someone please let me know how the @mentions can be preserved while editing the body of the FeedItem record? Thanks in advance.

1

1 Answer 1

0

Using the salesforce's standard connectAPI and the connectAPI helper class by Alouie helped me solve this. It helped me retain the @mentions and hashtags in the chatter posts that were being posted.

My feedItem Helper class through which I am calling the ConnectAPI and the helper class method to process the chatter posts look like below.

public class FeedItemHelper {
    /**
    * Call this method from an after insert FeedItem trigger.
    * It updates the feed items passed in and preserves @mentions.
    */
    
    public static void edit(FeedItem[] feedItems) {
        
        
        Set<String> chatterParentIdList = new Set<String>();
        List<String> feedItemIds = new List<String>();

        String communityId = Network.getNetworkId();

        List<FeedItem> caseParentIdList = [SELECT Id, 
                                                Parent.Type, 
                                                ParentId 
                                                FROM FeedItem 
                                                WHERE Id IN :feedItems 
                                                AND Parent.Type = 'Case'];

        
        if(caseParentIdList.size() > 0) {
  
            for (FeedItem f : caseParentIdList) {
                feedItemIds.add(f.id);
                chatterParentIdList.add(f.ParentId);
            }
  
            // Get all feed items passed into the trigger (Step #1).
  
            ConnectApi.BatchResult[] results = ConnectApi.ChatterFeeds.getFeedElementBatch(communityId, feedItemIds);
  
            for (ConnectApi.BatchResult result : results) { 
                if (result.isSuccess()) {
                    Object theResult = result.getResult();
                    if (theResult instanceof ConnectApi.FeedItem) {
                        ConnectApi.FeedItem item = (ConnectApi.FeedItem) theResult;
                        System.debug('The feedItem call results ' + item);
  
                        // Convert message segments into input segments (Step #2a).
  
                        ConnectApi.FeedItemInput input = ConnectApiHelper.createFeedItemInputFromBody(item.body);

                        System.debug('printing input ' + input);
  
                        // Modify the input segments as you see fit (Step #2b).
  
                        modifyInput(input, item.id);
  
                        // Update the feed item (Step #2c).
                        // We need to update one feed item at a time because there isn't a batch update method yet.
  
                        ConnectApi.ChatterFeeds.updateFeedElement(communityId, item.id, input);
  
                    } else {
                        // Do nothing. Posting other feed element types isn't supported.
                        System.debug('other feed element types isn\'t supported');
                    }
  
                } else {
                        System.debug('Failure in batch feed element retrieval: ' + result.getErrorMessage());
                }
            }
        }
    }
  
    /**
    * Update the feed item input here!
    */
  
    public static void modifyInput(ConnectApi.FeedItemInput input, String itemId) {
  
        //This example appends text to the feed item.
        String fedParentId;
        FeedItem fed = [SELECT Id, 
                                ParentId 
                                FROM FeedItem 
                                WHERE Id = :itemId];
            
        if (fed != null) {
            fedParentId = fed.ParentId;
        }

        Case caseFed = [SELECT Id, 
                                Subject, 
                                CaseNumber 
                                FROM Case 
                                WHERE Id = :fedParentId];
  
        ConnectApi.TextSegmentInput textInput = new ConnectApi.TextSegmentInput();
            
        textInput.text = caseFed.CaseNumber + ' : ' +caseFed.Subject;
          //textInput.text = 'nn---nDisclaimer: Modified in a trigger.';
  
        input.body.messageSegments.add(textInput);
    } 
}

And the ConnectAPI Helper class by Alouie which processes the chatter posts, and the test class to cover that ConnectAPI helper class can be found in the github by clicking this github URL

Hope this helps someone.

You must log in to answer this question.

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