Newer Version Available

This content describes an older version of this product. View Latest

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

  1. From Setup, enter Action Link Templates in the Quick Find box, then select Action Link Templates.
  2. 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
  3. 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
  4. 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) to associate the action link group with a feed item and post it.

1// Get the action link group template Id.
2ActionLinkGroupTemplate template = [SELECT Id FROM ActionLinkGroupTemplate WHERE DeveloperName='Doc_Example'];
3
4// Add binding name-value pairs to a map.
5// The names are defined in the action link template(s) associated with the action link group template.
6// Get them from Setup UI or SOQL.
7Map<String, String> bindingMap = new Map<String, String>();
8bindingMap.put('ApiVersion', 'v33.0');
9bindingMap.put('Text', 'This post was created by an API action link.');
10bindingMap.put('SubjectId', 'me');
11
12// Create ActionLinkTemplateBindingInput objects from the map elements.
13List<ConnectApi.ActionLinkTemplateBindingInput> bindingInputs = new List<ConnectApi.ActionLinkTemplateBindingInput>();
14
15for (String key : bindingMap.keySet()) {
16    ConnectApi.ActionLinkTemplateBindingInput bindingInput = new ConnectApi.ActionLinkTemplateBindingInput();
17    bindingInput.key = key;
18    bindingInput.value = bindingMap.get(key);
19    bindingInputs.add(bindingInput);
20}
21
22// Set the template Id and template binding values in the action link group definition.
23ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new ConnectApi.ActionLinkGroupDefinitionInput();
24actionLinkGroupDefinitionInput.templateId = template.id;
25actionLinkGroupDefinitionInput.templateBindings = bindingInputs;
26
27// Instantiate the action link group definition.
28ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition =
29 ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(), actionLinkGroupDefinitionInput);
30
31ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
32ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
33ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new ConnectApi.AssociatedActionsCapabilityInput();
34ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
35ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
36
37// Define the FeedItemInput object to pass to postFeedElement
38feedItemInput.body = messageBodyInput;
39feedItemInput.capabilities = feedElementCapabilitiesInput;
40feedItemInput.subjectId = 'me';
41
42// The MessageBodyInput object holds the text in the post
43messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
44
45textSegmentInput.text = 'Click to post a feed item.';
46messageBodyInput.messageSegments.add(textSegmentInput);
47
48
49// The FeedElementCapabilitiesInput object holds the capabilities of the feed item.
50// For this feed item, we define an associated actions capability to hold the action link group.
51// The action link group ID is returned from the call to create the action link group definition. 
52feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
53associatedActionsCapabilityInput.actionLinkGroupIds = new List<String>();
54associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);
55
56// Post the feed item. 
57ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);