You need to sign in to do that
Don't have an account?
Jakob 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:
And here's the test:
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!
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!
All Answers