5

i'm using the chatter connect to create a chatter post.

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];

              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.

7

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.