How to create a chatter post in the body I want to add Links in between. (in Apex code)

its not complete link or complete plain text. I am looking at mix of both

Example Chatter post -

just has call with John[link to contact here] and closed opportunity name[link to opportunity ]

Update - its not simple chatter post with LinkUrl (the one suggested in reply is very easy)

I need final chatter post to look like as follows - (basically with hyperlinks)

just has call with John and closed opportunity name

  • 2
    You cannot (yet) use rich HTML in chatter posts. You'll need to include the full URL text as part of the post, as in Derek's answer below. Aug 22, 2012 at 17:36

3 Answers 3


So, I haven't actually tried this, but here's what I would try first:

FeedItem myPost = new FeedItem();
myPost.Parentid = object.Id; //user's Id I'm assuming
myPost.body = 'Text here http://www.internets.com more text http://www.theinternets.com';
insert myPost;

Obviously, you'd have to get the Id of the object you're posting to and the links to the contact and opportunity.

  • I tested this and it seems to work fine. Aug 22, 2012 at 1:18
  • 1
    I think what he wants is more complicated than a simple link. He wants to be able to do something like myPost.body = '@Derek Lansing - Text here'; and have the resulting post change the @Derek Lansing to a link like it would if you posted the message through the Chatter UI. There's probably more to it, because it would have to make it into the feed of the linked Object (e.g., into Derek Lansing's User Profile feed). Aug 22, 2012 at 1:39
  • @PeterKnolle - rich HTML isn't possible today in chatter posts. Derek's answer is really as good as it gets for now. Aug 22, 2012 at 18:17
  • So you'd have to manually create the link for the User Name and then also create a duplicate post for their user profile feed. The ajax post of a normal chatter post of @[Peter Knolle] - Hey there! posts a param to /chatter/handlers/feeditems of text={@005d0000000xhgo} - Hey there!, so I guess that the standard Salesforce chatter code parses that ID out to create the link and creates relevant posts in the referenced objects' feeds (user profile in this case). It'd be nice to be able to do post.Body='{@005d0000000xhgo} - Hey there!' and have SF linkify it and create the related object post. Aug 22, 2012 at 18:32

Have you considered using ConnectApi classes. Here is example that generates following post :

Here is Apex code:

 ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();            
    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();   
    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

    Contact cnt = new Contact (FirstName='John', LastName='Smith');
    insert cnt;

    Opportunity opp = new Opportunity(Name='Opp-01', StageName='Identified', CloseDate= System.today()); 
    insert opp;

      ConnectApi.TextSegmentInput textSegmentInput1 = new ConnectApi.TextSegmentInput();
      textSegmentInput1.text = 'just has call with ';   
      ConnectApi.EntityLinkSegmentInput entityLinkSegmentInputContact = new ConnectApi.EntityLinkSegmentInput();
      entityLinkSegmentInputContact.entityId = cnt.Id;   
      ConnectApi.TextSegmentInput textSegmentInput2 = new ConnectApi.TextSegmentInput();
      textSegmentInput2.text = 'and closed ';     
      ConnectApi.EntityLinkSegmentInput entityLinkSegmentInputOpportunity = new ConnectApi.EntityLinkSegmentInput();
      entityLinkSegmentInputOpportunity.entityId = opp.Id;  

      feedItemInput.body = messageBodyInput;
      feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
      feedItemInput.subjectId = opp.Id; 

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

One of the advantages of using ConnectApi is also that it allows to post feed element with a @mention.

  • You're talking about @mention but you're using ConnectApi.EntityLinkSegmentInput which is actually not the same. There's a ConnectApi.MentionSegmentInput object to properly mention someone/some group.
    – Link14
    Mar 2, 2023 at 20:26

//Adding a Link post

FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = 'Enter post text here';
post.LinkUrl = 'http://www.someurl.com'; //This is the way of adding Link to chatter feed
insert post;

You must log in to answer this question.

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