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
Phu Dang 1Phu Dang 1 

I want to prevent customer from posting comment on the chat feeder.

I want to prevent customer from posting comment on the chat feeder in Communities.

I got the trigger but it seem not right.  What is wrong here?

 trigger InternalChatterCommentTrigger on FeedComment (before insert) {
list<user> ExternalUser = [select id from user where ProfileId in('00e50000001FdBYAA0')];
FeedComment
     for (FeedComment fc : trigger.new){
             for(user u : ExternalUser){
                {
                 if (fc.CreatedById == u.id)
                 {
                   fc.adderror('You do not have permission to comment on Chatter. Please respond on this case using Comment related list');
    
        }
    }
}
}



Best Answer chosen by Phu Dang 1
Ankit AroraAnkit Arora
You should replace the fc.CreatedById by UserInfo.getUserId() as record is not yet inserted and will never enter in condition. Try with this :
trigger InternalChatterCommentTrigger on FeedComment (before insert) {
list<user> ExternalUser = [select id from user where ProfileId in('00e50000001FdBYAA0')];
     for (FeedComment fc : trigger.new){
             for(user u : ExternalUser){
                {
                 if (UserInfo.getUserId() == u.id)
                 {
                   fc.adderror('You do not have permission to comment on Chatter. Please respond on this case using Comment related list');
    
        }
    }
}
}
}

All Answers

Ankit AroraAnkit Arora
You should replace the fc.CreatedById by UserInfo.getUserId() as record is not yet inserted and will never enter in condition. Try with this :
trigger InternalChatterCommentTrigger on FeedComment (before insert) {
list<user> ExternalUser = [select id from user where ProfileId in('00e50000001FdBYAA0')];
     for (FeedComment fc : trigger.new){
             for(user u : ExternalUser){
                {
                 if (UserInfo.getUserId() == u.id)
                 {
                   fc.adderror('You do not have permission to comment on Chatter. Please respond on this case using Comment related list');
    
        }
    }
}
}
}

This was selected as the best answer
Phu Dang 1Phu Dang 1
Thank you Ankit.