+ Start a Discussion
sfdc007sfdc007 

Record is Read only error trigger help

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

when i click on post , i am getting

Apex trigger FeedItemTrigger caused an unexpected exception, contact your administrator: FeedItemTrigger: execution of AfterInsert

caused by: System.FinalException: Record is read-only: Trigger.FeedItemTrigger: line 31, column 1


Line 31 :

        fdItem.Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;


 

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){
     system.debug('feeditemlist'+fdItem );
        Opportunity parentopportunity = oppty.get(fdItem.Parentid);

        String chatterBody = fdItem.Body;

        fdItem.Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;
          feeditemupdatelist.add(fdItem);
                
    }
    
    
    
    //if(feeditemupdatelist!=null){
    PIPIntelHelper.skipChatterTriggers = true;

   // update feeditemupdatelist;
         system.debug('feeditemlistupdate'+feeditemupdatelist);
   // }
    

    }
    
  
 

}
 

Kindly help me whats the issue here

Thanks



help me how to fix the issue

Fahad-AkhtarFahad-Akhtar
Hi,
I belive the issue is that you are trying to udpate FeedItem record in after context, if you cant change your trigger to before, you should query all the record or set the id and update your records not trigger.new context variable because trigger.new will be readonly in after.

This will help: https://hisrinu.wordpress.com/2011/05/17/difference-between-before-trigger-and-after-trigger/
Thanks,
Fahad Akhtar
 
sfdc007sfdc007
can you please help me with the changes in my code please , i am really struck  here

Thanks
Fahad-AkhtarFahad-Akhtar
Hi,
Your code should look something like this, its not tested so expect some issues in it
 
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;
      FeedItem f = new FeedItem(id = fdItem.id,Body = chatterBody + '\n Account Id is :'+ parentopportunity.AccountId + ' \n Opportunity Id :'+parentopportunity.Id;);
      System.debug('feeditemlist'+fdItem );
      Opportunity parentopportunity = oppty.get(fdItem.Parentid);
      feeditemupdatelist.add(f);
    }
    //if(feeditemupdatelist!=null){
    PIPIntelHelper.skipChatterTriggers = true;

   // update feeditemupdatelist;
         system.debug('feeditemlistupdate'+feeditemupdatelist);
   // }
    }
}

Thanks,
Fahad Akhtar
sfdc007sfdc007
Hi Fahad

I tried the above code , but its fetching only the content , but not fetching the account id and opportunity id

 
MY TRIGGER CODE :

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

Kindly help me pls