function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Douglas HauckDouglas Hauck 

Example of using PollCapabilityInput to vote?

Hi, all -

I'm trying to set up a helper method to generate a ConnectApi.PollCapabilityInput class.  (This is part of a larger helper class to generate FeedItemInputs.)  According to the documentation, this class has two properties:
  • choices:  List<String> 
    • The choices used to create a new poll. You must specify 2–10 poll choices for each poll. 
    • Required for creating a poll.
  • myChoiceId:  String 
    • ID of an existing choice on the feed poll.  Used to vote on an existing poll. 
    • Required for voting on a poll.
Using the first one to add a poll seems fairly straight-forward, but I'm having trouble with how to use the myChoiceId to vote on a poll.  Specifically, once I create a PollCapabilityInput object with that field set, how do I use it?  Do I just add it to a blank FeedItemInput and post it?  What feed to I post it to, and what kind of FeedElement is it?

I would really like to see a complete example for this Capability, but I can't seem to find one, here or anywhere else.  Can someone point me toward such an animal?  If not, does anyone at least know how to use this class to vote on an existing poll?

Thanks,
Doug
 
Arjun Kadayaprath 1Arjun Kadayaprath 1
Hi Doug,

Please let me know how you used ConnectApi.PollCapabilityInput to create a poll. I'm new to Salesforce and an example code would be of great help.

Thanks in advance,
Arjun.
doughauck-corpdoughauck-corp
Hi, Arjun - 

So first, I recommend you read the paragraph under ConnectApi.FeedElementCapabilityInput in the Apex Developers Guide (http://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connectapi_input_feed_element_capability.htm). 

Basically, you don't create a "poll"; you create a normal post (a ConnectApi.FeedItemInput or FeedElementInput object) and set its capabilities property to a new ConnectApi.FeedElementCapabilitiesInput object.  Now you can add any capabilites you want to that post, including polling, just by adding instances of the associated CapabilityInput classes (e.g. PollCapabilityInput) to the FeedElementCapabilitiesInput object you created. 

The project I was working on when I asked this question got cancelled, so I never did find out how to use it to respond to a poll.  But I have posted some code below that should allow you to at least create a poll.  I will warn you that it's never been tested (because cancelled) but I'm reasonably confident it will work, and I've added some comments to help you expand on it.  Hope it helps!!

Best,
Doug
 
public ConnectApi.FeedElement createPoll() 
    {
        // Create the poll and give it some choices.
        ConnectApi.PollCapabilityInput pollCapability = new ConnectApi.PollCapabilityInput();
        pollCapability.choices = new List<string> { 'Choice 1', 'Choice 2', 'Choice 3'};
        
        // Create a new ItemCapabilities object, and add the poll as a capability. 
        ConnectApi.FeedElementCapabilitiesInput itemCapabilities = new ConnectApi.FeedElementCapabilitiesInput();
        itemCapabilities.poll = pollCapability;
        
        // You could also add other capabilities (Files, Content, DirectMessage etc.)  
        // Some are displayed, while others (like DM) are behaviors.
        
        // Now create the parent feed item input (called 'input' because it's before posting; after posting it's a feed element).
        ConnectApi.FeedItemInput feedItem = new ConnectApi.FeedItemInput();	
        feedItem.feedElementType = ConnectApi.FeedElementType.FeedItem;		// This is the type for a normal post.
        feedItem.capabilities = itemCapabilities;							// Add the list of capabilities to the feed item.
        
        // That takes care of creating the Poll part of the post.  
        // Now let's finish it up with some message text, and direct it to a specific user.      
        
        // The message body is the main part of the message.  It consists of a series of segments (objects that inherit from MessageSegmentInput).
        // There are segments for Text, Images, Links, Hashtags, Mentions, etc.  Anything that can go in a post.
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();        
        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
        feedItem.body = messageBodyInput;
        
        // Note that the parser will recognize hashtags or links in your text string, and convert them to link or hashtag segments for you.  
        // It will NOT recognize mentions ('@username'), so you must handle those yourself (see below).
        ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
        textSegment.text = 'What choice do you want?';
        messageBodyInput.messageSegments.add(textSegment);

        // A mention segment is where you call out specific people who should see this.  (It's the equivalent of putting '@username' in a post.)
        ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
        mentionSegment.id = '005RR000000Dme9';								// The ID (or username, but w/o the '@') of the user being mentioned.
        messageBodyInput.messageSegments.add(mentionSegment);				// Add a separate mention segment for each user mentioned (up to 25).
        
        // Finally, the subjectId is the ID of the user/group/record whose feed you're posting to (or 'me' to post to feed of current user.)
        feedItem.subjectId = '0F9RR0000004CPw';
        
        // Now that the item is all set up, we use the ChatterFeeds.postFeedElement() static method to post it to the feed.
        // The getNetworkId() method returns the user's community, or null if he is in the internal community or Communities aren't set up.
        // If you know you don't use communities, you can just put 'internal' or null.
        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItem);
        
        // Return the resulting element, in case you want to do something else with it, but you don't have to.
        return feedElement;
    }



 
Arjun Kadayaprath 1Arjun Kadayaprath 1
That was real quick and of great help Doug.

Thanks a lot.
-Arjun.