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
sfdcjoeysfdcjoey 

dml in constructor

why Cant you do a dml in constructor?
sfdcMonkey.comsfdcMonkey.com
hi sfdcjoey
DML operation is not allowed in the constructor of Apex class.
Whenever we go for a DML operation in constructor it resticts us in Salesforce.Only DDL is allowed. its salesforce restiction
but If you want to Perform DML then you have to write a function in class and call this functon by action attribute of <apex:page action="YOUR Funciton Name">

There are some blog 

http://salesforceworld4u.blogspot.in/2014/08/how-to-do-dml-operation-when-page-loads.html

http://www.mindfiresolutions.com/DML-Operation-Salesforce-ApexA-Trick-To-Avoid-Problem-Of-DML-At-Constructor-1809.php
Thanks
Mark it best answer if it helps you so it make proper solution for other :)
Sergio AlcocerSergio Alcocer

Easy, 

Imagine that your users are loged in on salesforce.
You have a visualforce like https://c.eu0.visual.force.com/apex/createTask
If any of your users get into a external website that has something like 
 

<img src="https://c.eu0.visual.force.com/apex/createTask" style="opacity:0"/>
It will actually trigger your dmls without the user even knowing that, which is risky business

Kind regards.