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
KhaledZeroKhaledZero 

read only Group using trigger

Hi,

 

how to create a read-only group + like + comment and not being able to post for members except admin and owner
* how to use triggers on the FeedItem in this group?.

 

 

i tried whith this code but it not work:

 

trigger:_______________________________________

 

 

trigger GroupReadOnly on FeedItem (after insert) {    
    
    CollaborationGroup gp = [Select OwnerId, Id From CollaborationGroup Where Name = 'Group_ReadOnly'];
    
    List<FeedItem> feedItems = new List<FeedItem>();
    
    for(FeedItem item : trigger.new){
        if(item.ParentId == gp.Id)
        {
            feedItems.add(item);
        }      
    }
   if(feedItems.size() >0) GroupReadOnlyClass.FilterFeedItems(feedItems);
}

 

class:_______________________________________

 

public class GroupReadOnlyClass{    
    public static void FilterFeedItems(List<FeedItem> feedItems){
        
        Id[] listFeedItemToDelete = new Id[]{};
        CollaborationGroup gp = [Select OwnerId, Id From CollaborationGroup Where Name = 'Group_ReadOnly'];
        
        for(FeedItem item :feedItems){
            if(item.ParentId == gp.Id)
             {
                if(item.InsertedById != gp.OwnerId){
                    listFeedItemToDelete.add(item.Id);
                }        
            }
        }
        if (listFeedItemToDelete.size() > 0 )
        {
           DeleteFeedItems.DeleteFeedItems(listFeedItemToDelete);
        }
    }
}

 

 

class delete:________________________

 

 

public class DeleteFeedItems
{
 @future
 public static void DeleteFeedItems(Id[] listFeedItemToDelete)
 {
   if (listFeedItemToDelete.size() > 0 )
   {
     List<FeedItem > feedItems= [Select Id FROM FeedItem where Id=: listFeedItemToDelete];
     if ( feedItems != null)
     {
       delete feedItems;
       System.Debug(feedItems.size() + ' Feeds deleted !');
     }
   }
 }
}

 

 

thx.

 

Best Answer chosen by KhaledZero
Bryan WiseBryan Wise
I went to use this code the other day and then stumbled upon the fact that this is now just a feature of chatter: https://help.salesforce.com/apex/HTViewHelpDoc?id=collab_groups_broadcast_about.htm&language=en_US Broadcast Chatter Groups

All Answers

KhaledZeroKhaledZero

solution:

 

Trigger: _______________________________________________

 


trigger GroupReadOnly on FeedItem (before insert) {    
    
    CollaborationGroup gp = [Select OwnerId, Id From CollaborationGroup Where Name = 'Group_ReadOnly'];
    
    List<FeedItem> feedItems = new List<FeedItem>();
    
    for(FeedItem item : trigger.new){
        if(item.ParentId == gp.Id)
        {
            feedItems.add(item);
        }      
    }
   if(feedItems.size() >0) GroupReadOnlyClass.FilterFeedItems(feedItems);

}

 

 

 

Class:______________________________________________________________

 


public class GroupReadOnlyClass{    
    public static void FilterFeedItems(List<FeedItem> feedItems){
        
        CollaborationGroup gp = [Select OwnerId, Id From CollaborationGroup Where Name = 'Group_ReadOnly'];
        
        for(FeedItem item :feedItems){
            if(item.ParentId == gp.Id)
             {
                if(UserInfo.getUserId()!= gp.OwnerId){
                    item.addError('You cannot post! Just Owner can post in this group');                    
                }        
            }
        }
        
    }
}

 

 

Bryan WiseBryan Wise
I went to use this code the other day and then stumbled upon the fact that this is now just a feature of chatter: https://help.salesforce.com/apex/HTViewHelpDoc?id=collab_groups_broadcast_about.htm&language=en_US Broadcast Chatter Groups
This was selected as the best answer