[SalesForce] FeedItem to record with multiple @mention

I am trying to achieve feed item to record with multiple @ mentions when status of record goes to Complete.Here is the sample code which creates a feed item to record with single @ mention.

         ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
         ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
         ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
         ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
         messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
         textSegmentInput.text = 'Customer has completed the application';
          messageBodyInput.messageSegments.add(textSegmentInput);

         mentionSegmentInput.id = a.PermID; //a.PermID is the User Id
         messageBodyInput.messageSegments.add(mentionSegmentInput);

         feedItemInput.body = messageBodyInput;
         feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
         // Use a record ID for the subject ID.
          feedItemInput.subjectId = a.Id ; //a.Id is the record ID
           ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput, null);

Best Answer

To add more mentions, simply create more ConnectApi.MentionSegmentInput objects and add them to the messageBodyInput.messageSegments list before you call ConnectApi.ChatterFeeds.postFeedElement(). I agree with cricketlang's ConnectApiHelper suggestion too!

Related Topic