Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

So there's times you can't DML (getters, constructors, components without allow DML, etc).

Can we use chatter in apex, or is that also considered inserting records?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

I did a quick test using the ConnectAPI, and looks like it is treated as DML

This code on a page produces the following error as expected:

System.LimitException: DML currently not allowed

public with sharing class ChatterDML {

private final Account account;

    public ChatterDML (ApexPages.StandardController stdController) {
        this.account = (Account)stdController.getRecord();
        account.Name = 'Test Chatter Update';
        try{
        update account;
        //ConnectAPI.FeedItem fi = ConnectAPI.ChatterFeeds.PostFeedItem(null, ConnectApi.FeedType.Record, account.Id, 'New Name is ' + account.Name);
       }
        catch (Exception Ex) {
       }
   }
}

But this code calling the ConnectAPI also generates a DML error:

System.LimitException: ConnectApi:DML currently not allowed

I assume the ConnectAPI label is appended to the error because it runs in the ConnectAPI namespace

public with sharing class ChatterDML {

    private final Account account;

        public ChatterDML (ApexPages.StandardController stdController) {
            this.account = (Account)stdController.getRecord();
            account.Name = 'Test Chatter Update';
            try{
            ConnectAPI.FeedItem fi = ConnectAPI.ChatterFeeds.PostFeedItem(null, ConnectApi.FeedType.Record, account.Id, 'New Name is ' + account.Name);
           }
            catch (Exception Ex) {
           }
       }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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