この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

トリガを使用した Chatter 非公開メッセージのモデレーション

ChatterMessage のトリガを記述して、組織またはコミュニティの非公開メッセージのモデレーションを自動化します。トリガを使用して、メッセージが会社のメッセージングポリシーに準拠していること、およびメッセージにブラックリストに登録された語が含まれていないことを保証できます。
使用可能なエディション: Salesforce Classic
使用可能なエディション: Enterprise Edition、Performance Edition、Unlimited Edition、および Developer Edition

必要なユーザ権限
ChatterMessage の Apex トリガを保存する Apex 開発

および

「Chatter メッセージとダイレクトメッセージの管理」

非公開メッセージの本文と送信者に関する情報を確認するには、Apex before insert トリガを記述します。検証メッセージをレコードまたは本文項目に追加することにより、メッセージが失敗し、エラーがユーザに返されます。

after insert トリガを作成することはできますが、ChatterMessage は更新することができないため、ChatterMessage を変更する after insert トリガは実行時に失敗して該当するエラーメッセージが表示されます。

非公開メッセージのトリガを作成するには、[設定] から、[クイック検索] ボックスに「ChatterMessage のトリガ」と入力し、[ChatterMessage のトリガ] を選択します。または、開発者コンソールで [ファイル] | [新規] | [Apex トリガ] をクリックし、[sObject] ドロップダウンリストから ChatterMessage を選択してもトリガを作成できます。

次の表は、ChatterMessage で公開される項目のリストです。

表 1. ChatterMessage で使用可能な項目
項目 Apex データ型 説明
Id ID Chatter メッセージの一意の識別子
Body String 送信者によって投稿された Chatter メッセージの本文
SenderId ID 送信者のユーザ ID
SentDate DateTime メッセージの送信日時
SendingNetworkId ID メッセージの送信元のネットワーク (コミュニティ)

この項目は、コミュニティが有効で、非公開メッセージが少なくとも 1 つのコミュニティで有効な場合にのみ表示されます。

次の例は、それぞれの新規メッセージを確認するために使用される ChatterMessage の before insert トリガを示します。このトリガはクラスメソッド moderator.review() をコールして、それぞれの新規メッセージを挿入前に確認します。

1trigger PrivateMessageModerationTrigger on ChatterMessage (before insert) {
2    ChatterMessage[] messages = Trigger.new;
3    
4    // Instantiate the Message Moderator using the factory method
5    MessageModerator moderator = MessageModerator.getInstance();
6    
7    for (ChatterMessage currentMessage : messages) {
8        moderator.review(currentMessage);    
9    }
10}

メッセージがポリシー違反の場合 (メッセージ本文にブラックリストに登録された語が含まれる場合など)、Apex addError メソッドをコールすることで、メッセージの送信を防ぐことができます。addError をコールして、項目またはメッセージ全体にカスタムエラーメッセージを追加できます。次のスニペットは、reviewContent メソッドのうち、エラーを��ッセージの [本文] 項目に追加する部分を示します。

1if (proposedMsg.contains(nextBlackListedWord)) {
2             theMessage.Body.addError(
3                 'This message does not conform to the acceptable use policy');
4             System.debug('moderation flagged message with word: ' 
5                 + nextBlackListedWord);
6             problemsFound=true;
7             break;
8          }

次に、送信者とメッセージの内容を確認するメソッドが含まれる MessageModerator クラスの全体を示します。このクラスのコードの一部は、簡略化のために削除されています。

1public class MessageModerator {
2   private Static List<String> blacklistedWords=null;
3   private Static MessageModerator instance=null;
4   
5   /**
6     Overall review includes checking the content of the message,
7     and validating that the sender is allowed to send messages.
8   **/
9   public void review(ChatterMessage theMessage) {
10    reviewContent(theMessage);
11    reviewSender(theMessage);
12   }
13   
14   /**
15     This method is used to review the content of the message. If the content
16     is unacceptable, field level error(s) are added.
17   **/
18   public void reviewContent(ChatterMessage theMessage) {
19      // Forcing to lower case for matching
20      String proposedMsg=theMessage.Body.toLowerCase();  
21      boolean problemsFound=false; // Assume it's acceptable
22      // Iterate through the blacklist looking for matches
23      for (String nextBlackListedWord : blacklistedWords) {
24          if (proposedMsg.contains(nextBlackListedWord)) {
25             theMessage.Body.addError(
26                 'This message does not conform to the acceptable use policy');
27             System.debug('moderation flagged message with word: ' 
28                 + nextBlackListedWord);
29             problemsFound=true;
30             break;
31          }
32         }
33         
34       // For demo purposes, we're going to add a "seal of approval" to the 
35       // message body which is visible.
36       if (!problemsFound) {
37         theMessage.Body = theMessage.Body + 
38             ' *** approved, meets conduct guidelines';
39       }
40         
41    }
42   
43   /**
44     Is the sender allowed to send messages in this context?
45     -- Moderators -- always allowed to send
46     -- Internal Members -- always allowed to send
47     -- Community Members -- in general only allowed to send if they have 
48           a sufficient Reputation
49     -- Community Members -- with insufficient reputation may message the 
50           moderator(s)
51   **/
52   public void reviewSender(ChatterMessage theMessage) {
53      // Are we in a Community Context?
54      boolean isCommunityContext = (theMessage.SendingNetworkId != null);
55 
56      // Get the User
57      User sendingUser = [SELECT Id, Name, UserType, IsPortalEnabled 
58                          FROM User where Id = :theMessage.SenderId ];  
59      // ...          
60   }   
61   
62   /**
63     Enforce a singleton pattern to improve performance
64   **/
65   public static MessageModerator getInstance() {
66     if (instance==null) {
67        instance = new MessageModerator();
68     }
69     return instance;
70   }
71   
72
73   /**
74     Default contructor is private to prevent others from instantiating this class 
75     without using the factory.
76     Initializes the static members.
77   **/
78   private MessageModerator() {
79      initializeBlackList();
80   }
81   /** 
82     Helper method that does the "heavy lifting" to load up the dictionaries 
83     from the database.  
84     Should only run once to initialize the static member which is used for 
85     subsequent validations.
86   **/
87   private void initializeBlackList() {
88      if (blacklistedWords==null) {
89          // Fill list of blacklisted words
90          // ...
91      }
92   }
93}