+ Start a Discussion
dlCamelotdlCamelot 

Trigger that limits chatter group posting to announcements only

Trying to build a trigger that will limit posts on a few Chatter Groups.  If anyone posts something that is NOT an announcement in these groups (indicated by 'Announcements' in the Group description), then they should receive an error.

The below code works if there's 1 group, but I need this to work for more than one group.
trigger announcementsOnly on FeedItem (after insert, after update) {

CollaborationGroup group = [SELECT OwnerId, Id FROM CollaborationGroup WHERE Description LIKE '%Announcements%'];
    
 List <FeedItem> fList = new List <FeedItem>();
    
    for(FeedItem items : trigger.new)
        
    {
        
       if(items.ParentId == group.Id && items.Type != 'AdvancedTextPost')

       {
      	 	items.addError('Please post an announcement post, not a text post.);
      	}
    }
    
}
Best answer will be voted on!
 
Best Answer chosen by dlCamelot
CARLOS ARTURO PINTOR SALOMÓN 3CARLOS ARTURO PINTOR SALOMÓN 3
Hey there! Check out and run the following trigger!
trigger announcementsOnly on FeedItem (after insert, after update) {
    
    List<CollaborationGroup> groupList = [SELECT OwnerId, Id FROM CollaborationGroup WHERE Description LIKE '%Announcements%'];
    
    List <FeedItem> fList = new List <FeedItem>();
    
    for(FeedItem items : trigger.new)
        
    {
        for(CollaborationGroup gr : groupList){
            
            if(items.ParentId == gr.Id && items.Type != 'AdvancedTextPost')
                
            {
                items.addError('Please post an announcement post, not a text post.');
            }
        }
    }    
}
Let me know if it works!