+ 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.
Madhuri MogalMadhuri Mogal
Thanks @amitashtekar....It works for me by making  readOnly="false".
Sanju Sam MathewSanju Sam Mathew
Hi, 
I too am facing the same problem. "System.LimitException: Too many DML statements: 1"

This is my code:

public with sharing class met1x_DVIMarkerController {
    @AuraEnabled(cacheable=true)
    public static boolean getcheckboxvalue(String recordid){
        String objName=findObjectNameFromRecordId(recordid);
        try{
            System.debug('objName'+objName);
            System.debug('recordid'+recordid);
            if(objName=='Account'){
                
                return [select GiDP_DomesticViolenceIndicator__c from Account where id=:recordid and GiDP_DomesticViolenceIndicator__c=true limit 1].GiDP_DomesticViolenceIndicator__c;
                //return obj.get(0).GiDP_DomesticViolenceIndicator__c;
            }
            else if(objName == 'Asset'){
                return [select GiDP_DomesticViolenceIndicator__c from Asset where id=:recordid and GiDP_DomesticViolenceIndicator__c=true limit 1].GiDP_DomesticViolenceIndicator__c;
            }
            else if(objName == 'vlocity_ins__InsuranceClaim__c'){
                return [select GiDP_DomesticViolenceIndicator__c from vlocity_ins__InsuranceClaim__c where id=:recordid and GiDP_DomesticViolenceIndicator__c=true limit 1].GiDP_DomesticViolenceIndicator__c;
                
            }
            else if(objName=='GiDP_ImageMetadataRepo__c'){
                // List<GiDP_ImageMetadataRepo__c> metadatarepos=[select id,]
                List<GiDP_ImageMetadataRepo__c> metadatrepos= [select id,GiDP_PolicyRecord__r.GiDP_FraudIndicator__c from GiDP_ImageMetadataRepo__c where id=:recordid and GiDP_PolicyRecord__r.GiDP_DomesticViolenceIndicator__c=true limit 1];
                if(metadatrepos.size()>0){
                    return true;
                }
            }
            /*if(Test.isRunningTest()){
                
                throw new DMLException('Static DML Exception to cover catch block');
                
            }*/
            return false;
        }
        catch (Exception ex) {
            ExceptionLogUtility.createException('getcheckboxvalue method', 'Exception', ex.getStackTraceString(), 
                                                'met1x_DVIMarkerController Class', 'Application', objName+' Object', ex.getMessage());
            return null;
        }
    }
    @AuraEnabled(cacheable=true)
    public static boolean getFraudIndicatorInfo(String recordid){
        String objName=findObjectNameFromRecordId(recordid);
        try{
            System.debug('objName'+objName);
            System.debug('recordid'+recordid);
            if(objName=='Account'){
                return [select GiDP_FraudIndicator__c from Account where id=:recordid and GiDP_FraudIndicator__c=true limit 1].GiDP_FraudIndicator__c;
                //return obj.get(0).GiDP_DomesticViolenceIndicator__c;
            }
            else if(objName == 'Asset'){
                return [select GiDP_FraudIndicator__c from Asset where id=:recordid and GiDP_FraudIndicator__c=true limit 1].GiDP_FraudIndicator__c;
            }
            else if(objName == 'vlocity_ins__InsuranceClaim__c'){
                return [select GiDP_FraudIndicator__c from vlocity_ins__InsuranceClaim__c where id=:recordid and GiDP_FraudIndicator__c=true limit 1].GiDP_FraudIndicator__c;
                
            }
            else if(objName=='GiDP_ImageMetadataRepo__c'){
                //return true;
                List<GiDP_ImageMetadataRepo__c> metadatrepos= [select id,GiDP_PolicyRecord__r.GiDP_FraudIndicator__c from GiDP_ImageMetadataRepo__c where id=:recordid and GiDP_PolicyRecord__r.GiDP_FraudIndicator__c=true limit 1];
                if(metadatrepos.size()>0){
                    return true;
                }
            }
            return false;
        }
        catch (Exception ex) {
            ExceptionLogUtility.createException('getFraudIndicatorInfo method', 'Exception', ex.getStackTraceString(), 
                                                'met1x_DVIMarkerController Class', 'Application', objName+' Object', ex.getMessage());
            return null;
        }
    }
    @AuraEnabled(cacheable=true)
    public static String findObjectNameFromRecordId(String recordId){
        String objectName = '';
        try{
            //Get prefix from record ID
            //This assumes that you have passed at least 3 characters
            String myIdPrefix = String.valueOf(recordId).substring(0,3);
            
            //Get schema information
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
            
            //Loop through all the sObject types returned by Schema
            for(Schema.SObjectType stype : gd.values()){
                if(stype.getDescribe().getKeyPrefix() != null && 
                   (stype.getDescribe().getKeyPrefix() == '001' ||
                    stype.getDescribe().getKeyPrefix() == '02i' ||
                    stype.getDescribe().getKeyPrefix() == 'a')){
                        Schema.DescribeSObjectResult r = stype.getDescribe();
                        String prefix = r.getKeyPrefix();
                        System.debug('Prefix is ' + prefix);
                        
                        //Check if the prefix matches with requested prefix
                        if(prefix!=null && prefix.equals(myIdPrefix)){
                            objectName = r.getName();
                            System.debug('Object Name! ' + objectName);
                            break;
                        }
                    }
            }
        }
        catch (Exception ex) {
            ExceptionLogUtility.createException('findObjectNameFromRecordId method', 'Exception', ex.getStackTraceString(), 
                                                'met1x_DVIMarkerController Class', 'Application', objectName+' Object', ex.getMessage());
        }
        return objectName;
    }
}
David Roberts 4David Roberts 4
Have you tried removing the 'cacheable=true' instruction?
See https://www.gammone.com/en/programming/how-to-resolve-the-too-many-dml-statements-1-error-in-salesforce