0

The documentation seems to say that when you create a Task or Event related to a Case then it will automatically create a FeedItem

ActivityEvent—indirectly generated event when a user or the API adds a Task associated with a feed-enabled parent record (excluding email tasks on cases). Also occurs when a user or the API adds or updates a Task or Event associated with a case record (excluding email and call logging).

But when I create a Task like below, I do not get a FeedItem created for the case?

insert new Task(
    Subject = 'SMS Message', 
    Description = body,
    WhoId = whoId,
    WhatId = whatId, // set to the Case ID
    Priority = 'Normal',
    Status = 'Completed', 
    TaskSubtype = 'Email',
    ActivityDate = Date.today(),
    Twilio_SID__c = sid
);

If the above approach does not create a FeedItem, how can I do it directly using Apex so that a feed item appears in the timeline on the case layout?

2 Answers 2

1

The simplest way is to insert it like any other SObject (see below). Here's a link to the FeedItem object docs. It's worth mentioning that this method only works if you don't need to @mention a User/ Group.

insert new FeedItem(
  ParentId = thisCase.Id,
  Body = "The text to be displayed on the post"
);
2
  • Thanks Dan, how do I link the FeedItem to the Task so the user can click through and see the Task Description (in this example it is SMS content)?
    – Robs
    Commented Jun 8, 2020 at 14:48
  • I see two possible ways of doing this. You could: 1) Set the Feed Item's Body to the value of Task.Description. This would effectively duplicate the data, but saves the User having to navigate anywhere. It also saves you from having to go with one of the other options... 2) Use the Connect API Helper to @mention the Task directly in the Feed Item (github.com/forcedotcom/ConnectApiHelper). The Helper is a great tool, but has strict limits. 3) Try and hack a URL link into the FeedItem body using the Task Id (not sure this is possible, let alone advisable) Commented Jun 8, 2020 at 15:29
3

Based on my testing, the standard functionality of auto creating FeedItem on insert of Task; only works when TaskSubtype is set to Task or Call.

I inserted 4 tasks based on the TaskSubtype values, only 2 appeared on feed on Case:

List<Task> tasks = new List<Task>();
for(String taskSubtype: new List<String> {'Task', 'Email', 'ListEmail', 'Call'}) {
    Task t = new Task(
        Subject = TaskSubtype, 
        Description = 'body',
        WhoId = whoId,
        WhatId = whatId, // set to the Case ID
        Priority = 'Normal',
        Status = 'Completed', 
        ActivityDate = Date.today(),
        TaskSubtype = taskSubtype
    );
    tasks.add(t);
}
insert tasks;

If desired, you need to add a condition in your code to insert a FeedItem explicitly when the TaskSubtype is Email as suggested by Dan.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .