Code Monkey home page Code Monkey logo

connectapihelper's Introduction

ConnectApiHelper

ConnectApiHelper is an Apex class that makes it easier to do common operations with the classes in the ConnectApi namespace. It includes convenience methods to:

  • Post Chatter @-mentions with Apex code.
  • Post rich text, inline images, and record links with Apex code.
  • Take a feed item or comment body and return an input body that matches it (useful for either editing or re-posting).

Easier @-mentions

If you want to mention someone in a post that says: Hey there @Jane Doe, how are you?, you can do it in one line like this:

ConnectApi.FeedItem fi = (ConnectApi.FeedItem) ConnectApiHelper.postFeedItemWithMentions(Network.getNetworkId(), 'me', 'Hey there {005D00000015tjz}, how are you?');

... instead of this:

ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);

ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = '005D00000015tjz'; // The ID of the user to mention.
messageInput.messageSegments.add(mentionSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = ', how are you?';
messageInput.messageSegments.add(textSegment);

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;
input.subjectId = 'me';

ConnectApi.FeedItem fi = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), input);

Streamlined rich text, inline images and record links

If you want to add rich text, inline images, or record links in your post, one line will do it:

ConnectApi.FeedItem fi = (ConnectApi.FeedItem) ConnectApiHelper.postFeedItemWithRichText(Network.getNetworkId(),
'me', 'Have you seen this <b>gorgeous</b> view? {img:069x00000000D7m:View of the Space Needle from our office.} \nBy the way, please check {record:01t3E000002GCm9QAG}');

... instead of this:

ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Have you seen this ';
messageInput.messageSegments.add(textSegment);

