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.
“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?
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.
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)
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.
With either message option you choose, note that Chatter posts only support a subset of HTML as shown below:
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!
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:
- Must be easy to use and launchable by Process Builder or Flow (declarative)
- Must support setting the author to someone other than the running user
- Must support rich-text
- 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.
- 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
- 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.
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”.
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!
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

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:
- Chatter 101 for the Salesforce Admin by Becky Webster
- Navigate to Chatter Email Notifications
- Manage Your Personal Chatter Email Settings
- Chatter Email Digests
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!
Very impressive and informative post Doug! I learned a lot from it. Thanks!!!!
LikeLiked by 1 person
Thanks Sara! This solution took me on a wild ride. I learned a lot too!
LikeLike
The way you have pieced together many of these declarative features that truly don’t want to work together in a way that admins can leverage without coding is (to echo Sara’s comments) truly impressive.
Well done!
LikeLiked by 1 person
Thanks Joseph!
LikeLike
Hi Doug, this is extremely creative and has been hugely helpful for a project of mine, thank you! One issue I just noticed, though, is that all the Chatter posts now say “edited by [current user]” because of the updateComment(). For my purposes this is distracting and undesired. Can you think of any way around this?
LikeLiked by 1 person
I’m glad you like the solution! Unfortunately, due to the unique nature of this creative approach, the Chatter post is updated and I’m not sure how to get around it saying “edited by” because technically it was edited in the code.
LikeLike
Hi Doug, thanks so much for this amazing piece of functionality. Would you mind if I present this solution at our Salesforce Team day (with credit to you of course!)
LikeLiked by 1 person
Hi Michelle, I’m glad you like it! I’d be honored for you to share this with your team. Thanks
LikeLike
Hello, love the tool however when installing chatter bot Feeds I face an error with deploying to PROD environment. After analyzing, the ChatterBotGroupMemberTriggerHandlerTest fails because it references the Custom Setting with the Email Service User Id. Based On the instructions the email service is set after Deploying the Feeds but not possible.
Has anyone else faced this issue? If so, how did you proceed?
LikeLiked by 1 person
Hi Kenton,
Hrmm… try deleting the FlowDefinition files from the “src” folder, as well as remove the reference to it from package.xml.
I believe the issue is that the process builders are firing but since you’re in the middle of deploying the metadata, you haven’t had a chance to configure the custom setting records as you noted. This is probably poor design of the code and intermixing the two projects on my side, sorry.
LikeLike
can we use feeditem body as merge field in the email template rather than static text that goes out to all users? i need to repost a feeditem with same body (cloning ) but just as a different user other than the context user. its strange but i cant find body field as merge field in the template. any other alternative to achieve this? thanks for taking your time !
LikeLike
Hi Doug,
First, thanks for the awesome tool! Does this support changing the ‘To’ or audience? I have a use case where the message may be going to an internal user or a community user based on who’s assigned the record. In our environment we usually control this with the ‘All with access’ setting when creating the chatter post vs Internal Only.
Thanks!
LikeLike