Chatter Private Messages Trigger

Write a trigger for ChatterMessage to automate the moderation of private messages in an organization or community. Use triggers to ensure that messages conform to your company’s messaging policies and don’t contain blacklisted words.Although you can create an after insert trigger, ChatterMessage is not updatable, and consequently, any after insert trigger that modifies ChatterMessage will fail at runtime with an appropriate error message.Here the trigger
trigger ChatterMessageTrigger on ChatterMessage (before insert) {
    ChatterMessage[] messages = Trigger.new;
    MessageModerator moderator = MessageModerator.getInstance();
    for (ChatterMessage currentMessage : messages) {
        moderator.review(currentMessage);   
        
    }
    
}

This example shows a before insert trigger on ChatterMessage that is used to review each new message. This trigger calls a class method, moderator.review(), to review each new message before it is inserted. If a message violates your policy, for example when the message body contains blacklisted words, you can prevent the message from being sent by calling the Apex addError method. You can call addError to add a custom error message on a field or on the entire message. The following snippet shows a portion of the review content method that adds an error to the message Body field.

 

public class MessageModerator {
    private Static List<String> blacklistedWords=null;
    private Static MessageModerator instance=null;
    public void review(ChatterMessage theMessage) {
        reviewContent(theMessage);
        reviewSender(theMessage);
    }
    public void reviewContent(ChatterMessage theMessage) {
        String proposedMsg=theMessage.Body.toLowerCase();  
        boolean problemsFound=false; // Assume it's acceptable
        // Iterate through the blacklist looking for matches
        for (String nextBlackListedWord : blacklistedWords) {
            if (proposedMsg.contains(nextBlackListedWord)) {
                theMessage.Body.addError(
                    'This message does not conform to the acceptable use policy');
                System.debug('moderation flagged message with word: ' 
                             + nextBlackListedWord);
                problemsFound=true;
                break;
            }
        }
        
        // For demo purposes, we're going to add a "seal of approval" to the 
        // message body which is visible.
        if (!problemsFound) {
            theMessage.Body = theMessage.Body + 
                ' *** approved, meets conduct guidelines';
        }
        
    }
    
    public void reviewSender(ChatterMessage theMessage) {
        // Are we in a Community Context?
        boolean isCommunityContext = (theMessage.SendingNetworkId != null);
        
        // Get the User
        User sendingUser = [SELECT Id, Name, UserType, IsPortalEnabled 
                            FROM User where Id = :theMessage.SenderId ];  
        // ...          
    }   
    
    public static MessageModerator getInstance() {
        if (instance==null) {
            instance = new MessageModerator();
        }
        return instance;
    }
    
    
    private MessageModerator() {
        initializeBlackList();
    }
    private void initializeBlackList() {
        if (blacklistedWords==null) {
            blacklistedWords = new List<String> ();
            blacklistedWords.add('heavy lifting') ; 
            blacklistedWords.add('Real-time') ; 
            blacklistedWords.add('UnHealthy') ; 
        }
    }
}

The below image show when you are trying to send the chatter message with the body that contains blacklisted words then it will through an error message.