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
humble learnerhumble learner 

Trigger on Feed Item

Hi All,

 

I am trying to create a trigger on Feed Item to prevent unauthorized users to post. Unfortunately, I am getting following error on all Feed:

 

caused by: System.FinalException: SObject row does not allow errors

 My code is below:

public class ChatterPrevent{
    public static void CheckUserAccess(List<FeedItem> FeedPrevent){
        Set<id> licensetype = new Set<id>();
        Set<id> isfeedenabled = new set<id>();
        //Map<id,User> nocommentorpost = new Map<id,User>();
        Set<id> nocommentorpost = new Set<id>();
        Set<id> commentonly = new Set<id>();
        //Map<id,String> userstatforchatteranswer = new Map<id,String>();
        List<UserLicense> AllPartnerUsers = [SELECT Id,Name FROM UserLicense where name = 'Partner Community'];
        
        /*for(FeedItem fi: FeedPrevent){
        	isfeedenabled.add(fi.CreatedById);
        }*/
        
        for(UserLicense UL:AllPartnerUsers){
        	licensetype.add(UL.id);
        }
        List<User> unauthorizedusers = [SELECT Id,isActive, user.profile.userlicenseid,name,Community_Chatter_Permissions__c,Community_Chatter_Answers_permissions__c
                                        FROM User where user.profile.userlicenseid in: licensetype and isActive = true];
        for(User U:unauthorizedusers){
            if(U.Community_Chatter_Permissions__c == 'Cannot Post or Comment'){
        		//nocommentorpost.put(U.id,U);
                nocommentorpost.add(U.id);
            }
            else if(U.Community_Chatter_Permissions__c == 'Can Comment Only'){
            	commentonly.add(U.id);
            }
        }
        System.debug('id'+unauthorizedusers);
        
        
        List<FeedItem> getAllFeed = [select id,InsertedbyId from FeedItem where InsertedbyId in:nocommentorpost];
        //System.debug(getAllFeed);
        
        for(User fu:[select id from User where id in:nocommentorpost]){
            for(FeedItem fi:getAllFeed){
                if(fu.id==fi.InsertedById){
                    System.debug(fi.Id);
                	fi.adderror('You are not Authorized to Post or Comment');
                }
            }
        }
    }
    
    
}

 

I am new to Apex any direction will be greatly appreciated....

 

Thank you.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I'd expect you to need a before delete trigger on the FeedItem rather than the comment.

All Answers

bob_buzzardbob_buzzard

You can only add errors to the records that the trigger is processing - in this case you have queried back records from the database so you can't add an error to those.  

 

I suspect you should put the unauthorized users in a map keyed by id, then iterate the records from the trigger (I assume this is feedprevent?) and add an error to any where the insertedbyid has an entry in the map.

humble learnerhumble learner

Hi Bob,

 

Thanks for your reply. I have updated my trigger with your advice to the following. But This one does not fire at all. What am I not doing right here?? Yes FeedPrevent is a Trigger.new:

 

public class ChatterPrevent{
    public static void CheckUserAccess(List<FeedItem> FeedPrevent){
        Set<id> isfeedenabled = new set<id>();
        Map<id,User> nocommentorpost = new Map<id,User>();
        //Set<id> nocommentorpost = new Set<id>();
        Set<id> commentonly = new Set<id>();
        //Map<id,String> userstatforchatteranswer = new Map<id,String>();

        List<User> unauthorizedusers = [SELECT Id,isActive, profile.userlicenseid,name,Community_Chatter_Permissions__c,Community_Chatter_Answers_permissions__c,
                                        profile.userlicense.name FROM User where profile.userlicense.name = 'Partner Community' and isActive = true];
        System.debug('id'+unauthorizedusers);
        
        for(User U:unauthorizedusers){
            if(U.Community_Chatter_Permissions__c == 'Cannot Post or Comment'){
        		nocommentorpost.put(U.id,U);
                //nocommentorpost.add(U.id);
            }
            else if(U.Community_Chatter_Permissions__c == 'Can Comment Only'){
            	commentonly.add(U.id);
            }
        }
        System.debug('id'+nocommentorpost);
        
        //List<FeedItem> getAllFeed = [select id,InsertedbyId from FeedItem where InsertedbyId in:nocommentorpost];
        //System.debug(getAllFeed);
        
        for(FeedItem fp:feedprevent){
        	if(nocommentorpost.containskey(fp.insertedbyid)){ //does not fire at all
//if(fp.insertedbyid == communityusersid.get(fp.insertedbyid).id){ this gives error System.debug(fp.insertedbyid); system.debug(nocommentorpost.get(fp.insertedbyid).id); fp.adderror('You are not Authorized to Post or Comment'); } } } }

 

humble learnerhumble learner

Sorry Bob, My mistake I was not looping through the map. I am doing it in the following way and It's working now. If I want users to prevent delete a post, Does my trigger be on Feed Comment???

for(FeedItem fp:feedprevent){
            for(ID mapids:nocommentorpost.keySet()){
            	fp.adderror('You are not Authorized to Post or Comment');
            }
        }

 

bob_buzzardbob_buzzard

I'd expect you to need a before delete trigger on the FeedItem rather than the comment.

This was selected as the best answer
humble learnerhumble learner

Yes that's correct. I have done it follwoing way:

 

trigger ChatterPrevent on FeedItem (before insert,before update, before delete) {
    if(trigger.isinsert && trigger.isbefore){
		if(recursionPrevent.preventflag == false) {
   			//List <FeedItem> FeedPrevent = Trigger.new;
   			ChatterPrevent.CheckUserAccess(Trigger.new);
            //LukeClass.CheckUserAccess(Trigger.new);
    	}
    }
    else if(trigger.isdelete && trigger.isbefore){
    	ChatterPrevent.CheckUserAccess(Trigger.old);
    }
}

 

Thanks ever so much for your guidance...

humble learnerhumble learner

Hi Bob, 

 

Is my trigger not meant to prevent any comment on post as well since Feed Item is the parent of feed comment?? Or I need a separate trigger on feed comment for that??