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
Venkat R. BadeVenkat R. Bade 

Delete/Prevent Chatter Feed Trigger on FeedItem

The initial idea was to block all chatter feed from external batch jobs which run nightly. We had a trigger do it.
trigger PreventPost on FeedItem (before insert) {
    if(UserInfo.getProfileId() == '00eo0000000U2if') //User Account Profile ID used for running batch jobs
    {       
        for(feedItem tmp : trigger.new){           
                tmp.addError('API Unable to Post');
            }
        }
    }

It was simple and very efficient until the scheduled batch jobs ran. The trigger could not prevent the scheduled batch jobs from posting chatter feed into accounts. Upon further investigation, we found out that scheduled batch jobs run as a system and that might be the reason.
Any ideas on if we can delete the feed after its posted or have a way to prevent it from getting posted. 
NagendraNagendra (Salesforce Developers) 
Hi Venkat,

The best way to prevent these chatter feeds from being added in a certain context is to check for that context.

The System class has four methods that check various contexts, depending on which kind of classes your org runs, you may need to add one or several.
if (
    System.IsBatch()     ||
    System.IsQueryable() || 
    System.IsFuture()    ||
    System.IsScheduled() ||
    UserInfo.getProfileId() == '00eo0000000U2if'
) {
    // Prevent feed item creation...
}
Please let us know if you have any queries.

Thanks,
Nagendra.
 
Venkat R. BadeVenkat R. Bade
Hello Nagendra,

Tried this already the feed still, gets posted...