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 

Missing ParentId in 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.

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);
    }
}

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
Hargobind_SinghHargobind_Singh
Hi,

When creating a feed, you either need to connect it to an object, or a user. Updating parentID with userId should work, and the error of parentId not found should go away. Try this: 


@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        f.parentID = UserInfo.getUserId(); 
        insert f;
        Test.StopTest();
        System.assertEquals ('legal test', f.body);
    }
}




All Answers

Hargobind_SinghHargobind_Singh
Hi,

When creating a feed, you either need to connect it to an object, or a user. Updating parentID with userId should work, and the error of parentId not found should go away. Try this: 


@isTest
public class ChatterPostTest
{
    static testMethod void insertNewChatterPostTest()
    {
        Test.StartTest();
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        f.parentID = UserInfo.getUserId(); 
        insert f;
        Test.StopTest();
        System.assertEquals ('legal test', f.body);
    }
}




This was selected as the best answer
KatherineCKatherineC
Yes, it works!! Thank you so much, it's very helpful. Btw, in the trigger, can I put more than 1 key word? Or I have to create a trigger for each key word? I also notice that the trigger is case sensitive, if I post "Legal" in chatter, the trigger won't fire, has to be "legal". Is it normal?
Hargobind_SinghHargobind_Singh
Hi, 

You can make it case-insensitive by changing this line :

if (f.body!=null &&   f.body.toUpperCase().contains('LEGAL' ) ) {

And, if you want to add more words, you can do it in this trigger itself:

if (f.body!=null &&  (  f.body.toUpperCase().contains('LEGAL' )  || f.body.toUpperCase().contains('SECONDWORD' ) || f.body.toUpperCase().contains('THIRDWORD' )  )) {
KatherineCKatherineC
Wow, works great, thanks so much!!!