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
KatherineCKatherineC 

Chatter Post Trigger Test Class

Hi All,
We created a trigger for Chatter. When a post contains keyword ”legal', a new record under ChatterPost will be generated. We also created Test class but got error, please help, thank you so much.

Compile Error: Illegal assignment from Schema.SObjectField to Id at line 8 column 9

@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem post = new FeedItem();
        post.ParentId = ChatterPost__c.Id;
        post.Body = 'legal test';
        insert post;
        Test.StopTest();
        System.assertEquals ('legal test', post.body);
    }
}

TRIGGER:
Trigger ChatterKeywordLegal on FeedItem (after insert) {
    List<FeedItem> FeedItems = new List<FeedItem>();
   for (FeedItem f : Trigger.new) {
           if (f.body!=null &&   f.body.contains('legal' ) ) {
               ChatterPost__c C = new ChatterPost__c();
               c.Description__c = f.body;
                     
              insert C;
        }
    }
   }
Best Answer chosen by KatherineC
Pavan DavePavan Dave
Feeditem-> Parent ID:
This would be ID of the object type to which the FeedItem is related.
For example, set this field to a UserId to post to someone’s profile feed, or an AccountId to post to a specific account.

So before inserting feeditem, you need to mention create an object to which feeditem refers to and asssign it's id to feeditem->Parentid.

        Test.StartTest();
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        Any_Object__c obj = new Any_Object__c();
        insert obj;                                                 // before inserting there should be all required field should be assinged values.
        f.parentid = obj.id;                                    // here assign obj id as parent id for feed post.
        insert f;
        Test.StopTest();
        System.assertEquals ('legal test', f.body);

All Answers

Pavan DavePavan Dave
You missed creating Custom object record of type ChatterPost__c.

@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem post = new FeedItem();
        ChatterPost__c C = new ChatterPost__c();
        c.Description__c = 'Test body'; 
        post.ParentId = c.Id;
        post.Body = 'legal test';
        insert post;
        Test.StopTest();
        System.assertEquals ('legal test', post.body);
    }
}
KatherineCKatherineC
Thanks for the reply, it has error message that required field missing: ParentId

Btw, I think the chatterpost__c should be created after feeditem is inserted, should not be included in test class.
Pavan DavePavan Dave
I am not much aware of your business logic but this is what I can grab from first post. Try this for Parent id error.

@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem post = new FeedItem();
        ChatterPost__c C = new ChatterPost__c();
        Insert c;
        c.Description__c = 'Test body';
        post.ParentId = c.Id;
        post.Body = 'legal test';
        insert post;
        Test.StopTest();
        System.assertEquals ('legal test', post.body);
    }
}
KatherineCKatherineC
Thanks for the help, unfortunately it's not working. I think maybe need to create a user and this user created new feeditem with key word "legal", then trigger will fire to create new ChatterPost. Maybe I should use "f" after feeditem rather than post, it caused confusion. But again, I don't know how to fix it, below is the test class I did again with error message.

Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ParentId]: [ParentId]
Stack Trace Class.ChatterPostTest.insertNewChatterPostTest: line 9, column 1


@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        insert f;
        Test.StopTest();
        System.assertEquals ('legal test', f.body);
    }
}
Pavan DavePavan Dave
Feeditem-> Parent ID:
This would be ID of the object type to which the FeedItem is related.
For example, set this field to a UserId to post to someone’s profile feed, or an AccountId to post to a specific account.

So before inserting feeditem, you need to mention create an object to which feeditem refers to and asssign it's id to feeditem->Parentid.

        Test.StartTest();
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        Any_Object__c obj = new Any_Object__c();
        insert obj;                                                 // before inserting there should be all required field should be assinged values.
        f.parentid = obj.id;                                    // here assign obj id as parent id for feed post.
        insert f;
        Test.StopTest();
        System.assertEquals ('legal test', f.body);

This was selected as the best answer
KatherineCKatherineC
Yes, got it, appreciate your help.