ConnectApi.MarkupBeginSegmentInput markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
markupBeginSegment.markupType = ConnectApi.MarkupType.Bold;
messageInput.messageSegments.add(markupBeginSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'gorgeous';
messageInput.messageSegments.add(textSegment);

ConnectApi.MarkupEndSegmentInput markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
markupEndSegment.markupType = ConnectApi.MarkupType.Bold;
messageInput.messageSegments.add(markupEndSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = ' view? ';
messageInput.messageSegments.add(textSegment);

ConnectApi.InlineImageSegmentInput inlineImageSegment = new ConnectApi.InlineImageSegmentInput();
inlineImageSegment.fileId = '069x00000000D7m';
inlineImageSegment.altText = 'View of the Space Needle from our office.';
messageInput.messageSegments.add(inlineImageSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = ' \nBy the way, please check ';
messageInput.messageSegments.add(textSegment);

ConnectApi.EntityLinkSegmentInput entityLinkSegment = new ConnectApi.EntityLinkSegmentInput();
entityLinkSegment.entityId = '01t3E000002GCm9QAG';
messageInput.messageSegments.add(entityLinkSegment);

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;
input.subjectId = 'me';

ConnectApi.FeedItem fi = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), input);

Installation

Just copy the ConnectApiHelper and ConnectApiHelperTest classes to your Salesforce org. If you use Salesforce DX, you can clone this repo and develop using your preferred IDE, source control system, and scratch orgs.

Usage

For @-mentions, the methods to use are ConnectApiHelper.postFeedItemWithMentions and ConnectApiHelper.postCommentWithMentions and the parameters and formatting syntax are described in the method comments. To include rich text and inline images as well (starting in version 35.0), the method to use is ConnectApiHelper.postFeedItemWithRichText. You can also refer to the ConnectApiHelperTest class for more examples.

For creating input bodies from output bodies, the methods are ConnectApiHelper.createFeedItemInputFromBody and ConnectApiHelper.createCommentInputFromBody.

Salesforce API Versions

If you need to use an earlier version of the Salesforce API, the current ConnectApiHelper class may not compile, because the methods being called may not be available in the earlier version.

We've provided variants of ConnectApiHelper so that you can use it with earlier Salesforce API versions:

API Version ConnectApiHelper location Limitations
43.0 and higher default
36.0-42.0 v36-v42 Record links not supported
35.0 v35 Same as above
32.0-34.0 v32-v34 Same as above, and rich text not supported
31.0 and earlier v31AndEarlier Same as above

connectapihelper's People

Contributors

alouie-sfdc avatar bleyle avatar jaertgeerts avatar jamesward avatar nvuillam avatar pepke41 avatar stephenanders avatar svc-scm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

connectapihelper's Issues

ConnectApiHelperTest: "Context user does not have permission to create rich text in feed"

I installed the software from UnofficialSF.com and when running unit tests, got this error

Class.ConnectApi.ChatterFeeds.postFeedElement: line 1458, column 1
Class.ConnectApiHelperTest.testCreateInputFromBody: line 257, column 1
10:18:52.130 (194554713)|FATAL_ERROR|ConnectApi.ConnectApiException: Context user does not have permission to create rich text in feed

The ContextUser is UserInfo.getUserId() - which is me, user with System Admin profile. The test appears to be posting a bolded element.

test class is V47

Network.getNetworkId() returns null for the test context

What permission to I need to setup for the running user to be able to execute this unit test?

Error

I am getting the following error when trying to use the example post.

Illegal assignment from ConnectApi.FeedElement to ConnectApi.FeedItem

Code:
ConnectApi.FeedItem fi = ConnectApiHelper.postFeedItemWithMentions(Network.getNetworkId(), 'me', 'Hey there {005D00000015tjz}, how are you?');

My class and the helper class are on V36 and I have made the correction to the one line of the helper method for the V36 change that is listed in this issues area.

Make the class bulk-friendly

The class is not bulkified to use in triggers, would be super useful if it could be done so....

I am getting this while operating in a for loop
System.LimitException: Too many DML statements: 151

Thanks!

EntityLink segment not allowed. Contact your administrator to enable it.

When using the current version of the helper to create a post in a community, with a reference to a file, I get the error "EntityLink segment not allowed. Contact your administrator to enable it". This does not seem to happen when I create posts internally (eg: when the networkId is null). Has anyone else run into this, and if so, is it actually possible to "enable it" as the error message indicates, or is this a limitation of posting into a community?

getMessageSegmentInputs not working with "<p style="text-align: center;">Hello</p>"

We are using the lightning-input-rich-text(LWC) and when user using text alignment center|right that time <p style="text-align: center;">Hello</p> HTML is generating.

Post we are passing <p style="text-align: center;">Hello</p> HTML as param(inputText) to getMessageSegmentInputs method, but this piece of code String textSegment = inputText.substring(strPos, globalMatcher.start()); returning invalid HTML, hence system throwing error.

Output of textSegment post executing code:
<p style="text-align: center;">Hello

Error:
Error while parsing Rich Text Content: The markup in the document following the root element must be well-formed.

Could you please help us to resolve this issue.

@amerine @mars @vazexqi @jamesward @edmorley

Thank you,
Hemant

Remove dependency to SeeAllData

Using SeeAllData=True is against best practices documented https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

Since ConnectAPIs are not accessible in data silo'd tests, it would be great to see these examples reworked to use a mock/stubbing framework instead of requiring tests to see all data in an org.

Financial Force has published a robust example of a mock/stubbing framework but, it would be fantastic to have something for salesforce developers leveraging Connect APIs like this to be able to plug-and-play from this repo.

Use Variable on the Post

Hi! First of all thanks to post this to the community.

I have a doubt, not really a issue.

I want to know if I there is a way that I can use a variable inside the String that will pass to the postFeedItemWithRichText method.

Some like this:

ConnectApi.FeedItem fi = (ConnectApi.FeedItem) ConnectApiHelper.postFeedItemWithRichText (Network.getNetworkId(), 'me', 'Hey there {005D00000015tjz}, {some variable} ?');

Then I could change the post based on some previous criteria.

But nevertheless thanks again for the code!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.