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
vikas  malikvikas malik 

Can we do DML operation in constructor

Hi- I just want to delete the existing records in contructor itself. 
This code is working in action method but its not working in constructor, Please share me idea if any one have. 

List<Forecast_Revenue_Report__c> foreRevRepOutput = [SELECT Forecast_Amount__c, Forecast_Month__c, opp_id__c FROM Forecast_Revenue_Report__c WHERE opp_id__c = :this.oppId];
for(Forecast_Revenue_Report__c frDelete : foreRevRepOutput ) {
                    delete frDelete;

Best Answer chosen by vikas malik
Gaurav NirwalGaurav Nirwal


No you can't perform any DML in Constructor.

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