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
Jakob BerntssonJakob Berntsson 

Inserting FeedComment in test class

Hi! I'm trying to make a trigger that sets a Case's status to "Open" when a FeedComment is inserted or updated. While the trigger works fine when I've tested it in real-world cases in my sandbox, I have not been able to get any test case working.

Here's the trigger:
trigger UpdateStatus on FeedComment (after insert, after update) {
    
    List<Case> caselist = new List<Case>();
    List<Case> casestoupd = new List<Case>();
    List<Id> caseids = new List<Id>();
    
    for(FeedComment f:Trigger.New) {
        if (f.ParentId.getSObjectType() == Case.SObjectType) {
            caseids.add(f.ParentId);
        }
    }
    
    try {
        caselist = [Select Id, Status from Case where Id IN :caseids];
    }
    catch(Exception e) {
        caselist = null;
    }
    
    for(Case c:caselist) {
        c.Status = 'Open';
        casestoupd.add(c);
    }
    
    List<Database.SaveResult> updateResults = Database.update(casestoupd,False);
}

And here's the test:
@isTest
private class UpdateStatus_UnitTest {
    
    static testMethod void testTrigger() {
        
        Case c = new Case(Subject='Test Case', Status = 'New');
        insert c;
        
        test.startTest();
        
        FeedItem f = new FeedItem(ParentId=c.Id, Body='Test Post');
        insert f;
        
        FeedComment fc = new FeedComment(CommentBody='Test Comment',FeedItemId = f.Id);
        insert fc;
        
        System.assertEquals('Closed', c.Status);
        
        test.stopTest();
        
    }
}

The problem seems to be that FeedComment's ParentId field isn't writable. When I try to print out fc.ParentId it just returns null, and because of this the System.assertEquals test fails.

Really appreciate any help I can get on this. Thanks in advance!
Best Answer chosen by Jakob Berntsson
Raj VakatiRaj Vakati
Try like this .. query and compare
 
@isTest
private class UpdateStatus_UnitTest {
    
    static testMethod void testTrigger() {
        
        Test.StartTest();
        Case c = new Case(Subject='Test Case', Status = 'New');
        insert c;
        
        
        
        FeedItem f = new FeedItem();
        f.ParentId = c.id;
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        // fc.RelatedRecordId =c.Id;
        fc.FeedItemId = f.Id;   // please add this
        //  fc.ParentId =c.id;
        insert fc;
       FeedComment fcQ = [Select Id ,CommentBody , FeedItemId ,ParentId from FeedComment ];
        
        System.assertEquals(fcQ.ParentId ,c.Id);
        Test.StopTest();
        
        
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try like this .. query and compare
 
@isTest
private class UpdateStatus_UnitTest {
    
    static testMethod void testTrigger() {
        
        Test.StartTest();
        Case c = new Case(Subject='Test Case', Status = 'New');
        insert c;
        
        
        
        FeedItem f = new FeedItem();
        f.ParentId = c.id;
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
        // fc.RelatedRecordId =c.Id;
        fc.FeedItemId = f.Id;   // please add this
        //  fc.ParentId =c.id;
        insert fc;
       FeedComment fcQ = [Select Id ,CommentBody , FeedItemId ,ParentId from FeedComment ];
        
        System.assertEquals(fcQ.ParentId ,c.Id);
        Test.StopTest();
        
        
        
    }
}

 
This was selected as the best answer
Jakob BerntssonJakob Berntsson
Awesome, thanks Raj! That works splendid, and with a similar query for the case I also managed to get my initial System.assertEquals for the case status working:
Case cQ = [Select Id, Subject, Status from Case];

System.assertEquals('Open', cQ.Status);