In my previous post I introduced Chatter Bot for Groups, a way to easily listen for Chatter Groups being created or updated or when users join or leave a group.

One of the use cases we are able to support with Chatter Bot for Groups is automating Chatter posts to users, such as welcome posts when a community user joins a specific group. However, those posts were always authored by the current user which means the joining user (or group manager who added the user) would be the author. Sometimes it can be weird when you see in your Chatter Feed that you’re talking to yourself.

chatter-bot-post-author-you

“It just looks goofy,” Brian Bauder remarks.

Wouldn’t it be nice if that automated post had been authored by someone else, like the community manager of the group?

chatter-bot-post-author-not-you

IdeaExchange

Todd Barry submitted this idea to IdeaExchange, “Post to Chatter on behalf of Someone Else“. Until this becomes easier to do, I’ve developed the next best thing (biased opinion of course!).

Introducing Chatter Bot for Feeds

This solution, available on GitHub, enables you to post Chatter messages authored by any user. The best part is that you can automate these messages with Process Builder or Flow! Couple this with Chatter Bot for Groups and Chatter Bot for Topics and you can develop some very rich experiences for your users.

Below is an example where I had my Chatter Expert user give a shout-out to Chatter Bot giving it some advice on how to create rich-text messages. This was activated when Chatter Bot joined my “Success – Getting Started” group in my org. Chatter Bot feels all warm and fuzzy inside because Chatter Expert is giving it some attention. Chatter Expert is chillin’ and relaxin’ because they have automated this task. Win-Win. And if Chatter Bot comments on the post then Chatter Expert can engage. Win-Win-Win.

chatter-bot-posted-as-another-user.jpg

To make the Chatter post we can automate with Process Builder. Chatter Bot for Feeds includes an invocable Apex class that lets you specify:

  • Who is making the post (user id)
  • Where the post will be made (user, group, record id)
  • What the message will be (plain or rich text)

chatter-bot-feeds-process-builder.png

You have two options for specifying the message:

  • Use the Message input variable and type whatever you want, or
  • Use the Email Template Unique Name input variable

The Message option is good for quick testing or for simple messages. For longer or more complex messages then I recommend the Email Template option.

By using an Email Template you can use the “Send Test and Verify Merge Fields” button on template page to preview what the HTML / rich-text will look like as well as have the option to use merge fields if the Chatter post is in context of a specific record. Text or Custom HTML template types make good choices here. Note, we don’t need an Email Alert because we aren’t sending this as an actual email to the recipient but rather the template is a convenient way to maintain and preview the message.

chatter-bot-post-message-template-preview.png

With either message option you choose, note that Chatter posts only support a subset of HTML as shown below:

chatter-post-rich-text-html.PNG

You all are full of creative ideas for how you can use this at your company. For the moment, all I can think of is implementing some kind of drip campaign for onboarding new employees or new customers that joined a community. I’d love to hear in the comments what you all come up with!

Responsibility

As the #AwesomeAdmin that you are, you understand that with great power comes great responsibility. Impersonate other users on Chatter with care and always have consent from the intended author before automating posts by them. Thanks!

impersonate-user-with-care-superpower.png

Design Considerations

The GitHub project will give you all the information you need to get the solution deployed in your org and up and running. The rest of this post is about how and why I implemented the solution the way I did. So if you’re interested in all the details under the hood then read on.

For this project I had four key design requirements:

  1. Must be easy to use and launchable by Process Builder or Flow (declarative)
  2. Must support setting the author to someone other than the running user
  3. Must support rich-text
  4. Must support @ mentions

Let’s walkthrough our options on the platform for making Chatter posts and see how close they satisfy those requirements. We will see why I had to develop a custom solution.

Option 1: Post to Chatter using standard features in Process Builder and Flow

That would definitely be the simplest #NoCode approach, but those actions run as the current user and would not allow us to set an arbitrary author. Also, although they do support @ mentions they do not support rich-text formatting. The message is treated as plain text.

