easier Way to @mention - Winter 15 feature

Apex Jan 12, 2015

In Winter 15 Salesforce added with new features called Standard Invocable Actions in that chatterPost is useful for @mention users in easier manner

So here I create a Utils type class called feedMention which was responsible for chatter posting in record/user/group. It has four parameters viz

  1. text => which want to include @mention user/groups
  2. recId => recordId/UserId/GroupId
  3. userOrGroup => possible values 'user' or 'group' (case sensitive)
  4. sessId => userinfo.getSessionId() => pass session Id it wont vary (standard for all calls)
public class feedMention {    
    @future(callout = true)
    public static void newAddMention(string text, string recId, string userOrGroup, string sessId){
         String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
         if(userOrGroup == null){
             userOrGroup = 'user'; // valid input user or group
         }

         String url = salesforceHost + '/services/data/v32.0/actions/standard/chatterPost';
         system.debug('Url generated: '+url);
                
         HttpRequest req = new HttpRequest();        
         req.setMethod('POST');
         req.setEndpoint(url);
         req.setHeader('Content-type', 'application/json');
         req.setHeader('Authorization', 'OAuth ' + sessId);
        
         req.setBody('{"inputs": [{"text": "'+text+'", "SubjectNameOrId": "'+recId+'", "type": "'+userOrGroup+'"}] }');
         Http http = new Http();        
         HTTPResponse res = http.send(req);         
    }

}

Usage

feedMention.newAddMention('Testing from Console @[00590000000ekBr] you know what you can again @[00590000000ekBr] directly here itself easy?', '50090000002jc2G', 'user', userinfo.getSessionId());

Happy Coding!!!

Phanindra Mangipudi

Salesforce, Lightning Web Componets, Node.Js, Angular 2+, Bootstrap