+ Start a Discussion
Mikhail IvanovMikhail Ivanov 

Trigger failed if Apex code assigns FeedItem.CreatedById for new FeedItem record

We have a piece of code which is working fine for Salesforce licenses but is failing when it runs under Site Guest User licence in client's instance as a part of managed package. This code is in Attachment object 'on insert' trigger.
FeedItem resumeFile = new FeedItem(
	ParentId = resumePoolChatterGroup.Id,
	Type = 'ContentPost',
	ContentData = resumeAttachment.Body,
	ContentFileName = resumeAttachment.Name,
	CreatedById = idToContactMap.get(resumeAttachment.ParentId).OwnerId
It failed on line:
CreatedById = idToContactMap.get(resumeAttachment.ParentId).OwnerId
and tells:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AVTRRT.Trigger_Attachment: execution of AfterInsert caused by: line 7, column 37: Dependent class is invalid and needs recompilation: AVTRRT.ChatterResume: line 70, column 66: Field is not writeable: FeedItem.CreatedById: []
In Salesforce documentation it tells:
If the logged-in user has the “Insert System Field Values for Chatter Feeds” user permission, the create field property is available on CreatedBy and CreatedDate system fields for this object. This allows the logged-in user to set these fields to the original post author and creation date upon migration instead of accepting the system field value when migrated, which would be the logged-in user and the date the migration was performed, respectively. The fields can't be updated after migration.
I tried to set “Insert System Field Values for Chatter Feeds” permission but didn't find such option.

Also, the issue is happening only for one of client's Force.com Site. For another one we don't have such issue.

Is it possible to make it work? 

Insert System Field Values is enabled by contacting Salesforce.com support. You have to contact Salesforce support to enable system field editing. On client organisation you have to contact Salesforce to make enable system fields editing.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)

CreatedBy, CreatedDate etc. are known as the Audit Field which you can not update, insert using DML in APEX. You need to contact salesforce support for enabling Audit Fields. Audit Fields are generally used while migrating history data from third party system to salesforce with legacy dates.

Mikhail IvanovMikhail Ivanov
Thank you for your replies.

But that code is working fine in our development instance as well as in the most of our clients instances. Even that client which is experiencing that issue has some other Force.com Sites where that code is working fine. The issue happens just for one specific Force.com Site.

For FeedItem object the field CreatedById is creatable if a profile has “Insert System Field Values for Chatter Feeds” user permission enabled.
Mikhail, I'm having the same issue.  Did you ever find a resolution?
I'm running into the same thing and while I don't have a solution, I have an idea that didn't work to save someone else time.

First, there is a discrete permission under System Permissions for Insert System Field Values for Chatter Feeds, you don't need to contact Salesforce support for that. That is different than the system audit fields mentioned about that you do need support to open up.

I tried creating a perm set with just that permission in it and nice luck, "Can't assign permission set Insert System Field Values for Chatter Feeds to user Job Application Site Guest User. The user license doesn't allow the permission: Insert System Field Values for Chatter Feeds"
Maximilian-Albin EkströmMaximilian-Albin Ekström

Sorry for replying on an old thread but hopefully this will help anyone reading this as this is still an issue.
My WORKAROUND was to create a Platform Event instead, and use a trigger on that event to create the FeedItem.

Could also be applied to when creating other objects that also creates FeedItems themselves.