function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

Can I set it to automatically create a chatterpost after I insert a new task via Apex?

Hello, we have some simple apex code that creates a task record, and the whoid of the task is a Lead. 

My question is: Just as when a regular activity is logged, it automatically posts to chatter with a summary of the activity, I need to do the same here. Specifically, I need it to post to the chatter on the Lead record page. Can this be done?
Best Answer chosen by Michael M
sachinarorasfsachinarorasf
Hi Michael,

You can insert chatter from Apex its Api name FeedItem, You want to post chatter on which object record pass that objects record Id in ParentId.

FeedItem post = new FeedItem();
post.ParentId = leadId;
post.Body = 'Task is created';
insert post;

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

sachinarorasfsachinarorasf
Hi Michael,

You can insert chatter from Apex its Api name FeedItem, You want to post chatter on which object record pass that objects record Id in ParentId.

FeedItem post = new FeedItem();
post.ParentId = leadId;
post.Body = 'Task is created';
insert post;

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
Michael MMichael M
Hi Sachin, thank you for that information. I want my posts to look like this in chatter, namely where there's a hyperlink to the task and it mentions the user's name, and it shows it with a green icon. Is that possible via apex?
User-added image