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
sfdc007sfdc007 

Chatter help needed

Hi,

I am having a apex class which i am trying to post the opp id and account id from opportunity along with the text on opportunity object alone , this fires on posting a content i n chatter


i have written the apex class , but for some reasong its not firing

help me if i am doing anything wrong here
public class EditFeedItemHelper {

  /**

  * Call this method from an after insert FeedItem trigger.

  * It updates the feed items passed in and preserves @mentions.

*/

  public static void edit(FeedItem[] feedItems) {

    String communityId = Network.getNetworkId();

    List<String> feedItemIds = new List<String>();

    for (FeedItem f : feedItems) {

        feedItemIds.add(f.id);

    }

        // Get all feed items passed into the trigger (Step #1).

    ConnectApi.BatchResult[] results = ConnectApi.ChatterFeeds.getFeedElementBatch(communityId, feedItemIds);

    for (ConnectApi.BatchResult result : results) {

        if (result.isSuccess()) {

            Object theResult = result.getResult();

            if (theResult instanceof ConnectApi.FeedItem) {

                ConnectApi.FeedItem item = (ConnectApi.FeedItem) theResult;

                // Convert message segments into input segments (Step #2a).

                ConnectApi.FeedItemInput input = ConnectApiHelper.createFeedItemInputFromBody(item.body);

                // Modify the input segments as you see fit (Step #2b).

                modifyInput(input);

                // Update the feed item (Step #2c).

                // We need to update one feed item at a time because there isn't a batch update method yet.

                ConnectApi.ChatterFeeds.updateFeedElement(communityId, item.id, input);

            }

            else {

                // Do nothing. Posting other feed element types isn't supported.

            }

        }

        else {

                System.debug('Failure in batch feed element retrieval: ' + result.getErrorMessage());

        }

    }

}

    /**

    * Update the feed item input here!

    */

    public static void modifyInput(ConnectApi.FeedItemInput input) {

          set<id> parentAccountIds = new set<id>();
          List<FeedItem> fd = new List<FeedItem>();
              for(FeedItem fdItem : fd){
          String idStr = fdItem.Parentid;
            if(idStr.startsWith('006')){
           parentAccountIds.add(idStr);
        }
                      }
        
           Map<id,Opportunity> oppty  = new Map<id,Opportunity>([Select id, AccountId  from Opportunity where id in:parentAccountIds]);
             if(oppty.size()>0){
             for(FeedItem fdItem : fd){
          Opportunity parentopportunity = oppty.get(fdItem.Parentid);
                  String chatterBody = fdItem.Body;
           fdItem.Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;

          
      FeedItem feedpost = new FeedItem();
        feedpost.ParentId = 'a0hD000000E3jeI';  //ObjectID
        feedpost.Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;
       // feedpost.Type = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;
        //feedpost.LinkUrl = 'http://maps.google.com' ;
        
        insert feedpost;
            
             
              



        // This example appends text to the feed item.

        ConnectApi.TextSegmentInput textInput = new ConnectApi.TextSegmentInput();
        textInput.text =  feedpost.Body;
        input.body.messageSegments.add(textInput);

    }
      }
              
     }

}

Kindly help me pls

Thanks n Advance
Durga PaavanDurga Paavan
Hi sfdc007,

Can you let us know where this class being used/called? If it is being used in Trigger, Can you let us know in which object trigger is being used?
Durga PaavanDurga Paavan
As per documentation, Trigger on FeedItem will not work.

Please find below link for the same.
https://help.salesforce.com/articleView?id=000339961&type=1&mode=1

Cheers,
Durgapaavan