Check the below options to fix the “System.LimitException: Too many DML statements: 1” exception in Salesforce.
1. Visualforce Page:
In <apex:page> tag, if attribute “readonly” is true and if we
try to execute DML statements(Insert, Update, Delete), then it shows Too
many DML statements: 1 out of 0 Error in Salesforce.
try to execute DML statements(Insert, Update, Delete), then it shows Too
many DML statements: 1 out of 0 Error in Salesforce.
So, in order to rectify this error, set readOnly = “false” or remove this attribute in <apex:page> tag.
Sample code:
<apex:page controller=”Sample” sidebar=”false” readOnly=”false”>
When you set readOnly attribute of page to true then it does not allow any DML statements.
2. Aura Component:
Do not use @AuraEnabled(cacheable=true). Use @AuraEnabled. Remove (cacheable=true).
3. Lightning Web Component:
a. Do not use @AuraEnabled(cacheable=true). Use @AuraEnabled. Remove (cacheable=true).
b. use async await while calling method that does DML Operations.
Sample code for LWC: https://infallibletechie.com/2022/02/how-to-do-dml-when-using-lightning-web.html
Thank you Guys!
WOW! Thank you very much. This helped me out
Helped me too, Thank you so much buddy.
How to figure out this issue in LWC
There is currently no document on LWC. For Aura, it was 4 MB – https://releasenotes.docs.salesforce.com/en-us/winter18/release-notes/rn_lc_client_data_limit.htm
I had this issue with LWC also. I fixed it by removing the "cacheable=true" attribute from the apex method.