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
jameskCAjameskCA 

How to do a chatter mention in Apex in 2016?

I'm hoping someone could provide some sample code or point me to the correct documentation.  I'm not a chatter expert, but I'm trying to accomplish the equivalent of saying @salesAdmin "there's a problem with project 1234." and have it show up in the users chatter feed.  I've done a lot of searching on the web and many of the posts are from 2013 or earlier.  It seems like there have been a lot of changes as all the example code I have tried doesn't seem to work and returns the error Method was removed after version 3x.0

A specific example I tried was:
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<ConnectApi.MessageSegmentInput>();

mentionSegmentInput.id = '005RR000000Dme9';
messageBodyInput.messageSegments.add(mentionSegmentInput);

textSegmentInput.text = 'Could you take a look?';
messageBodyInput.messageSegments.add(textSegmentInput);

feedItemInput.body = messageBodyInput;
feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
feedItemInput.subjectId = '0F9RR0000004CPw';

ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
Any ideas?  What is the best way to do this as of the most current release in 2016?
 
Best Answer chosen by jameskCA
James LoghryJames Loghry
Hi Jim,

I for one am SHOCKED that the ConnectAPI methods are confusing for you.

Kidding and sarcasm aside, it looks like the three parameter version of the postFeedElement was deprecated due to some changes in how files are handled in the Connect API.  Instead of passing in a file as the third parameter, it's now handled as part of the FeedItemInput object.

However, the two parameter method is still supported, so if you just remove the ", null", the method will work.  I successfully tested the following out on my developer org using API v36:
 
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<ConnectApi.MessageSegmentInput>();

mentionSegmentInput.id = 'USER OR GROUP ID';
messageBodyInput.messageSegments.add(mentionSegmentInput);

textSegmentInput.text = 'Could you take a look?';
messageBodyInput.messageSegments.add(textSegmentInput);

feedItemInput.body = messageBodyInput;
feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
feedItemInput.subjectId = 'SOME RECORD ID';

ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);

 

All Answers

James LoghryJames Loghry
Hi Jim,

I for one am SHOCKED that the ConnectAPI methods are confusing for you.

Kidding and sarcasm aside, it looks like the three parameter version of the postFeedElement was deprecated due to some changes in how files are handled in the Connect API.  Instead of passing in a file as the third parameter, it's now handled as part of the FeedItemInput object.

However, the two parameter method is still supported, so if you just remove the ", null", the method will work.  I successfully tested the following out on my developer org using API v36:
 
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<ConnectApi.MessageSegmentInput>();

mentionSegmentInput.id = 'USER OR GROUP ID';
messageBodyInput.messageSegments.add(mentionSegmentInput);

textSegmentInput.text = 'Could you take a look?';
messageBodyInput.messageSegments.add(textSegmentInput);

feedItemInput.body = messageBodyInput;
feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
feedItemInput.subjectId = 'SOME RECORD ID';

ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);

 
This was selected as the best answer
jameskCAjameskCA
James,
thanks for the reply with code.  Could you explain what the subjectId is?  I only want to send a user an updated message, it's not realted to a record.  Can I just leave that part out?  

For the mentionSegmentInput.id I was going to use the id of the user I wanted to send the message to, is that correct?  
James LoghryJames Loghry
Hi Jim,

The MentionSegmentInput.Id should be the id of the user, correct.  (It could also be a group to mention).

The subjectId is the Id of a the record your posting the chatter message on.  For instance, if I wanted to mention Billy on Case 123's case feed, I would use the Case record id as the subject Id.

Subject Ids can also be User Ids or Chatter Group Ids, so you can simply copy and paste the user Id into both, if that's what you want.  Otherwise, you noted something about "Project 1234", so you might want to put the Id of "Project 1234" in there instead.

 
jameskCAjameskCA

Okay, that makes sense.  Last question (I hope).  I've been doing chatter posts / mentions via process builder and it looks slightly different.  I'm just wondering if it's a different method to achieve this, or it's just because one is being created via process builder and one via apex.  Here is a picture of what the post looks like from the users profile -
User-added image


When I do the above via apex - the formatting is different and looks like this - 
User-added image

Are these functionally different? Or just the formatting?  

Thanks again for all your help.  The whole reason I need to use apex is process builder doesn't support cross object reference for person accounts :(
 

MoggyMoggy
Hi James,
is there a way to add an @mention without loosing the formatting, I foolowed all samples and played around a bit but if  a user ask a question and does not at mention anyone the question is not seen by the responsible group. We tried Flow / Process builder but the following apex was the best shot so far except that we lost the Richtext formatting and i was not able yet to set anywhere isRichtext to true...
public class addAddMention{


    public void checkItems(List<FeedItem> fi){
       String name='Salesforce Superusers';
       List<CollaborationGroup> cg=[select id from CollaborationGroup where Name=:name LIMIT 1];
       String groupId = cg[0].Id;
       String communityId = Network.getNetworkId();
       List<FeedItem> tochange= new List<FeedItem>(); 
        for(FeedItem f :fi){
            if(f.Type == 'QuestionPost'){
                if(!f.Body.contains('@')){
                    tochange.add(f);
                }
            }
        }
        if(!tochange.isEmpty()){
            for(FeedItem f : tochange){
                String feedElementId = f.Id;
                ConnectApi.FeedEntityIsEditable isEditable = ConnectApi.ChatterFeeds.isFeedElementEditableByMe(communityId, feedElementId);

                if (isEditable.isEditableByMe == true){

                    ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
                    ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
                    ConnectApi.QuestionAndAnswersCapabilityInput questionAndAnswersCapabilityInput = new ConnectApi.QuestionAndAnswersCapabilityInput();
                    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
                    ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
                    ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
                    mentionSegmentInput.id = GroupId;
                    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
                    textSegmentInput.text = f.body;
                    messageBodyInput.messageSegments.add(textSegmentInput);
                    messageBodyInput.messageSegments.add(mentionSegmentInput);
                    feedItemInput.body = messageBodyInput;
                    feedItemInput.capabilities = feedElementCapabilitiesInput;
                    feedElementCapabilitiesInput.questionAndAnswers = questionAndAnswersCapabilityInput;
                    questionAndAnswersCapabilityInput.questionTitle = f.title;
                    ConnectApi.FeedElement editedFeedElement = ConnectApi.ChatterFeeds.updateFeedElement(communityId, feedElementId, feedItemInput);
                }
            
            }
        }
    
    }


}

A trigger is fired on the post and calls the checkItems method, if the type of feedElement is of question and if there is no @ in the body 
the ConnectApi code adds the @Mention ( in our case a Group called Salesforce Superusers )

issue is the out put looks like 
User-added image
but it should be like

Hello Subject
done or not


Any ideas?

As far as i understand the whole documentation, we can't update the feedItem but replace it, but how to maintain the former body