+ 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.