I've a VF page which have readOnly='true' attribute and I am trying to implement a button that when clicked will make a chatter post with some Blob content as element attachment using ConnectApi namespace classes in Apex.

I'm receiving this error:

Too many DML statements: 1 

My code looks something like this:

<apex:page readOnly="true" controller="MyTestPageController">
This is a test page.<br />
Post Content of some Visualforce Page as content into a Chatter Feed.<br />
<br />
<apex:commandButton value=" Post Content to Chatter " action="{!postToChatter}" />

The Code Apex class looks like this:

public with sharing class MyTestPageController {
    public MyTestPageController() { }

    public void postToChatter() {
        PageReference p                                         = Page.MyTestVFPage;

        ConnectApi.FeedItemInput feedItemInput                  = new ConnectApi.FeedItemInput();
        feedItemInput.subjectId                                 = 'me';

        ConnectApi.ContentCapabilityInput contentInput          = new ConnectApi.ContentCapabilityInput();
        contentInput.title                                      = 'My File';

        ConnectApi.FeedElementCapabilitiesInput capabilities    = new ConnectApi.FeedElementCapabilitiesInput();
        capabilities.content                                    = contentInput;

        feedItemInput.capabilities                              = capabilities;

        Blob myBlob                                             = p.getContent();
        ConnectApi.BinaryInput binInput                         = new ConnectApi.BinaryInput(myBlob, 'application/x-excel', 'Data_File_'+Datetime.now().getTime());

        ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput, binInput);

Can anyone suggest if I can make ConnectApi do chatter post without me having to remove readOnly="true" VF Page attribute?

  • 1
    what is the if you remove the readOnly="true"? – Ratan Paul Nov 30 '15 at 16:54
  • It works in that case. I've corrected the error msg received in my post above, when readOnly=true and this is something that I need in my app. – VarunC Nov 30 '15 at 17:09
  • if readOnly="false" works for you then why you are not using? – Ratan Paul Nov 30 '15 at 17:15
  • because as I said, that would break an existing functionality. readOnly attribute allows for higher records fetch limit. – VarunC Nov 30 '15 at 17:28
  • can you use @future methods to post the feedElement? – cropredy Nov 30 '15 at 18:05

When you set readOnly attribute of page to true then it doesnot allow any DML statements.

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.