Find your content:

Search form

You are here

Create Chatter post with mix of HyperLinks and text in the Body (in Apex code)

 
Share

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


Attribution to: Prafulla Patil

Possible Suggestion/Solution #1

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 ';   
      messageBodyInput.messageSegments.add(textSegmentInput1);
      ConnectApi.EntityLinkSegmentInput entityLinkSegmentInputContact = new ConnectApi.EntityLinkSegmentInput();
      entityLinkSegmentInputContact.entityId = cnt.Id;   
      messageBodyInput.messageSegments.add(entityLinkSegmentInputContact);
      ConnectApi.TextSegmentInput textSegmentInput2 = new ConnectApi.TextSegmentInput();
      textSegmentInput2.text = 'and closed ';     
      messageBodyInput.messageSegments.add(textSegmentInput2);
      ConnectApi.EntityLinkSegmentInput entityLinkSegmentInputOpportunity = new ConnectApi.EntityLinkSegmentInput();
      entityLinkSegmentInputOpportunity.entityId = opp.Id;  
      messageBodyInput.messageSegments.add(entityLinkSegmentInputOpportunity);        

      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.


Attribution to: thyryn

Possible Suggestion/Solution #2

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.


Attribution to: DerekLansing

Possible Suggestion/Solution #3

//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;

Attribution to: Chamil
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/732

My Block Status

My Block Content