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
Ritesh AswaneyRitesh Aswaney 

Post as a different user

Trying to insert a Chatter Feed record as a User which is not the executing user 

 

FeedItem fi = new FeedItem(Body = 'Hello World', ParentId = '<ID OF USER>');

insert fi;

 

But this inserts it as a FeedItem from the executing User to the User whose Id is plugged into the FeedItem.

 

FeedItems seem to be created in the executing Users Chatter Stream.Wondering what I'm missing.

Best Answer chosen by Admin (Salesforce Developers) 
cloudcodercloudcoder

You need to make sure the user you are using to the insert the post has the  "Insert System Field values for Chatter" attribute enabled on their profile.  From the Winter 11 release notes:

 

"This field is the ID of the user who added this object to the feed. For example, if a client application migrates multiple

posts and comments from another application into a feed, then InsertedById is set to the ID of the logged-in user.

If the logged-in user has the Insert System Field Values for Chatter Feeds user permission, the create field property is available on CreatedById 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."

 

Once you have that attribute enabled, you can set the CreatedById field, with the actual inserting users id being recorded in InsertedById.

 

The following code snippet will insert as the user as identified by other-user-id:

 

 

FeedItem post = new FeedItem();
post.ParentId = 'other-user-id'; 
post.createdById = 'other-user-id';
post.Body = 'hello there';
insert post;

 

 

All Answers

cloudcodercloudcoder

You need to make sure the user you are using to the insert the post has the  "Insert System Field values for Chatter" attribute enabled on their profile.  From the Winter 11 release notes:

 

"This field is the ID of the user who added this object to the feed. For example, if a client application migrates multiple

posts and comments from another application into a feed, then InsertedById is set to the ID of the logged-in user.

If the logged-in user has the Insert System Field Values for Chatter Feeds user permission, the create field property is available on CreatedById 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."

 

Once you have that attribute enabled, you can set the CreatedById field, with the actual inserting users id being recorded in InsertedById.

 

The following code snippet will insert as the user as identified by other-user-id:

 

 

FeedItem post = new FeedItem();
post.ParentId = 'other-user-id'; 
post.createdById = 'other-user-id';
post.Body = 'hello there';
insert post;

 

 

This was selected as the best answer
Ritesh AswaneyRitesh Aswaney

I wouldn't have figured that one ! Thanks Quinton :)