salesforceapex-codesalesforce-chatter

How to Create a read-only group in SalesForce


I want to know 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 posting in this group?.

i tried but it not work:

trigger N_GroupReadOnly on FeedItem (before insert) {

ID groupId = [Select Id from CollaborationGroup where Name = 'Group_ReadOnly'].Id;
CollaborationGroup ownerId = [Select OwnerId From CollaborationGroup Where Name = 'Group_ReadOnly'];
for(FeedItem item : trigger.new){
    if((item.ParentId == groupId) && (item.InsertedById != ownerId.OwnerId)){
        system.debug('you can not add post in this group');
        alert("you can not add post in this group");
        delete item ;
        return;
    }
    else{
        insert item;
    }
}

}

Thank you.


Solution

  • Solution:

    as per this developerforce.com forum entry:

    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');                    
                    }        
                }
            }
    
        }
    }