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
EnryEnry 

Created by user in chatter api connect

Hi. I'm using the chatter connect to create a chatter post.

Ref: Connect

 

All works fine but i want to specify the user that creates the chatter post.

In the previous version of my code, i don't use the connect and i have the following instruction:

 testpost.CreatedById=user.id; 

 

Do you know how i can do this in the connect api chatter?

My class:

Global class HappyBirthdayRandomPilot implements Schedulable{
//FOR TEST METHOD
   public static String CRON_EXP = '0 0 0 3 9 ? 2022';
   
  Global void execute(SchedulableContext sc) 
    {

            
           // SEARCH BY STATIC RESOURCE NAME
           List<StaticResource> Pictures=[Select Name, ContentType, Body From StaticResource where Name like 'PicturehappyBirthday%'];
           // IS THE USER BIRTHDAY?              
           List<user> lstu=[SELECT id,name,date_of_birth__c FROM user WHERE  CALENDAR_MONTH(date_of_birth__c)=:date.today().month() AND  DAY_IN_MONTH(date_of_birth__c)=:date.today().day()];
           CollaborationGroup group=[Select id,name from CollaborationGroup where Name like 'name%' limit 1];
                     
           //String communityId = null;
             
             
              for(User u:lstu)
              {
                //GENERATE A RANDOM NUMBER [0-5] TO SELECT THE PICTURE
                   Integer choice=math.mod(Integer.valueof(Math.random()*100),6);
                   Blob Decodedbody=Pictures[choice].body;

                // CREATE A FEED_ITEM_TEXT 
                    ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
                    messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
                    ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
                    textSegment.text = 'Happy birthday to ';
                    messageInput.messageSegments.add(textSegment);
                    ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
                    mentionSegment.id = u.id;
                    messageInput.messageSegments.add(mentionSegment);
                    textSegment = new ConnectApi.TextSegmentInput();
                    textSegment.text = '!';
                    messageInput.messageSegments.add(textSegment);
                    ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
                    input.body = messageInput;

                // POST FILE_FEED_ITEM
                    ConnectApi.NewFileAttachmentInput fileIn = new ConnectApi.NewFileAttachmentInput();
                    fileIn.title ='Wishes!'; 
                    fileIn.description = '';
                    Input.attachment = fileIn;
                    string contentType='image/jpeg';
                    ConnectApi.BinaryInput feedBinary = new ConnectApi.BinaryInput(Decodedbody,contentType, 'Wishes!.jpg');
                    
                // PARAMETERS(communityId, feedType, subjectId, input,filebody);
                    ConnectApi.ChatterFeeds.postFeedItem(null,ConnectApi.FeedType.Record,group.id,Input, feedBinary);
                
              }
        }
    }

 

Thanks in advantage for any advice.

BR.

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
alouie_sfdcalouie_sfdc
Currently, both Chatter in Apex and the Chatter REST API do not support impersonation like the SObject API does. You can only post feed items as the current, logged-in user.

This is documented in "Differences Between ConnectApi Classes and Other Apex Classes":
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_connectapi_differences.htm

All Answers

alouie_sfdcalouie_sfdc
Currently, both Chatter in Apex and the Chatter REST API do not support impersonation like the SObject API does. You can only post feed items as the current, logged-in user.

This is documented in "Differences Between ConnectApi Classes and Other Apex Classes":
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_connectapi_differences.htm
This was selected as the best answer
Sure@DreamSure@Dream
Hi Enry/Alouie,


I want to get the id and nickname of the user who created the feeditem / comment using ConnectApi.
Could you please help me with this?


Thanks.
jody_blyjody_bly
ConnectApi.ChatterFeeds.getFeedItemsFromFeed returns a ConnectApi.FeedItemPage object. Its items property contains a list of ConnectApi.FeedItem objects. Use the ConnectApi.FeedItem.actor property to get the id and name of the actor who created the feed item. 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ConnectAPI_ChatterFeeds_static_methods.htm#apex_ConnectAPI_ChatterFeeds_getFeedItemsFromFeed_2 (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_ConnectAPI_ChatterFeeds_static_methods.htm#apex_ConnectAPI_ChatterFeeds_getFeedItemsFromFeed_2" target="_blank)