+ Start a Discussion
tmbarrytmbarry 

Change the Posting User of a Chatter Item

I need some help.  I created a trigger to create a chatter post everytime someone wins a opportunity - a nice little pat on the back if you will.  

User-added image

The problem is, that the chatter post comes from the person who won the opportunity, so it looks like they are patting themselves on the back.  I created a user called Sales Operations.  How do I make all these automated posts come from this Sales Operations user?

<pre>
trigger CreateChatterEntry on Opportunity (before Update) {

For(opportunity opp : Trigger.new){

// Check to see if the Oppty was previously Marked Closed Won
Opportunity beforeUpdate = System.Trigger.oldMap.get(opp.Id);
if(beforeUpdate.IsWon != opp.IsWon){ 

    String ownerIds = opp.Oppty_Owner_Name__c;
    String oppName = opp.name;
   
    Set<ID> ownerId = new Set<ID>();
    ownerId.add(opp.ownerId);
   
    Map<ID,User> userMap = new Map<ID,User>([SELECT ID, Name, FirstName FROM User WHERE ID In : ownerid]); //This is our user map
    User uName = userMap.get(opp.ownerId);
   
    // Get a list of All the Current Chatter Group Names
    List<CollaborationGroup> Grps = [Select Name, Id FROM CollaborationGroup];
    Map<string, String> GrpName = new Map<string, string>{};
    for(CollaborationGroup gn: Grps)
        Grpname.put(gn.name,gn.id);
   
    // If the Oppty is marked Closed Won, Create Chatter entry.
    If(opp.iswon ==true){
        FeedItem fitem = new FeedItem ();
         fitem.type = 'LinkPost';
                fitem.ParentId = grpName.get('Salesforce.com Users');  //Select What Chatter group to post message in.
                fitem.LinkUrl = '/' + opp.id; //This is the url to take the user to the activity
                fitem.Title = oppName + ' Opportunity';  //This is the title that displays for the LinkUrl
                //fitem.Body = ('Congratulations to ' + uName.name + ' on winning ' + oppname + '!  Nice Job '+ uName.firstname+'.');
                fitem.Body = ('Congratulations to ' + uName.name + ' on winning ' + oppname + '!');
               
                                   
        Insert fitem;
    } }  
}}
</pre>
Sonam_SFDCSonam_SFDC
Hi,

I did some reserch around this and turns out that one can update the createdByID through trigger such that the feeditem is posted by another user instead of the one who triggers the trigger.

Sample code that worked:

trigger create on Account (after update) {
FeedItem post = new FeedItem();
post.ParentId = '00190000005yXXX';
post.CreatedById ='00590000000iXXX';
post.Body = 'from another user';
insert post;

The thing to note over here is the permision required by the user(who triggers the trigger) is the following on the profile:“Insert System Field Values for Chatter Feeds”

read more: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_feeditem.htm