Newer Version Available
Define an Action Link in a Template and Post with a Feed Element
This example creates the same action link and action link group as the example Define an Action Link and Post with a Feed Element, but this example
instantiates the action link group from a template.
Step 1: Create the Action Link Templates
- From Setup, click .
- Use these values in a new Action Link Group Template:
Field Value Name Doc Example Developer Name Doc_Example Category Primary action Executions Allowed Once per User - Use these values in a new Action Link Template:
Field Value Action Link Group Template Doc Example Action Type Api Action URL /services/data/{!Bindings.ApiVersion}/chatter/feed-elements User Visibility Everyone can see HTTP Request Body { "subjectId":"{!Bindings.SubjectId}", "feedElementType":"FeedItem", "body":{ "messageSegments":[ { "type":"Text", "text":"{!Bindings.Text}" } ] } } HTTP Headers Content-Type: application/json Position 0 Label Key Post HTTP Method POST - Go back to the Action Link Group Template and select Published. Click Save.
Step 2: Instantiate the Action Link Group, Associate it with a Feed Item, and Post it
This example calls ConnectApi.ActionLinks.createActionLinkGroupDefinition(communityId, actionLinkGroup) to create an action link group definition.
It calls ConnectApi.ChatterFeeds.postFeedElement(communityId, feedElement, feedElementFileUpload) to associate the action link group with a feed item and post it.
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);