Business requires when a new account is created with account type is partner or vendor, a chatter post is added to the Account page. The chatter user must be a specific user and chatter post must mention the department director.
Analysis:
1. A trigger is added to Account object.
2. How to add the required chatter post using Apex code is a bit complicated.
There are two points:
2.1. How to add a specific user as the chatter post created by user through Apex Code?
2. 2. How to mention a user in chatter post through Apex Code
We have two ways to add a chatter post, using FeedItem object and using ConnectAPI name space classes.
With FeedItem object we can assigned CreatedById when the record is created. It meets the requirement item 2.1. But it cannot mention a user.
With ConnectAPI name space classes, it is the other way. It can mention a user but cannot assign createdbyId.
Solution:
1. Add a trigger on Account object, call the method in step 2.
PostFeedItem(accountId,mentionedUserId,'Please have a look? (--by autobot--)');
Please note '(--by autobot--)', this will be used to identify if the chatter post is created by apex code or real person input. This text will be used in the code below.
2. Create a method using ConnectAPI to generate chatter post.
This method has three parameters: parent record Id, mentioned User Id and the message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static void PostFeedItem(Id ParentId,Id mentionedUserId, string message) { ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput(); ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput(); ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput(); ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput(); messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>(); mentionSegmentInput.id = mentionedUserId; messageBodyInput.messageSegments.add(mentionSegmentInput); textSegmentInput.text = message; messageBodyInput.messageSegments.add(textSegmentInput); feedItemInput.body = messageBodyInput; feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem; feedItemInput.subjectId = ParentId; ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput); } |
3. Add a trigger on FeedItem, when a new FeedItem record is created, change the createdbyId.
In the trigger, it take text '(--by autobot--)' as identifier if createdbyid needs to be assigned to a specific user.
1 2 3 4 5 6 7 8 9 10 11 12 | trigger FeedItemTrigger on FeedItem (before insert) {
List<FeedItem> chatterPosts = new List<FeedItem>();
for(FeedItem chatterPost : trigger.new)
{
if (chatterPost.body.contains('(--by autobot--)'))
{
chatterPosts.add(chatterPost);
}
}
FeedItemTriggerHanlder(chatterPosts);
}
|
1 2 3 4 5 6 7 8 9 10 | public with sharing class FeedItemTriggerHanlder { public static void UpdateCreatedById(List<FeedItem> chatterPosts) { Id userId = [SELECT Id FROM User where isActive=true AND userName = 'xxxxx' LIMIT 1].Id; for (FeedItem chatterPost : chatterPosts) { chatterPost.createdById = userId; } } } |
How to use the code
You need to copy the code above in design section 2 and 3 and build you own Account trigger. As a reminder, it should be a AFTER trigger.