I am trying to get the topics of a FeedItem after it is created. TopicAssignment is the object that connects each FeedItem to its topics. I've created a trigger on FeedItem (after insert) that will run every time a new FeedItem is created. However, when querying TopicAssignment by EntityId in the trigger, nothing is returned. The query is shown here:

[SELECT TopicId, Topic.Name FROM TopicAssignment WHERE EntityId=:FeedItem.Id]

If I run the query in the Query Editor in the console, I get the TopicAssignment objects related to the topics. I suspect that TopicAssignment objects are not yet inserted into the database when the trigger is called. Is this the case, and is there a way I can work around this issue?

Any help is much appreciated!


This is one of those situations where a group of related objects are created together and there's no obvious way to run your code at the point where all of the related objects exist.

The FeedItem is created, then your trigger runs as the final part of that insert, then the TopicAssignments get created too late for your code to see them.

So, you'll need another approach, because the TopicAssignments won't exist in your trigger on FeedItem.

You haven't described your higher-level requirements but there are three potential routes that I can think of:

  • Trigger on TopicAssignment, and check that the EntityType is FeedItem
  • Pick up the topics using a scheduled process that runs regularly
  • Use a future method, initiated from FeedItem trigger. By the time that future method runs, the TopicAssignments will be there
  • I used the future method as you mentioned, and it worked. Thank you for your suggestion! – Thomas Huang Jul 11 '17 at 16:53

As per my understanding you want to make query on Topic Assignment object which is the child object of FeedItem object and you are writing trigger on insertion of the parent record (Feed Item) which does not have a child record (Topic Assignment) created yet so the result will be blank. Let me know if I have a clear understanding of the requirement

  • Yes, both you and Aidan's understanding is correct. Thank you for your answer! – Thomas Huang Jul 11 '17 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.