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
kirankumarreddy punuru 10kirankumarreddy punuru 10 

how delete the post from the chatter if the post contains specific keywords

Hi ,
Can some one tell me how to delete the comment from the chatter post if the post contains specific keywords in the set.any ideas?

Regards,
Kiran
AshlekhAshlekh
Hi,

You can write a trigger on FeedItem object to get the value which is enter by User in the chatter Post.

Actually when user post on chatter then all the post is saved in FeedItem object.
 
trigger FeedIteamTrigger on FeedItem (before insert) 
{
    for (FeedItem a : Trigger.new)
    {
          System.debug('-----POST----------------'+a);
    }
}


-Thanks
Ashlekh Gera
 
Austin GAustin G
In addition to AKG's response, you can take the business logic you need and put it in an external class. You'll need to have a list of words you need to find, and maintain it in that external class. Depending on how many phrases or keywords you're looking for, you might be able to use regex to do a replace for keywords, SSN's, or other sensitive info. If you're deleting FeedItems (or using any other DML statements), make sure to avoid calling that delete inside a loop.
 
trigger ValidateFeedContents on FeedItem (after insert, after update)
{
    List<FeedItem> chatterPostsToDelete = new List<FeedItem>():

    for (FeedItem item : Trigger.new)
    {
        if(ChatterUtility.TextContainsReservedWords(item.Body)
            chatterPostsToDelete.add(item):
    }
    
    delete chatterPostsToDelete;
}

/* External Class */
public class ChatterUtility
{

    public string[] keywords = new string[] {'[0-9][3]-[0-9]{2}-[0-9]{4}', 'Worda', 'Wordb'}; //Add whatever regex strings you want to look for here. This array looks for SSNs, and the phrases Worda and Wordb
    
    public static boolean TextContainsReservedWords(string input)
    {   
        for(string keyphrase : keywords)
        {
            if(input.contains(keyphrase))
                return true;        
        }
        return false;
    }
}

If what you need to do is more complex, then you might need to use a different mechanism than regex. Regex is great for pattern matching, but can have some performance issues if used on large inputs.