chatter-bot-feeds-standard-pb-flow-no-rich-text.png

  • Supports rich-text formatting? No.
  • Supports @ mentions? Yes.
  • Supports setting post author? No.

Option 2: Create FeedItem and set CreatedById to desired author

Creating a FeedItem like any other SObject in Apex would be an easy #LowCode option for supporting setting the author and supporting rich-text formatting, but it doesn’t support @ mentions.

Example:


FeedItem item = new FeedItem(
parentId = UserInfo.getUserId(), // where to post message
createdById = '005j000000Bz31U', // author to impersonate
body = '<p><b>FeedItem</b> supports <i>rich text</i> but not @ mentions:</p> @[0F9j00000008TNc] @0F9j00000008TNc {0F9j00000008TNc} @{0F9j00000008TNc}',
isRichText = true
);
insert item;

view raw

FeedItem.java

hosted with ❤ by GitHub

chatter-bot-feeds-feeditem-no-mentions.png

  • Supports rich-text formatting? Yes.
  • Supports @ mentions? No.
  • Supports setting post author? Yes.

Option 3: Use ConnectApi like the other hipster developers

Yes, Chatter in Apex provides many of the Chatter REST API functionality in Apex via the ConnectApi namespace. Though it may be powerful, it is unwieldly and a difficult API to use in Apex. Check out the examples for posting rich-text or mentions.

Thankfully some Force.com Developers, notably alouie and Jan Aertgeerts, provided a simpler way and have since open sourced a helper utility to make using this API a lot more manageable.

With the ConnectApiHelper class we now have easy support for rich-text formatting and @ mentions in Apex. But there’s a catch. The ConnectApi makes all requests to the Chatter REST API as the current user. We cannot specify the author this way.

  • Supports rich-text formatting? Yes.
  • Supports @ mentions? Yes.
  • Supports setting post author? No.

problem-think-solution

Let’s Get Creative

Between options 2 and 3 we are very close to meeting our requirements. So I thought maybe we can go with a hybrid approach.

Step 1: Insert the FeedItem record with CreatedById equal to our desired author.

Step 2: Use ConnectApi to update the Chatter post’s Body to include rich-text and @ mentions.

Now, in order for us to be able to set the FeedItem.CreatedById field we need our context user to have the system permission “Insert System Field Values for Chatter Feeds”.

insert-system-field-values-for-chatter-feeds

Up until this point, any action we take in Apex, whether invoked by Process Builder or Trigger, is running as whoever caused the action. And I don’t recommend granting this system permission to all users in your org. So we need a way to switch our context user to an administrator with this system permission. But how?

Set the Context User

Conveniently, when defining an Apex Email Service and its Email Service Addresses you can specify the context user that the code runs as. Perfect!

chatter-bot-post-message-email-service-context-user.png

Putting It All Together

Now we can use Process Builder to collect the information we need about the Chatter post, who to post as, where to post it, and what to post. Process Builder will invoke an Apex class that formats the data and sends it all nice and tidy to our Apex Email Service. At this point the context user will presto change-o from whoever launched the Process to our configured Administrator of the Email Service. We can now insert the FeedItem record specifying the CreatedById then use ConnectApi to update the post Body to include rich-text and @ mentions!

Flow Diagram

chatter-bot-feeds-flow-diagram.png

trailhead-astro-great-work-completed-challenge
Astro is pleased!

Next Steps: Chatter Notifications

As you begin to embrace more Chatter collaboration and automation, I encourage you to learn up and educate your users on how to manage their Chatter Email and Mobile Notifications. There’s a fine balance between being flooded with “noise” from groups or actions a user may not be interested in and not receiving enough of the important notifications like being @ mentioned.

Resources for Chatter Email Notifications:

Resources for Salesforce1 Mobile Notifications:

Once your users have Salesforce1 on their mobile device, teach them how to setup their Push Notification Settings so they can stay in-the-know while on-the-go!

salesforce1-push-notification-settings.png