+ Start a Discussion
IndiaSforceIndiaSforce 

Trigger to Block user to post Commnet on Chatter

As Admin can not prevent directly some profile based user to add comment. I have develope small trigger which can help you for this validation.

 

trigger RistrictUser on FeedComment (Before Insert) 
/*This trigger will restirct user to add comment on chatter*/
{
for(FeedComment FC: trigger.new)
    {
    /*Following Code will get Current User information*/
    User u = [SELECT id,ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];

    /*We are going to restrict user from 'Non-Chatter Users' profile */
        if(u.Profile.Name == 'Non-Chatter Users')
        {
            FC.adderror('You can not post comment'); //You can Customize the error message here 
        }

    }
}/*EOF*/

 

 

Let me know your feedback

cloudcodercloudcoder

Nice idea although your code needs some work to avoid governor limits etc. For example you should never call a SOQL query within a for loop.