{"id":141,"date":"2014-02-05T12:42:51","date_gmt":"2014-02-05T20:42:51","guid":{"rendered":"http:\/\/www.bridgefarmconsulting.com\/blog\/?p=141"},"modified":"2014-02-05T12:42:51","modified_gmt":"2014-02-05T20:42:51","slug":"new-helper-classes-for-working-with-the-connectapi","status":"publish","type":"post","link":"https:\/\/www.bridgefarmconsulting.com\/blog\/new-helper-classes-for-working-with-the-connectapi\/","title":{"rendered":"New helper classes for working with the ConnectAPI"},"content":{"rendered":"<p dir=\"ltr\">I was, as my friend <a href=\"https:\/\/twitter.com\/BeckyMaeW\" target=\"_blank\">Becky<\/a> will tell you, a bit of a <a href=\"https:\/\/www.salesforce.com\/chatter\/overview\/\" target=\"_blank\">Chatter<\/a> sceptic when it was first released. But over time, I have begun to see huge potential for using Chatter as a notification and collaboration tool. So I was excited when they released the <a href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_classes_connect_api.htm\" target=\"_blank\">ConnectAPI<\/a> &#8211; an Apex wrapper for the Chatter REST API. As this excellent\u00a0<a href=\"http:\/\/appirio.com\/category\/tech-blog\/2014\/01\/chatter-apex\/\" target=\"_blank\">post<\/a> explains, the ConnectAPI definitely makes consuming Chatter feed data easier.<\/p>\n<p dir=\"ltr\">But writing to Chatter using the ConnectAPI has often seemed<span style=\"line-height: 1.714285714; font-size: 1rem;\">\u00a0more challenging than it should, especially when you wanted to use features like @mention.\u00a0<\/span>You can&#8217;t write a post containing @mention using regular Apex, so you have to use the ConnectAPI. But writing a simple post like &#8216;Happy Birthday @Peter Churchill! &#8216; requires 15 lines of code, because you have to use the messageSegment objects:<\/p>\n<pre class=\"brush: java; gutter: true\">ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();\r\nmessageInput.messageSegments = new List&lt;ConnectApi.MessageSegmentInput&gt;();\r\n\r\nConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();\r\ntextSegment.text = &#039;Happy Birthday &#039;;\r\nmessageInput.messageSegments.add(textSegment);\r\n\r\nConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();\r\nmentionSegment.id = &#039;005D00000015tjz&#039;; \/\/ The ID of the user to mention.\r\nmessageInput.messageSegments.add(mentionSegment);\r\n\r\ntextSegment = new ConnectApi.TextSegmentInput();\r\ntextSegment.text = &#039;!&#039;;\r\nmessageInput.messageSegments.add(textSegment);\r\n\r\nConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();\r\ninput.body = messageInput;\r\n\r\nConnectApi.FeedItem fi = ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.News, &#039;me&#039;, input, null);<\/pre>\n<p dir=\"ltr\">While it is nice to have the flexibility that has been made available to us with messageSegments, it also requires a significant investment in time and effort and patience to get it all working. But as we know, Salesforce has always been a company that seeks out feedback. So when\u00a0<a href=\"https:\/\/twitter.com\/LaceySnr\" target=\"_blank\">Matt Lacey<\/a>\u00a0and I recently talked with some of the folks behind the ConnectAPI about how we were finding it difficult to use, they listened, and acted on the feedback. Last week, the ConnectAPI team released a <a href=\"https:\/\/github.com\/alouie-sfdc\/ConnectApiHelper\" target=\"_blank\">helper class<\/a>\u00a0to make it easier to use @mentions!\u00a0Just copy the code from the link above into your Dev Org, and suddenly you can write the same Happy Birthday post like this:<\/p>\n<pre class=\"brush: java; gutter: true\">String sText = &#039;Happy Birthday {005E0000005LZXh}!&#039;;\r\nConnectApi.FeedItem fi = ConnectApiHelper.postFeedItemWithMentions(null, &#039;me&#039;, sText);<\/pre>\n<p>You can also post comments with @mentions &#8211; so something like this will work:<\/p>\n<pre class=\"brush: java; gutter: true\">String sText = &#039;Happy Birthday {005E0000005LZXh}!&#039;;\r\nConnectApi.FeedItem fi = ConnectApiHelper.postFeedItemWithMentions(null, &#039;me&#039;, sText);\r\n\r\nString sText2 = &#039;Thanks {005E0000001fhsc}!&#039;;\r\nConnectApi.Comment fic = ConnectApiHelper.postCommentWithMentions(null, fi.Id, sText2);<\/pre>\n<p><a href=\"http:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.26.30-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-144\" alt=\"Screen Shot 2014-02-04 at 9.26.30 PM\" src=\"http:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.26.30-PM-300x138.png\" width=\"300\" height=\"138\" srcset=\"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.26.30-PM-300x138.png 300w, https:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.26.30-PM.png 478w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">Pretty awesome. \u00a0Even better, the code will parse any links it finds as links&#8230;so you can do something like this:<\/span><\/p>\n<pre class=\"brush: java; gutter: true\">String sText = &#039;Hey {005E0000005LZXh},did you see this blog post about this Org?&#039;+\r\n&#039;http:\/\/www.bridgefarmconsulting.com\/blog\/washington-post-article-about-polaris-project-and-sms\/&#039;+ &#039;\\n&#039; +\r\n&#039;Should we ask {005E0000001fhsc} about it tomorrow at the convention tomorrow?&#039;;\r\n\r\nConnectApi.FeedItem fi = ConnectApiHelper.postFeedItemWithMentions(null, &#039;me&#039;, sText);<\/pre>\n<p><a href=\"http:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.28.07-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-145\" alt=\"Screen Shot 2014-02-04 at 9.28.07 PM\" src=\"http:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.28.07-PM-300x95.png\" width=\"300\" height=\"95\" srcset=\"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.28.07-PM-300x95.png 300w, https:\/\/www.bridgefarmconsulting.com\/blog\/wp-content\/uploads\/2014\/02\/Screen-Shot-2014-02-04-at-9.28.07-PM.png 472w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Hopefully, these helper classes will make their way into the final product &#8211; once developers see how easy it is to work with the ConnectAPI, I think we will see an explosion of use cases. I will be doing my part to contribute to the process &#8211; my next post will be about how you can use the ConnectAPI\u00a0to share and post attachments and Chatter Files with records and users.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was, as my friend Becky will tell you, a bit of a Chatter sceptic when it was first released. But over time, I have begun to see huge potential for using Chatter as a notification and collaboration tool. So I was excited when they released the ConnectAPI &#8211; an Apex wrapper for the Chatter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,13],"tags":[],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-apex","category-connectapi"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":7,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bridgefarmconsulting.com\/blog\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}