+ Start a Discussion
amitashtekaramitashtekar 

System.LimitException: Too many DML statements: 1

Hi All,

I am facing this issue.

Anyone have any idea how to solve this.

public void SaveChanges()
{
List<Account> AccountList = [Select Id, Name,(Select Id From Contacts) From Account where Id=:this.AccountId];


if(AccountList.size() > 0)
{
Account UpdateAccount = AccountList[0];
UpdateAccount.Name = this.Name;
UpdateAccount.AccountNumber = this.AccountNumber;
update UpdateAccount;

if(AccountList[0].Contacts.size() > 0)
{
Contact UserContact = AccountList[0].Contacts[0];
UserContact.FirstName = this.FirstName;
UserContact.LastName = this.LastName;
UserContact.Email = this.PersonalEmailId;
update UserContact;
}
else
{
Contact UserContact = new CanGeneral__c(AccountId = AccountList[0].Id,FirstName = this.FirstName,
LastName = this.LastName, Email = this.PersonalEmailId);
insert UserContact;
}
}
}

Best Answer chosen by Admin (Salesforce Developers) 
amitashtekaramitashtekar

Hi Devendra,

 

If you are facing the same issue then please check whether you have made readOnly attribute of apex:page tag as true.

If so then just make it false;

 

When you set readOnly attribute of page to true then it doesnot allow any DML statements.

All Answers

Damien_Damien_

I would say report this to Salesforce.... limits should be up to either 20 or 100, not 0.

Devendra@SFDCDevendra@SFDC

Hi,

 

I am facing the same problem. I am getting the same error message.

 

caused by: System.LimitException: Too many DML statements: 1

 

What would be the way to solve this problem?

 

Thanks,

Devendra

amitashtekaramitashtekar

Hi Devendra,

 

If you are facing the same issue then please check whether you have made readOnly attribute of apex:page tag as true.

If so then just make it false;

 

When you set readOnly attribute of page to true then it doesnot allow any DML statements.

This was selected as the best answer
Devendra@SFDCDevendra@SFDC

 

Thank you so much!!

 

readOnly = True causing the issue.

 

Now changed it to readOnly =  False and it worked.

 

Thyanks,

Devendra

amitashtekaramitashtekar

You are welcome.

 

Actually I forgot to add solution for this.

 

 

 

 

Amit N. Bhandarkar (Dev Org)Amit N. Bhandarkar (Dev Org)
Thanks Amit. . . . Its really helpfull. . . -:)
Raúl Germán Muñoz Torres 9Raúl Germán Muñoz Torres 9
Hi, Goog morning 

I have a problem with my code, it marks me the same error, Please Help...

@AuraEnabled(cacheable=true)
    public static String searchDB(String objectName, String fld_API_Text, String fld_API_Val, 
                                  Integer lim,String fld_API_Search,String searchText ){
                                      
                                      searchText='\'%' + String.escapeSingleQuotes(searchText.trim()) + '%\'';
                                      
                                      
                                      String query = 'SELECT '+fld_API_Text+' ,'+fld_API_Val+
                                          ' FROM '+objectName+
                                          ' WHERE '+fld_API_Search+' LIKE '+searchText+ 
                                          ' LIMIT '+lim;
                                      
                                      List<sObject> sobjList = Database.query(query);
                                      List<ResultWrapper> lstRet = new List<ResultWrapper>();
                                      
                                      for(SObject s : sobjList){
                                          ResultWrapper obj = new ResultWrapper();
                                          obj.objName = objectName;
                                          obj.text = String.valueOf(s.get(fld_API_Text)) ;
                                          obj.val = String.valueOf(s.get(fld_API_Val))  ;
                                          lstRet.add(obj);
                                      } 
                                      return JSON.serialize(lstRet) ;
                                  }
    
    public class ResultWrapper{
        public String objName {get;set;}
        public String text{get;set;}
        public String val{get;set;}
    }
    @AuraEnabled(cacheable=false)
    public static void relacion(String campselected, String visita) {
        Map <string, object> datoscamp;
        string idcamp, namecamp;
        datoscamp = (Map<String, Object>)JSON.deserializeUntyped(String.valueof(campselected));  
        namecamp = (String) datoscamp.get('text');
        idcamp = (String) datoscamp.get('val');
        list <cond__Account_Campaign__c>  camp = [SELECT id, name, MX_PYME_VisitaRel__c FROM cond__Account_Campaign__c WHERE id=: idcamp];
        camp[0].MX_PYME_VisitaRel__c = visita;
        update camp[0];
        system.debug('insertcamp' + camp);
    }   
    
}
David Roberts 4David Roberts 4
Setting to false also worked for me.
However, I had set it true to allow more query rows (>50,000)
Just have to remember why I needed to do that...
Jake BackuesJake Backues
@Raúl Germán Muñoz Torres 9

I was getting the same issue with a cacheable method from an Aura component used on a Visualforce page. By removing (cacheablel=true) I was able to successfully make my dml statement.

I'm not sure if this will be the same solution needed for Lightning Web Components, but it worked for Aura Components.