I am new to Apex and I have the following code to call a function each time a new Chatter post is created:

public class MLPost {

    public static void MLPostCreate(FeedItem post) {

        for (FeedItem p : post){

I am getting this error and don't know what it means:

Compile Error: Loop must iterate over a collection type: SOBJECT:FeedItem

My trigger to call the above code is as follows:

trigger MLPostTrigger on FeedItem (before insert) {

    FeedItem posts = Trigger.new;


I get the following error when trying to compile the trigger code:

Error: Compile Error: Illegal assignment from LIST to SOBJECT:FeedItem at line 3 column 5

Any help would be greatly appreciated.

1 Answer 1


Update this particular line with this

public class MLPost {

    public static void MLPostCreate(List<FeedItem> posts) {

And update your trigger

trigger MLPostTrigger on FeedItem (before insert) {

  List<FeedItem> posts = Trigger.new;

  //  or you could also use 
  //  MLPost.MLPostCreate(trigger.new);

Explanation Actually trigger.new returns a list of SObjects, so in both the class and trigger code you should work with List. Hence, the error.

  • 3
    Using plural names for lists and sets helps a bit too i.e. I think its worth keeping the name "posts" in the method signature.
    – Keith C
    Commented Dec 19, 2014 at 20:05

You must log in to answer this question.

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