You need to sign in to do that
Don't have an account?
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
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
Thanks
Your code should look something like this, its not tested so expect some issues in it
Thanks,
Fahad Akhtar
I tried the above code , but its fetching only the content , but not fetching the account id and opportunity id
Kindly help me pls