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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0