You need to sign in to do that
Don't have an account?
jameskCA
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:
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?
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:
All Answers
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:
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?
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.
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 -
When I do the above via apex - the formatting is different and looks like this -
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 :(
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...
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
but it should be like
Hello Subject
done
or notAny 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