+ Start a Discussion
sfdc007sfdc007 

Chatter trigger help needed

Hi,

I am having a trigger on feed item which will fetch the account and opportunity id along  with the @mention

i  have a class which will store the @mention , and a trigger which will store the account and opp id

my issue is the trigger is throwing record is read  only on( after insert ) , whereas the before insert is fetching the ids but the @mention gets grad out

my logic needs after insert trigger only

 
MY TRIGGER :

trigger FeedItemTrigger on FeedItem (after insert , after update) {
  
  if(PIPIntelHelper.skipChatterTriggers == true)
    return;
  
  List<FeedItem>feeditemupdatelist = new List<FeedItem>();
  set<id> parentAccountIds = new set<id>();
  
  for(FeedItem fdItem : trigger.new){
    String idStr = fdItem.Parentid;
    if(idStr.startsWith('006')){
       parentAccountIds.add(idStr);
    }
  }
    
  System.debug('parentAccountIds'+ parentAccountIds);
  Map<id,Opportunity> oppty  = new Map<id,Opportunity>([Select id, AccountId  from Opportunity where id in:parentAccountIds]);

  if(oppty.size()>0){
    for(FeedItem fdItem : trigger.new){
      String chatterBody = fdItem.Body;
      Opportunity parentopportunity = oppty.get(fdItem.Parentid);
      FeedItem f = new FeedItem(id = fdItem.id,Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id);
      System.debug('feeditemlist'+fdItem );

      feeditemupdatelist.add(f);
    }
    //if(feeditemupdatelist!=null){
    PIPIntelHelper.skipChatterTriggers = true;

   // update feeditemupdatelist;
         system.debug('feeditemlistupdate'+feeditemupdatelist);
   // }
    }
}
 I am able to store the @mention , but the account ids and opp ids are not getting populated , help me what do i need to change in my trigger


Thanks in Advance
 
RAM AnisettiRAM Anisetti
Hi sfdc007,

why dont you try this one....if you update anything on same object ,then you must use before triggers only...otherwise u will get read only error


trigger FeedItemTrigger on FeedItem before insert , before update) {
  
  if(PIPIntelHelper.skipChatterTriggers == true)
    return;
  
  List<FeedItem>feeditemupdatelist = new List<FeedItem>();
  set<id> parentAccountIds = new set<id>();
  
  for(FeedItem fdItem : trigger.new){
    String idStr = fdItem.Parentid;
    if(idStr.startsWith('006')){
       parentAccountIds.add(idStr);
    }
  }
    
  System.debug('parentAccountIds'+ parentAccountIds);
  Map<id,Opportunity> oppty  = new Map<id,Opportunity>([Select id, AccountId  from Opportunity where id in:parentAccountIds]);

  if(oppty.size()>0){
    for(FeedItem fdItem : trigger.new){
      String chatterBody = fdItem.Body;
      Opportunity parentopportunity = oppty.get(fdItem.Parentid);
      
      fdItem.Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;
      
      
      System.debug('feeditemlist'+fdItem );

      
    }
    
    }
}