使用触发器审核 Chatter 私人消息

为 ChatterMessage 编写一个触发器,以自动审核 组织或 Experience Cloud 站点中的私人消息。使用触发器确保消息 符合贵公司的邮件策略,并且不包含列入黑名单的内容 的话。

适用于:Salesforce Classic
适用于:EnterprisePerformanceUnlimited 和 Developer Edition
用户权限 需要
要保存 ChatterMessage 的 Apex 触发器,请执行以下操作:作者 Apex和管理 Chatter 消息和 私信

在插入触发器之前编写 Apex 以查看私人消息 正文和有关发件人的信息。您可以将验证消息添加到记录或 正文字段,这会导致消息失败并返回错误给用户。

虽然您可以创建插入后触发器, ChatterMessage 不可更新,因此任何插入后触发 修改 ChatterMessage 将在运行时失败,并显示相应的错误消息。

要从“设置”中为私人消息创建触发器,请输入“快速查找”框,然后选择“ChatterMessage 触发器”。或者,您可以通过以下方式从开发人员控制台创建触发器 单击“文件”|”新品 |顶点触发器,然后从 sObject 下拉列表中选择 ChatterMessage。ChatterMessage Triggers

下表列出了公开的字段 喋喋不休的消息。

Apex 数据类型描述
同上编号Chatter 消息的唯一标识符
身体字符串发件人发布的 Chatter 消息的正文
发件人 ID编号发件人的用户 ID
发送日期日期时间发送邮件的日期和时间
SendingNetworkId编号发送消息的网络(站点)。仅当 已启用数字体验,并且至少在一个中启用了私人消息 网站。

此示例显示了 ChatterMessage 上用于查看的 before insert 触发器 每条新消息。此触发器调用类方法 ,以在插入每条新消息之前对其进行检查。moderator.review()

trigger PrivateMessageModerationTrigger on ChatterMessage (before insert) {
    ChatterMessage[] messages = Trigger.new;
    
    // Instantiate the Message Moderator using the factory method
    MessageModerator moderator = MessageModerator.getInstance();
    
    for (ChatterMessage currentMessage : messages) {
        moderator.review(currentMessage);    
    }
}

如果邮件违反了您的政策,例如,当邮件正文包含列入黑名单时 words,可以通过调用 Apex 方法阻止发送消息。您可以调用以在字段或 整个消息。以下代码片段显示了向消息“正文”字段添加错误的方法的一部分。addErroraddErrorreviewContent

if (proposedMsg.contains(nextBlockListedWord)) {
             theMessage.Body.addError(
                 'This message does not conform to the acceptable use policy');
             System.debug('moderation flagged message with word: ' 
                 + nextBlockListedWord);
             problemsFound=true;
             break;
          }

以下是完整的课程, 其中包含用于查看发件人和邮件内容的方法。部分 为简洁起见,已删除此类中的代码。MessageModerator

public class MessageModerator {
   private Static List<String> blocklistedWords=null;
   private Static MessageModerator instance=null;
   
   /**
     Overall review includes checking the content of the message,
     and validating that the sender is allowed to send messages.
   **/
   public void review(ChatterMessage theMessage) {
    reviewContent(theMessage);
    reviewSender(theMessage);
   }
   
   /**
     This method is used to review the content of the message. If the content
     is unacceptable, field level error(s) are added.
   **/
   public void reviewContent(ChatterMessage theMessage) {
      // Forcing to lower case for matching
      String proposedMsg=theMessage.Body.toLowerCase();  
      boolean problemsFound=false; // Assume it's acceptable
      // Iterate through the blocklist looking for matches
      for (String nextBlockListedWord : blocklistedWords) {
          if (proposedMsg.contains(nextBlockListedWord)) {
             theMessage.Body.addError(
                 'This message does not conform to the acceptable use policy');
             System.debug('moderation flagged message with word: ' 
                 + nextBlockListedWord);
             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';
       }
         
    }
   
   /**
     Is the sender allowed to send messages in this context?
     -- Moderators -- always allowed to send
     -- Internal Members -- always allowed to send
     -- Site Members -- in general only allowed to send if they have 
           a sufficient Reputation
     -- Site Members -- with insufficient reputation may message the 
           moderator(s)
   **/
   public void reviewSender(ChatterMessage theMessage) {
      // Are we in a Site Context?
      boolean isSiteContext = (theMessage.SendingNetworkId != null);
 
      // Get the User
      User sendingUser = [SELECT Id, Name, UserType, IsPortalEnabled 
                          FROM User where Id = :theMessage.SenderId ];  
      // ...          
   }   
   
   /**
     Enforce a singleton pattern to improve performance
   **/
   public static MessageModerator getInstance() {
     if (instance==null) {
        instance = new MessageModerator();
     }
     return instance;
   }
   

   /**
     Default contructor is private to prevent others from instantiating this class 
     without using the factory.
     Initializes the static members.
   **/
   private MessageModerator() {
      initializeBlockList();
   }
   /** 
     Helper method that does the "heavy lifting" to load up the dictionaries 
     from the database.  
     Should only run once to initialize the static member which is used for 
     subsequent validations.
   **/
   private void initializeBlockList() {
      if (blocklistedWords==null) {
          // Fill list of blocklisted words
          // ...
      }
   }
}