You need to sign in to do that
Don't have an account?
humble 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.
I'd expect you to need a before delete trigger on the FeedItem rather than the comment.
All Answers
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.
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:
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???
I'd expect you to need a before delete trigger on the FeedItem rather than the comment.
Yes that's correct. I have done it follwoing way:
Thanks ever so much for your guidance...
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??