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
Rashed Yaqubi 10Rashed Yaqubi 10 

Trigger on Chatter group to disable commenting from members based on profile ID

Hi there,

I am trying to disable the ability to comment in chatter groups based on profile ID.  I have 3 groups: one public and two private.  I want to disable all users with an "agent" profile from being able to comment in those specific groups. I am assuming this is a simple piece of code, but I am not able to create it on my own.  

I have tried the following trigger, but it doesn't seem to be working.  When i try to log in as an agent and do a post to a group the agent is a memeber of, it goes through.  I also noticed this code does not include the name of the group that the user cannot post in. Anyone have any ideas? Thanks! 

trigger ChatterCommentsDisabled on FeedComment (before insert) {
Id profileUser = UserInfo.getProfileId();

profile AnnuityProfile = [select id from profile where name ='D2D Agent User - Atlantic' or name ='D2D Agent User - Central'];
    for(FeedComment fc : trigger.new)
        if(fc.CommentType== 'ContentComment' && ProfileUser == AnnuityProfile.Id)
            fc.addError('you dont have permissions to Comment');
}
PawanKumarPawanKumar
Please try below.

trigger ChatterCommentsDisabled on FeedComment (before insert) {

Id profileId=userinfo.getProfileId();
String annuityProfileName =[Select Id,Name from Profile where Id=:profileId].Name;

Boolean isRestrictedProfile = false;
if(String.isNotEmpty(annuityProfileName)){
    if(annuityProfileName.equalsIgnoreCase('D2D Agent User - Atlantic')){
        isRestrictedProfile = true;
    }
    
    if(annuityProfileName.equalsIgnoreCase('D2D Agent User - Central')){
        isRestrictedProfile = true;
    }
}

if(isRestrictedProfile){
    for(FeedComment fc : trigger.new){
        fc.addError('you dont have permissions to Comment');
    }
}

}

Please mark it best if it helps you. Thanks.
PawanKumarPawanKumar
Please let me know if it's working for you. Thanks.
Rashed Yaqubi 10Rashed Yaqubi 10
Thank you for the response Pawan, however the code did not work.  No errors when I save the trigger, but users are still able to post.  I am working in sandbox and the reps have platform licenses.  Do I need to "deploy" or "run" the trigger in any way? - I assume not given that it is a trigger.  Any other ideas?