+ Start a Discussion
KatherineCKatherineC 

FeedComment Test Class Missing FeedItemId

Hi All,
We created a trigger on FeedComment - a new record under ChatterPost will be created automatically when a comment contains keyword "legal". The test class has error that missing FeedItemId, we're not sure how to get it fix, please help.

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

@isTest
public class CommentKeywordlegalTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);
    }
}

TRIGGER:
Trigger CommentKeywordLegal on FeedComment (after insert) {
    List<FeedComment> FeedComments = new List<FeedComment>();
   for (FeedComment f : Trigger.new) {
           if (f.commentbody!=null &&   f.commentbody.contains('legal' ) ) {
               ChatterPost__c C = new ChatterPost__c();
               c.Description__c = f.commentbody;
                     
              insert C;
        }
    }
   }

Best Answer chosen by KatherineC
Shingo YamazakiShingo Yamazaki
Hi, KatherineC

I'm Shingo Yamazaki.

It's probably because you forgot to set "feedItemId" on FeedComment.

@isTest
public class CommentKeywordlegalTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        fc.FeedItemId = f.Id;   // please add this
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);
    }
}

Does it work?

All Answers

Shingo YamazakiShingo Yamazaki
Hi, KatherineC

I'm Shingo Yamazaki.

It's probably because you forgot to set "feedItemId" on FeedComment.

@isTest
public class CommentKeywordlegalTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        fc.FeedItemId = f.Id;   // please add this
        insert fc;
        Test.StopTest();
        System.assertEquals ('legal test', fc.commentbody);
    }
}

Does it work?
This was selected as the best answer
KatherineCKatherineC
Yes, it works!! Thanks so much Shingo, you helped me again. :) Have a nice day!