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
cmlcml 

Need to add @mention through Apex code in chatter post

Hi I am writing a trigger on FeedItem which updates chatter post and add @mention. Now the issue is when we add @mention from Chatter UI, it comes as a hyperlink to User but when i do it through trigger code it doesn't come as hyperlink. I also tried to change feed type to 'LinkPost' but by that way we can only provide only one link to the post which is not the case here. Here i have to add more than one @mentions.
Is there any option by which i can achieve that? 

 

Thanks

missonjaraemissonjarae

Did you manage to find an answer to this? I'm looking for one as well....

gitguygitguy

I'm interested, too.

tggagnetggagne

Still no answer?

missonjaraemissonjarae

I opened a ticket with developer support and currently this functionality is not available/in the API.... I asked about any other workarounds to tag users/groups on the fly in a Chatter post based on dynamic data and they could not give me a workaround.  I'm surprised this is not available and will be adding an Idea to the IdeaExchange soon....

tggagnetggagne

"I'm surprised this is not available and will be adding an Idea to the IdeaExchange soon...."

 

I think I saw that already...  Vote it up

 

http://success.salesforce.com/ideaView?id=08730000000ZK3SAAW

 

I, too, am disappointed it's not supported.  As useful as Chatter can be, there are many things about it that seem half-baked.

 

If Google releases their API for posting messages an mentions before Salesforce does, I'll try to to figure out how to use that in our customers' applications.

missonjaraemissonjarae

Great, voted and commented.

 

 If you do figure something out with what google did, please post :) Thanks!

trmptrmp

So somehow the idea linked to above got merged into another idea that is now marked as delivered. I do not believe the ideas should have been merged together. Does anyone know if this funtionality has in fact been delivered or is this a mistake on the part of Sf Idea moderators?

missonjaraemissonjarae

To my knowledge, this has not been delivered.  Following up with premier support....

R4R4

I've come across the same issue.  This would be really helpful.

 

Is there a way that we could at least write HTML links into a FeedItem Body?  In visualforce, the apex:outputtext tag has an 'escape' boolean that controls how the component is rendered.  Just having this would be a HUGE step up.

trmptrmp

missonjarae,

 

Any luck with premier support?

 

Thanks!

missonjaraemissonjarae

Not sure about the escape boolean (though I'm pretty sure this isn't available yet).  But there IS an Apex @ mention pilot starting soon.  My org applied to be part of the pilot through our premier support rep so I would recommend reaching out to yours.  No word yet when we'll have access to this or if we even got in..

trmptrmp

Do you know if this pilot will include the ability to do this via the Web Services API as well?

missonjaraemissonjarae

Not sure.  Asked my rep and he's looking into it...stay tuned

missonjaraemissonjarae

Here you go- per support the sObject APIs DO NOT support @mentions.  The best way to do @mentions through the API is via the Chatter REST API.  Otherwise, like I said, an @ mention pilot for Apex is in the works.  Hope that helps!

ChrisOctagonChrisOctagon

"Is there a way that we could at least write HTML links into a FeedItem Body?"

 

I'm not sure I follow this... is this a VisualForce question? Any links that you include in a FeedItem body will already automatically get interpreted as links and rendered as links in the Chatter UI as well as returned as "link" segments from the Chatter REST API.

 

There is an upcoming pilot to expose the Chatter REST API functionality directly in Apex. The name of the pilot internally is "Connect in Apex" - if you contact support make sure to mention this name.

alouie_sfdcalouie_sfdc

Connect in Apex is now known as Chatter in Apex and will be generally available in the Summer '13 release. You can read more about it in the documentation (http://www.salesforce.com/us/developer/docs/apexcodepre/index.htm). Here's a simple code snippet that shows you how to @-mention someone using Chatter in Apex.

 

// Post a feed item that has an @-mention.

String communityId = null;
ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;
String userToMention = '005xx000001TDn3'
String subjectId = '005xx000001TDn3';

ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);

ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = userToMention;
messageInput.messageSegments.add(mentionSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = '. How are you?';
messageInput.messageSegments.add(textSegment);

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;

ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(communityId, feedType, subjectId, input, null);

 

MoggyMoggy
Has this ever been solved?
Issue is that the above sample only works for posting and in plain text, But chatterposts are RichText formatted and soon you try to update the FeedElement you lose the formatting ( like in Richtext to Longtext ) you get all the <b> </b> in the updtaed post

here is the sample that adds the @mention but lost the rich text formatting as from my understanding it creates first a complete new Item to then override the original item...

Actual problem is that if a user forgets the @mention the question is never been seen, so we aim to add the @ via apex

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);
                }
            
            }
        }
    
    }


}