Too Many DML Statements 1 in LWC Apex Controller

DESCRIPTION

This error you may be facing while making a simple call to apex method from LWC and any record is being created, updated or deleted.

SOLUTION

If the apex method you are invoking from LWC is annotated as

cacheable=true
cacheable=true then you can not make any DML in that context. So, in order to avoid this issue, please remove this cacheable=true.

Previously:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@AuraEnabled(cacheable=true)
public static String updateStatus(Id caseId) {
Case c = new Case(Id= caseId);
c.Status = 'New';
update c;
}
@AuraEnabled(cacheable=true) public static String updateStatus(Id caseId) { Case c = new Case(Id= caseId); c.Status = 'New'; update c; }
@AuraEnabled(cacheable=true)
public static String updateStatus(Id caseId) {
    Case c = new Case(Id= caseId);
    c.Status = 'New';
    update c;
}

Now

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@AuraEnabled
public static String updateStatus(Id caseId) {
Case c = new Case(Id= caseId);
c.Status = 'New';
update c;
}
@AuraEnabled public static String updateStatus(Id caseId) { Case c = new Case(Id= caseId); c.Status = 'New'; update c; }
@AuraEnabled
public static String updateStatus(Id caseId) {
    Case c = new Case(Id= caseId);
    c.Status = 'New';
    update c;
}