How can I add an @mention when creating a FeedItem via APEX?

I have found that this can be done via the Chatter API, and that there is a pilot program (http://developer.force.com/cookbook/recipe/connect-in-apex-pilot) to bring this to APEX, but I am not familiar at all with REST API. Is there a workaround that will allow me to add FeedItems with @mentions now?

Thanks

Answer

There is no other way other than invoking the chatter rest Api to post a chatter mention.

String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
        
String url =  salesforceHost + '/services/data/v23.0/chatter/feeds/record/' + RecordId + '/feed-items';
            
HttpRequest req = new HttpRequest();
            
req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
            
req.setBody('{ "body" : { "messageSegments" : [ { "type": "mention", "id" : "' + UserId + '" }, { "type": "text",  "text" : "' + ' ' + chatterAlert +  '" } ] } }');
Http http = new Http();
  
HTTPResponse res = http.send(req);

Attribution
Source : Link , Question Author : jackerman09 , Answer Author : metadaddy

Leave a Comment