function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShaneJensenShaneJensen 

FeedItem Trigger get CreatedBy before Insert

Hello,

I want to prevent users in my Partner Community from creating Chatter Posts.  I've created a trigger that checks the the list of Partner Users and will add an error if they are Partner Community user.

However, the Trigger.new on FeedItem doesn't contain the CreatedByID or InsertedByID before Insert, so the error never fires.  Here is the debug statement for a Trigger.new on FeedItem:
(FeedItem:{ParentId=005a000000AHEzLAAX, LikeCount=null, Type=TextPost, LastModifiedDate=null, LinkUrl=null, ContentSize=null, SystemModstamp=null, ContentData=null, RelatedRecordId=null, CommentCount=null, NetworkScope=null, ConnectionId=null, Body=test 2, Title=null, InsertedById=null, CreatedById=null, CreatedDate=null, IsDeleted=false, Visibility=null, Id=null, ContentDescription=null, ContentFileName=null, ContentType=null})

Is there anyway to get the ID of the user who created the post before insert?

Thanks,
Shane

Best Answer chosen by ShaneJensen
RitikaBhrgvRitikaBhrgv
Hi Shane,

Before triggers does not have access to values that are set by database on after insert(such as Created By, Created Date, Modified By, Modified Date etc.).

A work-around in this case is to utilize userinfo.getuserid() for fetching CreatedBy as salesforce automatically saves current logged-in user's id in CreatedBy field.

Hope that helps :)

All Answers

RitikaBhrgvRitikaBhrgv
Hi Shane,

Before triggers does not have access to values that are set by database on after insert(such as Created By, Created Date, Modified By, Modified Date etc.).

A work-around in this case is to utilize userinfo.getuserid() for fetching CreatedBy as salesforce automatically saves current logged-in user's id in CreatedBy field.

Hope that helps :)
This was selected as the best answer
ShaneJensenShaneJensen
Thank you that worked.