LoginSignup
0
1

More than 3 years have passed since last update.

ストリーミングAPIで受信したイベントの絞込み

Last updated at Posted at 2020-12-08

やりたいこと

自分がケース登録後、発番されたケース番号をOpenCTIソフトフォン(左下のやつ)に伝えたい
自分以外がケース登録したものは伝えない

実現イメージ

同一組織に田中と上川を作成して、別ブラウザで同時に開き上川でケースを作成したスクショです。
上川にのみ通知が来てるかと思います。

★上川にのみ通知

PushTopic1.PNG

★田中に通知はこない

pushtopic2.PNG

実現方法

参考:例: 再生なしの対話型 Visualforce ページ

PushTopicの作成

ケース作成時のみ通知してほしいので、NotifyForOperationCreate=trueにします。QueryのSelectには自分が作成したケースか判定に使用するためCreatedByIdを含めます。Salesforceの画面から作成できないため、開発者コンソールから作成します。

PushTopic pushTopic = new PushTopic();
pushTopic.Name = 'CaseCreated';
pushTopic.Query = 'SELECT Id, CaseNumber, CreatedById FROM Case';
pushTopic.ApiVersion = 50.0;
pushTopic.NotifyForOperationCreate = true;
pushTopic.NotifyForOperationUpdate = false;
pushTopic.NotifyForOperationUndelete = false;
pushTopic.NotifyForOperationDelete = false;
pushTopic.NotifyForFields = 'Referenced';
insert pushTopic;

静的リソースを作成

こちらを参考に静的リソースを作成してください。
demo.jsの以下の部分を変更します。

function subscribe(topic) {  
//...
//CreatedByIdで受信メッセージを絞込み
  channel = topic + '?CreatedById=' + userId;
//...     
}

ソフトフォン(VF)の作成

先ほどのdemo.jsと併せて以下の様な形で受信の絞込を設定します。
登録の際の絞り込み条件の設定

const userId = '{!$User.Id}';
/topic/CaseCreated?CreatedById=userId

ソフトフォン(VF)

こちらを参考にVFを作成してください。コメントの部分を追加してください。


<apex:page>
<apex:includeScript value="{!$Resource.json2_js}"/>
<script type="text/javascript" src="{!URLFOR($Resource.cometd_zip, 'dojo/dojo.js')}" data-dojo-config="async: 1"></script>
<apex:stylesheet value="{!$Resource.demo_css}"/>
<script>var token = '{!$Api.Session_ID}';</script>
        <div id="demo">
            <div id="datastream"></div>
        <script type="text/javascript" src="{!$Resource.demo_js}">
</script>
<script>
   //画面側でuseidを取得し、jsでチャネルのパラメータに設定
   const userId = '{!$User.Id}';
   console.log(userid);
</script> 
       <div id="input">
                <div id="join">
                    <table>
                        <tbody>
                            <tr>
                                <td>&nbsp;</td>
                                <td> Enter Topic Name </td>
                                <td>
                                    <input id="topic" type="text" />
                                </td>
                                <td>
                                    <button id="subscribeButton" 
                                        class="button">Subscribe</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div id="joined">
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    <button id="leaveButton" 
                                        class="button">Unsubscribe</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
    </div>
</apex:page>

プラットフォームイベントではできないのか?

ドキュメント見るとだいたい似たようなことは出来そうな気がしますが、自分のケース作成時だけ通知するのが出来なさそうです。

登録の際の絞り込み条件の設定
プラットフォームイベントでは、ストリーミング API における登録の際の絞り込み条件の設定はサポートされません。
https://developer.salesforce.com/docs/atlas.ja-jp.platform_events.meta/platform_events/platform_events_api_considerations.htm

プラットフォームイベントの場合は画面側で通知されたCreatedByIDで判定が必要かと思います。

あと、プラットフォームイベントを公開するのにケース作成時にトリガやプロセスビルダー等が必要なのがストリーミングAPIとの違いですね。
インテグレーションアーキテクトの資格試験ですと、こういうケースはストリーミングAPIが推奨されていました。

制限事項

制限もストリーミングAPIとこの部分は同じでした。
すべてのチャネルおよびすべてのイベント種別での、同時 CometD クライアント (登録者) の最大数 2,000(Performance Edition および Unlimited Edition) 1,000(Enterprise Edition)
https://developer.salesforce.com/docs/atlas.ja-jp.platform_events.meta/platform_events/platform_event_limits.htm

最後に

CTI周りで以下の記事も書いてみたのでどうぞ!
Open CTI で Lightning Message Service を使用してみる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1