Apex - Accurately Identify users tagged in Chatter comments and set them as followers automatically...












4















This question already has an answer here:




  • FeedItem Trigger - Parse Mentions?

    2 answers



  • Retrieve UserId of @mention user from chatter post in Apex

    1 answer




I already know how to set someone to auto-follow a record based on specific criteria (record created, modified, etc) by inserting records into the EntitySubscription object.



What I am trying to accomplish is a process in which, for a specific internal object, anyone who is @Mentioned on a chatter text post that is related to that object type will automatically become a follower of the record that they were mentioned in.



I can query for the information I need from the FeedItem object with [SELECT Id,ParentId,Body FROM FeedItem WHERE Type = 'TextPost'], but the body looks like this:




@Person One @Peron Two @Person Number Three can one of you provide me with a specific account that you are referring to?






Issue: I don't use regex often, but figured it would be useful here, however my problem is that there isn't really any clear 'line in the sand' in the Body of the FeedItem for me to tell the regex when to start/stop and guarantee that I've captured the full name of the user, and without the full name of the user I can't properly query for their User Id and insert their EntitySubscription record(s).



In my example above you'll note that I can't just rely on the two words that immediately follow an @ symbol, because there are some users who have a multi part name (Morgan Del Ray, as an example). I was hoping that the @Mentions would be formatted as @[Mention] so that I could use the [ ] as my limiters, but alas it is not.





Question: Is there any way I can guarantee this functionality will capture the full name using an advanced form of regex? Or is there
perhaps another object that actually stores the Id references of the
users that were tagged in a post? Somehow Salesforce manages to turn
the @Mention tag into a link to the User, but I don't see that in the
FeedItem object so I'm not sure where (or if) I can reference that
data directly.










share|improve this question















