+ Start a Discussion
Sanju Sam MathewSanju Sam Mathew 

I am getting this Error: System.LimitException: Too many DML statements: 1

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;
    }
}
SwethaSwetha (Salesforce Developers) 
HI Sanju,
Based on similar post https://salesforce.stackexchange.com/questions/246851/storable-action-system-limitexception-too-many-dml-statements-1  , can you Remove (Cacheable=true) and see if that fixes the error?

As given in this doc,

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_apex_auraenabled_annotation.htm

To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data, it can’t mutate data.

Related: https://developer.salesforce.com/forums/?id=9062I000000IE4kQAG

If this information helps, please mark the answer as best.Thank you
Naveen KNNaveen KN
Hey Sanju, Can you find which method excatly throwing this DML exception error from the console logs? as I see all the methods mentioned above are reading the data.