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
umakanth mettuumakanth mettu 

chatter trigger Help on Test class

Hi All,
we want to activate the chatter for email to case in lightning but we do not want any person to post or comment using chatter.
I have written two triggers for not posting anything and also commenting via case feed both are working fine but failed to write a test classes for both.
trigger ChatterCommentItem on FeedItem (before insert) {
for(FeedItem fi :
        if(fi.type == 'ContentPost' || fi.type == 'TextPost')
            fi.addError('you do not have permissions to post any comment or File.Please contact your system administrator.');
trigger ChatterComment on FeedComment (Before Insert) { 
    for(FeedComment fc :
if(fc.CommentType == 'ContentComment' || fc.CommentType == 'TextComment' )
            fc.addError('you do not have permissions to post any comment or File.Please contact your system administrator.');

I have written feed comment trigger for case object when the case status or any changes happens case feed is generated so no one should comment on it any help on the test classes for both triggers would be appriciated thank you.
Raj VakatiRaj Vakati
try this code
private class FeedItems_Test {
    static testMethod void linkChatterFiles_Oppty() {
        FeedItem f = new FeedItem();
        f.ParentId = UserInfo.getUserId();
f.type = 'ContentPost';
        f.body = 'test';
        insert f;
        FeedComment fc = new FeedComment();
        fc.CommentBody = 'legal test';
fc.CommentType = 'ContentComment' ;
        fc.FeedItemId = f.Id;   // please add this
        insert fc;

Hi umakanth,

Please check test classes;-

For FeedItem:-

private class ChatterCommentItemtest {

    static void testBeforeInsert() {
        FeedItem fi = new FeedItem(); = 'fi';
        fi.ParentId = UserInfo.getUserId();
        fi.type =  'ContentPost';
            Database.SaveResult result = Database.insert(fi, false);
        System.assertNotEquals(null, result.getErrors()[0].getMessage(), 'error message');

For FeedComment;-​

private class ChatterCommentFeedCommenttest {

    static void testBeforeInsert() {
        FeedComment fc = new FeedComment(); = 'fc';
        fc.CommentType =  'ContentComment';
            Database.SaveResult result = Database.insert(fc, false);
        System.assertNotEquals(null, result.getErrors()[0].getMessage(), 'error message');