Home SalesforceApex Posting Rich Text Chatter using Apex

Posting Rich Text Chatter using Apex

by Dhanik Lal Sahni

Salesforce Chatter is a collaboration tool that let users discuss and share information and files. We can post chatter information using apex, flow, process builder, or using API. This post will help in posting rich text chatter using Apex.

Use Case

User need to share cases with multiple users from case list views. We should be able to share some information along with sharing records mentioning all selected users.

Solution:

We can use chatter post to share infomation. We can use Post to Chatter flow action and FeedItem object to create a chatter post in flow. Both options have limitations while sending chatter posts.

Post to Chatter action can not send rich formatted information and FeedItem object can not mention the user in the post. If we want to share the formatted post and mention users as well then we have to use a custom solution.

Let us create a custom apex code that will share rich text content in chatter posts.

Steps for a custom solution

  1. Create an Apex class to share the formatted post
  2. Create an Apex class to Annotate for flow
  3. Create a flow to share posts using the apex class
  4. Create List Button and Add to List Button Layout
  5. Test Functionality

1. Create an Apex class to share the formatted post

We can create an apex class that will post formatted chatter information. We can use global pattern matching to find HTML tags within post text. Using different segment ConnectApi classes we can format post text.

We can use the apex class which is available at ConnectApiHelper. Create this class based on your organization’s naming guidelines.

2. Create an Apex class to Annotate for flow

Create another class that will invoke the above-created ConnectApiHelper. This class will be created as a flow action class. Create Invocable variables and get a list of input parameters from the flow.

@InvocableMethod annotation allows the Flow to call the method. We can use Label and Description attributes to put metadata of the class for flow. As we can use only 1 input variable in method so we will create a custom type FlowInput.

@InvocableVariable annotation allows the Flow to set the variables. We will create 3 variables communityId, subjectIds and postBody.

  • subjectIds – List of cases where the post will be posted.
  • postBody – Post content
  • communityId– Community Id where this will be posted. It is only required when we post on the community site.

3. Create a flow to share posts using the apex class

Now let us create a flow that will prepare post messages by mentioning users. We can use LWC or apex also to prepare messages but for better maintenance, use Flow. We can change the content at runtime on production but LWC and Apex have to go with the proper development cycle.

As we have to share cases with the multiple user in chatter, we will share information with each record’s chatter. Each record will be preferred over the user’s chatter post, as all activity will be recorded in the record’s chatter activity. After user selection, a chatter post sends to each record mentioning the selected user.

We have to do the below steps in screen Flow

  • Get Selected Record Ids
  • Use the Lookup component for User selection
  • Create a Chatter Post Message
  • Call Apex Class to send a formatted message

a. Get Selected Record Ids

Get selected records id from list view using standard ids variable in flow. Check out the post Post Chatter Feed Using FeedItem in Flow to get more detail on getting selected ids in flow.

b. Use the Lookup component for User selection

We can use the Lookup component to show user selection. We can enable multiple user selections using this component. Selected user ids are used in mentioning in the message body.

Check out our other post Multi Select Lookup in Screen Flow to see how we can use lookup in screen flow.

For this post, I have stored selected record ids in {!userIds} variable. It can store multiple recordIds.

c. Create a Chatter Post Message

create a chatter post message to send in the record post. We will use a Text Template to create a message body.

Create Mention Users- We have to prepare all mentioned users in the format of {userid} so create an assignment variable like the below image. This assignment can be done in the loop for all selected users.

Mention User in Flow for Posting Rich Text Chatter using Apex

Case List Preparation: We need to add all selected cases in the list so prepare the body in the format of {record:recordid} and put that in order or unorder list like

  • {record:3012v0000016qdbAAA}
  • . Prepare a list like the below image.

    Case list for Posting Rich Text Chatter using Apex

    Merge post text: Merge all information in text template type variable in rich text format.

    Create Text Template for Posting Formatted HTML Chatter using Apex
    Posting Rich Text Chatter using Apex

    d. Call Apex Class to send a formatted message

    Call action Share Formatted Chatter Post to send formatted message to each selected case record. We will use the above-created post message postBody.

    Custom Apex Action in Flow

    Complete Flow Screenshot

    Screen Flow

    4. Create List Button and Add to List Button Layout

    Create a button to call the above-created screen flow and add that button to the list view button layout. Refer Post Chatter Feed Using FeedItem in Flow to see about this step.

    5. Test Functionality

    Now test functionality by selecting case records in the case list view. This will post a message to reach a selected record. It will also send notifications using email to all mentioned users.

    References:

    Connect API Helper

    Post Chatter Feed Using FeedItem in Flow

     Multi Select Lookup in Screen Flow

    Related Posts

    Top 10 Best Practice for Lightning Flow

    You may also like

    Leave a Comment