+ Start a Discussion

dml in constructor

why Cant you do a dml in constructor?
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 


Mark it best answer if it helps you so it make proper solution for other :)
Sergio AlcocerSergio Alcocer


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.