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
Matt Eck 15Matt Eck 15 

Append @ Mention to FeedComment

Hey All,
I am currently hitting an issue editing a FeedComment as it is inserted. What I would like to do is append an @ mention to the comment so the Case Owner will be alerted. We have a support portal and we can not rely on customers to use the @ mention functionality but without this, the support team is not being alerted that a new comment has been added.

Here is my current trigger handler:
public class FeedCommentTriggerHandler {
    public static void appendAlert(List<FeedComment> comments){
        Map<Id,Case> caseMap = new Map<Id, Case>();
        for(FeedComment comment : comments){
            if(comment.ParentId.getSObjectType().getDescribe().getName() == 'Case'){
                caseMap.put(comment.ParentId, new Case());
            }
        }

        if(!caseMap.isEmpty()){
            caseMap = new Map<Id, Case>([SELECT Id, OwnerId, Owner.Name FROM Case WHERE Id IN :caseMap.keySet()]);

            for(FeedComment comment : comments){
                if(caseMap.containsKey(comment.ParentId)){
                    comment.CommentBody += ('\n@' + caseMap.get(comment.ParentId).Owner.Name);
                }
            }
        }
    }
}

Currentlythis "works" to add an @ mention by adding literally "@<OwnerId.Name>" but this is just plain text and not actually notifying the owner.

I have found several articles stating I need to utilize the Connect API such as this one: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectapi_examples_post_comment_feed_element_2.htm

However, all the articles I find are stating how to create a net new comment with an @mention but I want to append an @mention to an existing comment as it comes in. Is this possible or am I not approaching this the correct way?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Matt,

>> https://developer.salesforce.com/blogs/engineering/2015/06/preserving-mentions-triggers.html

As mentioned in the above link, the problem I am adding below for quick reference, can you try checking this article once?
 
The Problem: Mentions Disappear in Triggers
Apex triggers are a powerful part of the Salesforce1 platform. One way to customize Chatter is to write a trigger that modifies the body of a feed item or comment when it’s posted.

For example, compliance apps that prevent certain words or phrases from being posted do exactly that: they substitute blocklisted words with replacement text like “*****”.

The drawback is that if there’s an @mention, it gets “stripped out” and displayed as plain text—the person or group you’re mentioning doesn’t get notified, which is a serious limitation.

To see the problem, let’s create a simple trigger that adds a disclaimer at the end of each post:

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.