Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Send a Direct Message
Call a method to send a direct message.
Call postFeedElement(communityId,
feedElement) to send a direct message to two people.
1// Define the FeedItemInput object to pass to postFeedElement
2ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
3
4ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
5textSegmentInput.text = 'Thanks for attending my presentation test run this morning. Send me any feedback.';
6
7// The MessageBodyInput object holds the text in the post
8ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
9messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
10messageBodyInput.messageSegments.add(textSegmentInput);
11feedItemInput.body = messageBodyInput;
12
13// The FeedElementCapabilitiesInput object holds the capabilities of the feed item.
14// For this feed item, we define a direct message capability to hold the member(s) and the subject.
15
16List<String> memberIds = new List<String>();
17memberIds.add('005B00000016OUQ');
18memberIds.add('005B0000001rIN6');
19
20ConnectApi.DirectMessageCapabilityInput dmInput = new ConnectApi.DirectMessageCapabilityInput();
21dmInput.subject = 'Thank you!';
22dmInput.membersToAdd = memberIds;
23
24ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
25feedElementCapabilitiesInput.directMessage = dmInput;
26
27feedItemInput.capabilities = feedElementCapabilitiesInput;
28
29// Post the feed item.
30ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);