Newer Version Available
アクションリンクを定義し、フィード要素を使用して投稿する
この例では、アクションリンクグループ内に 1 つのアクションリンクを作成し、アクションリンクグループをフィード項目に関連付けてそのフィード項目を投稿します。
ユーザがこのアクションリンクをクリックすると、ユーザのフィードにフィード項目を投稿する Chatter REST API リソース /chatter/feed-elements が要求されます。ユーザがクリックしたアクションリンクが正常に実行されると、その状況は「成功」に変更され、フィード項目の UI が更新されます。
ユーザのフィードを更新して新規投稿を表示させます。
これは単純な例ですが、アクションリンクを使用して Salesforce リソースへのコールを行う方法が示されています。
アクションリンクはフィード項目のボタンと考えます。ボタンのように、アクションリンク定義には表示ラベル (labelKey) があります。アクションリンクグループ定義には、URL (actionUrl) や HTTP メソッド (method) のほか、省略可能なリクエストボディ (requestBody) や HTTP ヘッダー (headers) など、他にもプロパティがあります。
ユーザがこのアクションリンクをクリックすると、Chatter REST API に対して HTTP POST 要求が実行され、フィード項目が Chatter に投稿されます。requestBody プロパティは、新しいフィード項目のテキストなど、actionUrl リソースのリクエストボディを保持します。この例では、新しいフィード項目にテキストしか含まれていませんが、添付ファイルやアンケートなどの他の機能やアクションリンクも含めることができます。
ラジオボタンと同様に、アクションリンクはグループ内にネストする必要があります。グループ内のアクションリンクは、グループのプロパティを共有し、相互に排他的です (クリックできるのは、グループ内の 1 つのアクションリンクのみです)。1 つのアクションリンクを定義する場合でも、アクションリンクグループに含める必要があります。
この例では、ConnectApi.ActionLinks.createActionLinkGroupDefinition(communityId, actionLinkGroup) をコールしてアクションリンクグループ定義を作成します。
そのコールからアクションリンクグループ ID を保存し、ConnectApi.ChatterFeeds.postFeedElement(communityId, feedElement, feedElementFileUpload) へのコールでフィード要素と関連付けます。
このコードを使用するには、独自の Salesforce 組織の OAuth 値に置き換えます。また、expirationDate が将来の日付であることを確認します。コード内で To Do コメントを探します。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new ConnectApi.ActionLinkGroupDefinitionInput();
18ConnectApi.ActionLinkDefinitionInput actionLinkDefinitionInput = new ConnectApi.ActionLinkDefinitionInput();
19ConnectApi.RequestHeaderInput requestHeaderInput1 = new ConnectApi.RequestHeaderInput();
20ConnectApi.RequestHeaderInput requestHeaderInput2 = new ConnectApi.RequestHeaderInput();
21
22// Create the action link group definition.
23actionLinkGroupDefinitionInput.actionLinks = New List<ConnectApi.ActionLinkDefinitionInput>();
24actionLinkGroupDefinitionInput.executionsAllowed = ConnectApi.ActionLinkExecutionsAllowed.OncePerUser;
25actionLinkGroupDefinitionInput.category = ConnectApi.PlatformActionGroupCategory.Primary;
26// To Do: Verify that the date is in the future.
27// Action link groups are removed from feed elements on the expiration date.
28datetime myDate = datetime.newInstance(2016, 3, 1);
29actionLinkGroupDefinitionInput.expirationDate = myDate;
30
31// Create the action link definition.
32actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Api;
33actionLinkDefinitionInput.actionUrl = '/services/data/v33.0/chatter/feed-elements';
34actionLinkDefinitionInput.headers = new List<ConnectApi.RequestHeaderInput>();
35actionLinkDefinitionInput.labelKey = 'Post';
36actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpPost;
37actionLinkDefinitionInput.requestBody = '{\"subjectId\": \"me\",\"feedElementType\": \"FeedItem\",\"body\": {\"messageSegments\": [{\"type\": \"Text\",\"text\": \"This is a test post created via an API action link.\"}]}}';
38actionLinkDefinitionInput.requiresConfirmation = true;
39
40// To Do: Substitute an OAuth value for your Salesforce org.
41requestHeaderInput1.name = 'Authorization';
42requestHeaderInput1.value = 'OAuth 00DD00000007WNP!ARsAQCwoeV0zzAV847FTl4zF.85w.EwsPbUgXR4SAjsp';
43actionLinkDefinitionInput.headers.add(requestHeaderInput1);
44
45requestHeaderInput2.name = 'Content-Type';
46requestHeaderInput2.value = 'application/json';
47actionLinkDefinitionInput.headers.add(requestHeaderInput2);
48
49// Add the action link definition to the action link group definition.
50actionLinkGroupDefinitionInput.actionLinks.add(actionLinkDefinitionInput);
51
52// Instantiate the action link group definition.
53ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition = ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(), actionLinkGroupDefinitionInput);
54
55ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
56ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
57ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new ConnectApi.AssociatedActionsCapabilityInput();
58ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
59ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
60
61// Set the properties of the feedItemInput object.
62feedItemInput.body = messageBodyInput;
63feedItemInput.capabilities = feedElementCapabilitiesInput;
64feedItemInput.subjectId = 'me';
65
66// Create the text for the post.
67messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
68textSegmentInput.text = 'Click to post a feed item.';
69messageBodyInput.messageSegments.add(textSegmentInput);
70
71
72// The feedElementCapabilitiesInput object holds the capabilities of the feed item.
73// Define an associated actions capability to hold the action link group.
74// The action link group ID is returned from the call to create the action link group definition.
75feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
76associatedActionsCapabilityInput.actionLinkGroupIds = new List<String>();
77associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);
78
79// Post the feed item.
80ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);