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
Baktash H.Baktash H. 

DML currently not allowed

Hello,

i think I have this time sth. interesting.

The error message is in the Subject and it results because of the following.

I have a list, lets call it SuperList. And in a function getSuperList I put it in a datatable.

But the function should do sth. else too.

This:

       List<Obj__c> SuperRecords = new List<Obj__c> ();

        for(Obj__c x : SuperList){
            if(x.wasSuper__c == false) SuperRecords.add(i);
        }

update SuperRecords;

 

wasSuper is a checkbox. And i want that every Record which has ever been in the SuperList gets the checkbox wasSuper to true. But i get that annoying Error Message.

 

I hope you can help me.

 

Best Answer chosen by Admin (Salesforce Developers) 
goabhigogoabhigo

Are you calling getListName() inside constructor? DML operations are not allowed inside constructor. If you can post the code it will be useful for us to solve. In addition, try moving your DML operations to some other method, and call it in VF through <apex:page..... action="theNewmethod" >

All Answers

spraetzspraetz

It seems like you're attempting to do this in the page constructor.

 

You cannot do inserts/updates/deletes/undeletes/anything else that does a DML operation in the page constructor.

 

I think what you can instead do is define an action in the <apex:page action="{!YourActionHere}"> attribute and do your logic there.  You can do the stuff you wanted to do in the constructor there.

Baktash H.Baktash H.

It is not in the page constructor. It is all in a function getListName() which i use tu return a list to a datatable.

Do you have an other solution?

goabhigogoabhigo

Are you calling getListName() inside constructor? DML operations are not allowed inside constructor. If you can post the code it will be useful for us to solve. In addition, try moving your DML operations to some other method, and call it in VF through <apex:page..... action="theNewmethod" >

This was selected as the best answer
Baktash H.Baktash H.

No, i am not calling getListName() in the constructor. The only time it is used, is and called is on my VF page in the value attribute of the apex:datatable.

In the action is already another function, can i use 2 of them?

Baktash H.Baktash H.

I solved it with putting it in the action method...maybe it is not allowed to put dml actions in get-methods.

Prashant BhardwajPrashant Bhardwaj
Why do we not allowed DML operations in constructor ?