Newer Version Available
テンプレートのアクションリンクを定義し、フィード要素を使用して投稿する
この例では、「アクションリンクを定義し、フィード要素を使用して投稿する」の例と同じアクションリンクとアクションリンクグループを作成しますが、テンプレートからアクションリンクグループをインスタンス化します。
ステップ 1: アクションリンクテンプレートを作成する
- [設定] で、 をクリックします。
- 新しいアクションリンクグループテンプレートで次の値を使用します。
項目 値 名前 ドキュメントの例 開発者名 Doc_Example カテゴリ プライマリアクション 実行可 ユーザごとに 1 回 - 新しいアクションリンクテンプレートで次の値を使用します。
項目 値 アクションリンクグループテンプレート ドキュメントの例 アクションの種類 Api アクション URL /services/data/{!Bindings.ApiVersion}/chatter/feed-elements ユーザ表示設定 全員に表示 HTTP リクエストボディ {"subjectId": "{!Bindings.SubjectId}","feedElementType": "FeedItem","body": {"messageSegments": [{"type": "Text","text": "{!Bindings.Text}"}]}} HTTP ヘッダー Content-Type: application/json 位置 0 表示ラベルキー 投稿 HTTP メソッド POST - アクションリンクグループテンプレートに戻り、[公開済み] を選択します。[保存] をクリックします。
ステップ 2: アクションリンクグループをインスタンス化し、フィード項目に関連付けて投稿する
この例では、ConnectApi.ActionLinks.createActionLinkGroupDefinition(communityId, actionLinkGroup) をコールしてアクションリンクグループ定義を作成します。
ConnectApi.ChatterFeeds.postFeedElement(communityId, feedElement, feedElementFileUpload) をコールしてアクションリンクグループをフィード項目に関連付けて投稿します。
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Get the action link group template Id.
18ActionLinkGroupTemplate template = [SELECT Id FROM ActionLinkGroupTemplate WHERE DeveloperName='Doc_Example'];
19
20// Add binding name-value pairs to a map.
21// The names are defined in the action link template(s) associated with the action link group template.
22// Get them from Setup UI or SOQL.
23Map<String, String> bindingMap = new Map<String, String>();
24bindingMap.put('ApiVersion', 'v33.0');
25bindingMap.put('Text', 'This post was created by an API action link.');
26bindingMap.put('SubjectId', 'me');
27
28// Create ActionLinkTemplateBindingInput objects from the map elements.
29List<ConnectApi.ActionLinkTemplateBindingInput> bindingInputs = new List<ConnectApi.ActionLinkTemplateBindingInput>();
30
31for (String key : bindingMap.keySet()) {
32 ConnectApi.ActionLinkTemplateBindingInput bindingInput = new ConnectApi.ActionLinkTemplateBindingInput();
33 bindingInput.key = key;
34 bindingInput.value = bindingMap.get(key);
35 bindingInputs.add(bindingInput);
36}
37
38// Set the template Id and template binding values in the action link group definition.
39ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new ConnectApi.ActionLinkGroupDefinitionInput();
40actionLinkGroupDefinitionInput.templateId = template.id;
41actionLinkGroupDefinitionInput.templateBindings = bindingInputs;
42
43// Instantiate the action link group definition.
44ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition =
45 ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(), actionLinkGroupDefinitionInput);
46
47ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
48ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
49ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new ConnectApi.AssociatedActionsCapabilityInput();
50ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
51ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
52
53// Define the FeedItemInput object to pass to postFeedElement
54feedItemInput.body = messageBodyInput;
55feedItemInput.capabilities = feedElementCapabilitiesInput;
56feedItemInput.subjectId = 'me';
57
58// The MessageBodyInput object holds the text in the post
59messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
60
61textSegmentInput.text = 'Click to post a feed item.';
62messageBodyInput.messageSegments.add(textSegmentInput);
63
64
65// The FeedElementCapabilitiesInput object holds the capabilities of the feed item.
66// For this feed item, we define an associated actions capability to hold the action link group.
67// The action link group ID is returned from the call to create the action link group definition.
68feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
69associatedActionsCapabilityInput.actionLinkGroupIds = new List<String>();
70associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);
71
72// Post the feed item.
73ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);