+ Start a Discussion
sfdc007sfdc007 

Apex class for fetching opp id in chatter post

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;


            
             
              



        // This example appends text to the feed item.

        ConnectApi.TextSegmentInput textInput = new ConnectApi.TextSegmentInput();

        textInput.text =  chatterBody ;


        input.body.messageSegments.add(textInput);

    }
      }
              
     }

}


Kindly help me

Thanks
 
Sergio Mac-IntoshSergio Mac-Intosh
Use this to post in chatter:
 
FeedItem feedpost = new FeedItem();
feedpost.ParentId = 'a0hD000000E3jeI';  //ObjectID
feedpost.Body = 'Test Text Body' ;
feedpost.Type = 'LinkPost';
feedpost.LinkUrl = 'http://maps.google.com' ;
        
insert feedpost;

For other fieldtypes check: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_feedpost.htm
sfdc007sfdc007
Hi,

i tried the above code , but  its not fetching the opp id and acc id on posting a chatter feed


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);