2

I want insert in a chatter post an image that i have uploaded. How can i reference this resource in my code? I have different images loaded as static resources. I have a chatter's post on profile of the user but the picture isn't loaded.

Most recent code:

   List<FeedItem> posts = new List<FeedItem>();
   List<user> lst=[Select id,name from user where date_of_birth__c =today];

    // search by static resource name
   List <StaticResource> resourceList = [SELECT Name, Body
    FROM StaticResource WHERE Name like '%Filehappy%'ORDER BY Name ASC];
      for(User u:lst)
      {
       //generate a random number [0-5] for resource's name. 
            Integer index=math.mod(Integer.valueof(Math.random()*100),6);
            Blob decodedbody=resourceList[index].body;
            FeedItem post = new FeedItem();
            post.ParentId = u.id;
            post.CreatedById='005E0000002cjXv';
            post.Body = 'Happy birthday to '+u.name+'!';
            post.ContentData = decodedbody;
            post.ContentFileName = 'Wishes.jpg!';
            posts.add(post);
      }
         insert posts;
}

Thank you very much! KR

  • 3
    You are doing one of the big sins in salesforce development here, doing a DML statement in a loop (insert here), instead make a List<FeedItem> posts = new List<FeedItem>(); and in your loop add your new FeedItem to the list. posts.add(post); outside of your loop you can insert your list by doing insert posts; – pjcarly Apr 29 '13 at 9:39
  • @Enry: When updating your question with more details (which is good!) please leave the importants bits in there so other users can still make sense of your full question. – Samuel De Rycke May 7 '13 at 9:48
4

enter image description here

Blob body=[Select Name, ContentType, Body From StaticResource where name='abc'].body;

 FeedItem post = new FeedItem();
         post.ParentId = u.id;
         post.CreatedById='005E0000003XWRs';
         post.Body ='Happy birthday '+u.name;
         post.ContentData = body;
         post.ContentFileName = 'sample.png';
         insert post;

The above code i fetch data from static resource and use as a ContentData directly

http://wiki.developerforce.com/page/Chatter_Code_Recipes Refer these recepies .

  • 1
    tHANK you very helpful..this type of encoding works for all type of static resources? – Enry Apr 29 '13 at 11:00
  • Yes all file formats it will work.Content type is stored in base64 encoded in static resource. – Mohith Shrivastava Apr 29 '13 at 11:23
  • I get an error with code above: Illegal assignment from Blob to String at this line of code: String body=[Select Name, ContentType, Body From StaticResource where name='abc'].body; – Enry Apr 29 '13 at 12:56
  • I have updated the answer .One thing to look for is wherever you find blob declare a blob type variable and wherever you find conversion use String.ValueOf() function to convert – Mohith Shrivastava Apr 29 '13 at 13:11
  • Hi,please can you review my update code, i get an exception...thank you very much! – Enry May 4 '13 at 20:53
1

In Summer 13, you can use Chatter in Apex (classes in the ConnectApi namespace) to do this as well:

String communityId = null;
String subjectId = 'me';
String contentId = '069D00000001hOw';

ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();

// Add feed item body
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();

textSegment.text = 'Feed Item Text Body';
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
messageInput.messageSegments.add(textSegment);

feedItemInput.body = messageInput;

// Add existing content attachment
ConnectApi.ContentAttachmentInput attachmentInput = new ConnectApi.ContentAttachmentInput();

attachmentInput.contentDocumentId = contentId;
feedItemInput.attachment = attachmentInput;

// Post feed item
ConnectApi.FeedItem feedItem = ConnectApi.ChatterFeeds.postFeedItem(communityId, ConnectApi.FeedType.UserProfile, subjectId, feedItemInput, null);

Apex Developer's Guide: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_connect_api.htm

  • I like your answer. But what should the VF page contain. I want to upload a file in VF page chatter post (im building the whole chatter page in VF) and then run this code. I'm concerned on the UI of the posting thing – sfdcFanBoy Apr 20 '15 at 5:21

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.