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

アクションリンクを定義し、フィード要素を使用して投稿する

この例では、アクションリンクグループ内に 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) へのコールでフィード要素と関連付けます。

このコードを使用するには、独自の Salesforce 組織の OAuth 値に置き換えます。また、expirationDate が将来の日付であることを確認します。コード内で To Do コメントを探します。

1ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new ConnectApi.ActionLinkGroupDefinitionInput();
2ConnectApi.ActionLinkDefinitionInput actionLinkDefinitionInput = new ConnectApi.ActionLinkDefinitionInput();
3ConnectApi.RequestHeaderInput requestHeaderInput1 = new ConnectApi.RequestHeaderInput();
4ConnectApi.RequestHeaderInput requestHeaderInput2 = new ConnectApi.RequestHeaderInput();
5
6// Create the action link group definition.
7actionLinkGroupDefinitionInput.actionLinks = New List<ConnectApi.ActionLinkDefinitionInput>();
8actionLinkGroupDefinitionInput.executionsAllowed = ConnectApi.ActionLinkExecutionsAllowed.OncePerUser;
9actionLinkGroupDefinitionInput.category = ConnectApi.PlatformActionGroupCategory.Primary;
10// To Do: Verify that the date is in the future.
11// Action link groups are removed from feed elements on the expiration date.
12datetime myDate = datetime.newInstance(2016, 3, 1);
13actionLinkGroupDefinitionInput.expirationDate = myDate;
14
15// Create the action link definition.
16actionLinkDefinitionInput.actionType = ConnectApi.ActionLinkType.Api;
17actionLinkDefinitionInput.actionUrl = '/services/data/v33.0/chatter/feed-elements';
18actionLinkDefinitionInput.headers = new List<ConnectApi.RequestHeaderInput>();
19actionLinkDefinitionInput.labelKey = 'Post';
20actionLinkDefinitionInput.method = ConnectApi.HttpRequestMethod.HttpPost;
21actionLinkDefinitionInput.requestBody = '{\"subjectId\": \"me\",\"feedElementType\": \"FeedItem\",\"body\": {\"messageSegments\": [{\"type\": \"Text\",\"text\": \"This is a test post created via an API action link.\"}]}}';
22actionLinkDefinitionInput.requiresConfirmation = true;
23
24// To Do: Substitute an OAuth value for your Salesforce org. 
25requestHeaderInput1.name = 'Authorization';
26requestHeaderInput1.value = 'OAuth 00DD00000007WNP!ARsAQCwoeV0zzAV847FTl4zF.85w.EwsPbUgXR4SAjsp';
27actionLinkDefinitionInput.headers.add(requestHeaderInput1);
28
29requestHeaderInput2.name = 'Content-Type';
30requestHeaderInput2.value = 'application/json';
31actionLinkDefinitionInput.headers.add(requestHeaderInput2);
32
33// Add the action link definition to the action link group definition.
34actionLinkGroupDefinitionInput.actionLinks.add(actionLinkDefinitionInput);
35
36// Instantiate the action link group definition.
37ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition = ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(), actionLinkGroupDefinitionInput);
38
39ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
40ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
41ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new ConnectApi.AssociatedActionsCapabilityInput();
42ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
43ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
44
45// Set the properties of the feedItemInput object.
46feedItemInput.body = messageBodyInput;
47feedItemInput.capabilities = feedElementCapabilitiesInput;
48feedItemInput.subjectId = 'me';
49
50// Create the text for the post.
51messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
52textSegmentInput.text = 'Click to post a feed item.';  
53messageBodyInput.messageSegments.add(textSegmentInput);
54
55
56// The feedElementCapabilitiesInput object holds the capabilities of the feed item.
57// Define an associated actions capability to hold the action link group.
58// The action link group ID is returned from the call to create the action link group definition. 
59feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
60associatedActionsCapabilityInput.actionLinkGroupIds = new List<String>();
61associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);
62
63// Post the feed item. 
64ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);

投稿に失敗した場合、OAuth ID を確認します。

メモ