marked as duplicate by gNerb, Morgan Marchese, battery.cord, codeyinthecloud, David Reed apex
Users with the  apex badge can single-handedly close apex questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
41 mins ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    4















    This question already has an answer here:




    • FeedItem Trigger - Parse Mentions?

      2 answers



    • Retrieve UserId of @mention user from chatter post in Apex

      1 answer




    I already know how to set someone to auto-follow a record based on specific criteria (record created, modified, etc) by inserting records into the EntitySubscription object.



    What I am trying to accomplish is a process in which, for a specific internal object, anyone who is @Mentioned on a chatter text post that is related to that object type will automatically become a follower of the record that they were mentioned in.



    I can query for the information I need from the FeedItem object with [SELECT Id,ParentId,Body FROM FeedItem WHERE Type = 'TextPost'], but the body looks like this:




    @Person One @Peron Two @Person Number Three can one of you provide me with a specific account that you are referring to?






    Issue: I don't use regex often, but figured it would be useful here, however my problem is that there isn't really any clear 'line in the sand' in the Body of the FeedItem for me to tell the regex when to start/stop and guarantee that I've captured the full name of the user, and without the full name of the user I can't properly query for their User Id and insert their EntitySubscription record(s).



    In my example above you'll note that I can't just rely on the two words that immediately follow an @ symbol, because there are some users who have a multi part name (Morgan Del Ray, as an example). I was hoping that the @Mentions would be formatted as @[Mention] so that I could use the [ ] as my limiters, but alas it is not.





    Question: Is there any way I can guarantee this functionality will capture the full name using an advanced form of regex? Or is there
    perhaps another object that actually stores the Id references of the
    users that were tagged in a post? Somehow Salesforce manages to turn
    the @Mention tag into a link to the User, but I don't see that in the
    FeedItem object so I'm not sure where (or if) I can reference that
    data directly.










    share|improve this question















    marked as duplicate by gNerb, Morgan Marchese, battery.cord, codeyinthecloud, David Reed apex
    Users with the  apex badge can single-handedly close apex questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    41 mins ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      4












      4








      4








      This question already has an answer here:




      • FeedItem Trigger - Parse Mentions?

        2 answers



      • Retrieve UserId of @mention user from chatter post in Apex

        1 answer




      I already know how to set someone to auto-follow a record based on specific criteria (record created, modified, etc) by inserting records into the EntitySubscription object.



      What I am trying to accomplish is a process in which, for a specific internal object, anyone who is @Mentioned on a chatter text post that is related to that object type will automatically become a follower of the record that they were mentioned in.



      I can query for the information I need from the FeedItem object with [SELECT Id,ParentId,Body FROM FeedItem WHERE Type = 'TextPost'], but the body looks like this:




      @Person One @Peron Two @Person Number Three can one of you provide me with a specific account that you are referring to?






      Issue: I don't use regex often, but figured it would be useful here, however my problem is that there isn't really any clear 'line in the sand' in the Body of the FeedItem for me to tell the regex when to start/stop and guarantee that I've captured the full name of the user, and without the full name of the user I can't properly query for their User Id and insert their EntitySubscription record(s).



      In my example above you'll note that I can't just rely on the two words that immediately follow an @ symbol, because there are some users who have a multi part name (Morgan Del Ray, as an example). I was hoping that the @Mentions would be formatted as @[Mention] so that I could use the [ ] as my limiters, but alas it is not.





      Question: Is there any way I can guarantee this functionality will capture the full name using an advanced form of regex? Or is there
      perhaps another object that actually stores the Id references of the
      users that were tagged in a post? Somehow Salesforce manages to turn
      the @Mention tag into a link to the User, but I don't see that in the
      FeedItem object so I'm not sure where (or if) I can reference that
      data directly.










      share|improve this question
















      This question already has an answer here:




      • FeedItem Trigger - Parse Mentions?

        2 answers



      • Retrieve UserId of @mention user from chatter post in Apex

        1 answer




      I already know how to set someone to auto-follow a record based on specific criteria (record created, modified, etc) by inserting records into the EntitySubscription object.



      What I am trying to accomplish is a process in which, for a specific internal object, anyone who is @Mentioned on a chatter text post that is related to that object type will automatically become a follower of the record that they were mentioned in.



      I can query for the information I need from the FeedItem object with [SELECT Id,ParentId,Body FROM FeedItem WHERE Type = 'TextPost'], but the body looks like this:




      @Person One @Peron Two @Person Number Three can one of you provide me with a specific account that you are referring to?






      Issue: I don't use regex often, but figured it would be useful here, however my problem is that there isn't really any clear 'line in the sand' in the Body of the FeedItem for me to tell the regex when to start/stop and guarantee that I've captured the full name of the user, and without the full name of the user I can't properly query for their User Id and insert their EntitySubscription record(s).



      In my example above you'll note that I can't just rely on the two words that immediately follow an @ symbol, because there are some users who have a multi part name (Morgan Del Ray, as an example). I was hoping that the @Mentions would be formatted as @[Mention] so that I could use the [ ] as my limiters, but alas it is not.





      Question: Is there any way I can guarantee this functionality will capture the full name using an advanced form of regex? Or is there
      perhaps another object that actually stores the Id references of the
      users that were tagged in a post? Somehow Salesforce manages to turn
      the @Mention tag into a link to the User, but I don't see that in the
      FeedItem object so I'm not sure where (or if) I can reference that
      data directly.





      This question already has an answer here:




      • FeedItem Trigger - Parse Mentions?

        2 answers



      • Retrieve UserId of @mention user from chatter post in Apex

        1 answer








      apex feeditem entitysubscription mentions






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      gNerb

      5,691734




      5,691734










      asked 1 hour ago









      Morgan Marchese

      1,455426




      1,455426




      marked as duplicate by gNerb, Morgan Marchese, battery.cord, codeyinthecloud, David Reed apex
      Users with the  apex badge can single-handedly close apex questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      41 mins ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by gNerb, Morgan Marchese, battery.cord, codeyinthecloud, David Reed apex
      Users with the  apex badge can single-handedly close apex questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      41 mins ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          5














          I'll just repost the code I used for a similar requirement, without including any of the additional commentary I wrote here. I can't really speak to how well the ConnectAPI approaches work nor their speed. But I know this approach works because I have implemented it in a live environment.



          public with sharing class FeedItemService
          {
          public static final String USER_PREFIX = SObjectType.User.getKeyPrefix();
          public static final String EXPRESSION = '\{@(' + USER_PREFIX +'[0-9a-zA-Z]{15})\}';
          public static Pattern RAW_MENTION = Pattern.compile(EXPRESSION);

          public static void parse(List<FeedItem> feedItems)
          {
          for (Mention mention : (List<Mention>)JSON.deserialize(
          JSON.serialize(feedItems), List<Mention>.class
          )){
          for (Id userId : mention.getUserIds())
          {
          // do stuff
          }
          }
          }

          public class Mention
          {
          final String rawBody;
          final Id parentId;
          Set<Id> getUserIds()
          {
          Set<Id> userIds = new Set<Id>();
          Matcher m = RAW_MENTION.matcher(rawBody);
          while (m.find()) userIds.add(m.group(1));
          return userIds;
          }
          }
          }





          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            I'll just repost the code I used for a similar requirement, without including any of the additional commentary I wrote here. I can't really speak to how well the ConnectAPI approaches work nor their speed. But I know this approach works because I have implemented it in a live environment.



            public with sharing class FeedItemService
            {
            public static final String USER_PREFIX = SObjectType.User.getKeyPrefix();
            public static final String EXPRESSION = '\{@(' + USER_PREFIX +'[0-9a-zA-Z]{15})\}';
            public static Pattern RAW_MENTION = Pattern.compile(EXPRESSION);

            public static void parse(List<FeedItem> feedItems)
            {
            for (Mention mention : (List<Mention>)JSON.deserialize(
            JSON.serialize(feedItems), List<Mention>.class
            )){
            for (Id userId : mention.getUserIds())
            {
            // do stuff
            }
            }
            }

            public class Mention
            {
            final String rawBody;
            final Id parentId;
            Set<Id> getUserIds()
            {
            Set<Id> userIds = new Set<Id>();
            Matcher m = RAW_MENTION.matcher(rawBody);
            while (m.find()) userIds.add(m.group(1));
            return userIds;
            }
            }
            }





            share|improve this answer


























              5














              I'll just repost the code I used for a similar requirement, without including any of the additional commentary I wrote here. I can't really speak to how well the ConnectAPI approaches work nor their speed. But I know this approach works because I have implemented it in a live environment.



              public with sharing class FeedItemService
              {
              public static final String USER_PREFIX = SObjectType.User.getKeyPrefix();
              public static final String EXPRESSION = '\{@(' + USER_PREFIX +'[0-9a-zA-Z]{15})\}';
              public static Pattern RAW_MENTION = Pattern.compile(EXPRESSION);

              public static void parse(List<FeedItem> feedItems)
              {
              for (Mention mention : (List<Mention>)JSON.deserialize(
              JSON.serialize(feedItems), List<Mention>.class
              )){
              for (Id userId : mention.getUserIds())
              {
              // do stuff
              }
              }
              }

              public class Mention
              {
              final String rawBody;
              final Id parentId;
              Set<Id> getUserIds()
              {
              Set<Id> userIds = new Set<Id>();
              Matcher m = RAW_MENTION.matcher(rawBody);
              while (m.find()) userIds.add(m.group(1));
              return userIds;
              }
              }
              }





              share|improve this answer
























                5












                5








                5






                I'll just repost the code I used for a similar requirement, without including any of the additional commentary I wrote here. I can't really speak to how well the ConnectAPI approaches work nor their speed. But I know this approach works because I have implemented it in a live environment.



                public with sharing class FeedItemService
                {
                public static final String USER_PREFIX = SObjectType.User.getKeyPrefix();
                public static final String EXPRESSION = '\{@(' + USER_PREFIX +'[0-9a-zA-Z]{15})\}';
                public static Pattern RAW_MENTION = Pattern.compile(EXPRESSION);

                public static void parse(List<FeedItem> feedItems)
                {
                for (Mention mention : (List<Mention>)JSON.deserialize(
                JSON.serialize(feedItems), List<Mention>.class
                )){
                for (Id userId : mention.getUserIds())
                {
                // do stuff
                }
                }
                }

                public class Mention
                {
                final String rawBody;
                final Id parentId;
                Set<Id> getUserIds()
                {
                Set<Id> userIds = new Set<Id>();
                Matcher m = RAW_MENTION.matcher(rawBody);
                while (m.find()) userIds.add(m.group(1));
                return userIds;
                }
                }
                }





                share|improve this answer












                I'll just repost the code I used for a similar requirement, without including any of the additional commentary I wrote here. I can't really speak to how well the ConnectAPI approaches work nor their speed. But I know this approach works because I have implemented it in a live environment.



                public with sharing class FeedItemService
                {
                public static final String USER_PREFIX = SObjectType.User.getKeyPrefix();
                public static final String EXPRESSION = '\{@(' + USER_PREFIX +'[0-9a-zA-Z]{15})\}';
                public static Pattern RAW_MENTION = Pattern.compile(EXPRESSION);

                public static void parse(List<FeedItem> feedItems)
                {
                for (Mention mention : (List<Mention>)JSON.deserialize(
                JSON.serialize(feedItems), List<Mention>.class
                )){
                for (Id userId : mention.getUserIds())
                {
                // do stuff
                }
                }
                }

                public class Mention
                {
                final String rawBody;
                final Id parentId;
                Set<Id> getUserIds()
                {
                Set<Id> userIds = new Set<Id>();
                Matcher m = RAW_MENTION.matcher(rawBody);
                while (m.find()) userIds.add(m.group(1));
                return userIds;
                }
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                Adrian Larson

                104k19112235




                104k19112235















                    Popular posts from this blog

                    サソリ

                    広島県道265号伴広島線

                    Setup Asymptote in Texstudio