1

I want my code to be reviewed and please point me the scenarios where it can cause exception.

I have a trigger which modifies the chatter feed on Position__c object,


trigger FeedItemBodyModify on FeedItem (after insert) {

    List fiIds = new List(); 
    for(FeedItem fi : Trigger.new)
    {
        if('Position__c' == String.valueOf(fi.ParentId.getSObjectType())){
            fiIds.add(fi.Id);
        }

    }

    if(null != fiIds && fiIds.size() > 0){
        String commID = Network.getNetworkId();
        ConnectApi.BatchResult[] feeds = ConnectApi.ChatterFeeds.getFeedElementBatch(commID, fiIds);

        for (ConnectApi.BatchResult result : feeds) {
            if (result.isSuccess()) {
                Object theResult = result.getResult();
                if (theResult instanceof ConnectApi.FeedItem) {
                    ConnectApi.FeedItem item = (ConnectApi.FeedItem) theResult;   
                    ConnectApi.FeedItemInput input = ConnectApiHelper.createFeedItemInputFromBody(item.body);

                    ConnectApi.TextSegmentInput textInput = new ConnectApi.TextSegmentInput();
                    textInput.text = 'Disclaimer: Modified in a trigger.';

                    ConnectApi.TextSegmentInput lineTextInput = new ConnectApi.TextSegmentInput();
                    lineTextInput.text = '---';

                    ConnectApi.TextSegmentInput blankTextInput = new ConnectApi.TextSegmentInput();
                    blankTextInput.text = ' ';

                    ConnectApi.MarkupBeginSegmentInput markupBeginInput = new ConnectApi.MarkupBeginSegmentInput();
                    markupBeginInput.markupType = ConnectApi.MarkupType.Paragraph;

                    ConnectApi.MarkupEndSegmentInput markupEndInput = new ConnectApi.MarkupEndSegmentInput();
                    markupEndInput.markupType = ConnectApi.MarkupType.Paragraph;


                    input.body.messageSegments.add(markupBeginInput);
                    input.body.messageSegments.add(blankTextInput);                     
                    input.body.messageSegments.add(markupEndInput);
                    input.body.messageSegments.add(markupBeginInput);
                    input.body.messageSegments.add(lineTextInput);                     
                    input.body.messageSegments.add(markupEndInput);
                    input.body.messageSegments.add(textInput);

                    System.debug(input.body.messageSegments);

                    ConnectApi.ChatterFeeds.updateFeedElement(commID, item.id, input);
                }
            }
        }
    }

}

I referred https://developer.salesforce.com/blogs/engineering/2015/06/preserving-mentions-triggers.html for implementing this.

Q1. What is the exact limit for ConnectApi chatter modification?

Q2. I saw that limits are 'per-user per-hour'. So how to enable 'per-user per-hour' limit or it is by default?

Q3. How should I handle if code runs into limit exception? Because here I am trying to modify a feed posted by user and I don't think it would be good to mark error to the feed record just because we are unable to update the feed with some extra info.

Q4. If I am integrated with a third party app which on bulk upload generates chatter feed for each object, how should I handle it in trigger without hitting the limits?

  • 1
    I'm sure someone will answer your question. Looking at your code I would say replace 'Position__c' == String.valueOf(fi.ParentId.getSObjectType()) with fi.parentId.getSobjectType() == position__c.sObjectType – Jarvis Feb 23 '16 at 8:28
0

Q1: The exact limits aren't specified, but they're described generally on the Understanding Limits for ConnectApi Classes doc page.

Q2: The per-user. per-app, per-hour rate limits are the default. You can't opt out of them.

Q3. You could catch the ConnectApi.RateLimitException and allow the trigger to proceed so that at least the FeedItem gets inserted.

Q4. This is going to be tricky. Currently the ConnectApi.ChatterFeeds.updateFeedElement() method needs to be called for each feed item individually because there's no batch method available (it would be nice to have an updateFeedElementBatch(), similar to the existing getFeedElementBatch() and postFeedElementBatch() methods). You may need to throttle the bulk upload if that's an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.