Post to Chatter from Process Builder with hyperlinks to records

In Summer ‘18 Release Salesforce provided a way to “link to your most relevant records from a Post or Comment” in Chatter. This feature can be used directly while typing in Chatter, or from Apex code, however, they haven’t provided an out-of-the-box way to do this from declarative tools such as Process Builder or Flow. In this post I’ll provide a way how you can use these tools to easily post something similar to what I did in the picture below:

chatter_post

Prerequisite

As I said at the beginning of this post, there is no way to do this out-of-the-box just yet. Because of that, we have to write some custom code to get around this limitation, which, luckily, I already did. You can use the below Deploy to Salesforce button to deploy a couple of Apex classes to your sandbox from my GitHub repository. One class, ConnectApiHelper, is used to easily Post to Chatter from Apex code, and the second class, PostToChatterAction, contains an InvocableMethod that can be executed from Process Builder or Flow.

Deploy to Salesforce

Disclaimer: ConnectApiHelper class was not built by me. I have forked the official Salesforce repository and added the additional code to support adding links to records (previously it only supported mentiones of Users and Groups). I have also added the ability to bulk post up to 500 Chatter elements using 1 DML statement. Everything else was written by Salesforce.

Post to Chatter from Process builder

Hopefully, you have successfully deployed the Apex classes to your Sandbox so that you can use them from Process Builder or Flow. To test out this feature create a Process Builder on Opportunity object and make it run every time a record is created or edited. Next, add a condition without any criteria, so that it is executed every time a Process Builder is triggered (it should go without saying that we are doing this just for demo purposes, I don’t recommend using no criteria in Production). Finally, add an Apex immediate action and name it “Post to Chatter”. Your Process Builder should look similar to this:

process_builder

The Apex action takes in 2 required inputs and one optional:

  • Message - this is a message for your Chatter post.
  • Subject Id - the parent record of the post. This can be a user ID, a group ID, or a record ID.
  • Community Id - the optional ID of a community to which to post the message. This can either be an ID of a community, “internal”, or null (blank).

The Message parameter should be of type Formula so that we can concatenate field values from the related records. In order to post the same message as displayed at the beginning of this post, you can use the formula below:

 '<p>Hi {' & [Opportunity].OwnerId & '},</p><p>your <b>Opportunity</b> has been <i>updated</i>. This is the parent <u>Account</u> {'
 & [Opportunity].AccountId & '} and this is the <s>Opportunity</s> {' & [Opportunity].Id & '}.</p>'
 &
 '<p>&nbsp;</p><p>This is a new paragraph with a new line above it.</p>'
 &
 'And this is a list: <ol> <li>Item #1</li> <li>Item #2</li> <li>Etc..</li> </ol>'
 &
 '<p>&nbsp;</p>And one more list: <ul> <li>Item #1</li> <li>Item #2</li> <li>Etc..</li> </ul>'

It looks a bit messy on a first look, but keep in mind that I’ve added all the formatting tags I could in order to demonstrate all the different formatting capabilities when posting to Chatter. Your use case will probably result in a much simpler formula - probably something more similar to the one below:

'Hi {' & [Opportunity].OwnerId & '}, your opportunity {' & [Opportunity].Id & '} from account {' & [Opportunity].AccountId &
'} has been updated.'

Below you can find a cheat sheet of different formatting options and codes to tailor the Chatter Post to your needs:

  • <p>Some Text</p> - this will place “Some Text” in a paragraph
  • <b>Some Text</b> - this will make Some Text bold
  • <i>Some Text</i> - this will make Some Text italic
  • <u>Some Text</u> - this will underscore Some Text
  • <s>Some Text</s> - this will strikethrough Some Text
  • <ol> <li>Some Text</li> </ol> - this will place “Some Text” in an ordered list
  • <ul> <li>Some Text</li> </ul> - this will place “Some Text” in an un-ordered list
  • <p>&nbsp;</p> - this will result in a new line
  • {SOME_RECORD_ID} - placing a record ID between the curly brackets will make a hyperlink or @-mention to that record

That’s all the formatting options you have, but keep in mind that you can mix and match them as well, making a bold italic text for example.

Post to Chatter from Flow

The above was a quick demonstration on how to Post to Chatter from Process Builder with @-mentiones or with hyperlinks to existing records. You can use a similar approach from Flow by using the PostToChatterAction flow element. I won’t be demonstrating this approach in this blog post, as I believe that the information above should be sufficient. However, if you have any questions let me know below and I’ll try to answer them as quickly as possible, or even write an additional blog post to cover Flow.

Post to Chatter from Apex

If you are a Salesforce Administrator that doesn’t use code you can probably stop reading at this point. The rest of the post is about how you can use the ConnectApiHelper class to easily (and more naturally) post to Chatter from Apex code without juggling all those different ConnectApi classes.

As said in a disclaimer above the ConnectApiHelper class was not built by me, I just added a few more lines of code and methods to support bulk posting and adding hyperlinks to records. You can see all the code and some quick instructions in my GitHub repo. The quick gist of it is that if you deploy the ConnectApiHelper class to your sandbox you will be able to post to Chatter from Apex in the following manner:

Opportunity opp = [SELECT Id, AccountId, OwnerId FROM Opportunity LIMIT 1];

ConnectApiHelper.postFeedItemWithMentions(
  Network.getNetworkId(), //community Id where to post, or "internal", or null
  opp.AccountId, //this is the record where the post will appear
  'Hi {' + opp.OwnerId + '}, your Opportunity {' + opp.Id + '} has been <i>updated</i>.'));

Note that you can use all the formatting tags already described previously in this post.

Finally, in order to perform a bulk posting of multiple Chatter Posts at once by using only 1 DML statement, you can use the ConnectApiHelper.postFeedItemBatch method:

List<ConnectApi.FeedItemInput> chatterPosts = new List<ConnectApi.FeedItemInput>();

//add up to 500 Chatter Posts to a list
chatterPosts.add(
  (ConnectApi.FeedItemInput)
  ConnectApiHelper.createFeedItemWithMentions(
    'me',
    'Hi {' + opp.OwnerId + '}, your Opportunity {' + opp.Id + '} has been <i>updated</i>.'));

//finally post them all in a batch
ConnectApiHelper.postFeedItemBatch(
  Network.getNetworkId(),
  chatterPosts);

That’s the gist of its use in Apex. For more details visit the GitHub repository and explore the code and documentation.

Conclusion

We covered how to Post to Chatter using Process Builder and Flow using the code that can be manually obtained here, or quickly deployed using the Deploy to Salesforce button below. We also quickly covered how to use the provided helper classes to more easily Post to Chatter from Apex. I sincerely hope that this will help some people out there. If you have any questions or suggestion feel free to comment below or log an issue at GitHub.

Deploy to Salesforce

Written on August 11, 2018