function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TapasviniTapasvini 

dmlexception error

I am getting error : System.LimitException: DML currently not allowed 

 

Class.heatclass.getStore: line 9, column 1

 

my code is:

public String getStore() {
for(integer ac=0;ac<mapstr.size();ac++)
{
try{

auditcount__c e=new auditcount__c(Name='hello',count__c=5);
insert e;
}
catch(System.DMLException ex)
{
ApexPages.addMessages(ex);
}
}
return null;
}

 

Henry AkpalaHenry Akpala

You have 2 issues, one is the "Insert e" is hitting the governor limit becuase you are inserting for every record. What you want to do is move it outside the for loop like below.  Also I think you might run into an issue with the return null, probably want to return an empty string like "return ' '  "

 

public String getStore() {
for(integer ac=0;ac<mapstr.size();ac++)
{

auditcount__c e=new auditcount__c(Name='hello',count__c=5);

}

try{

insert e;
}
catch(System.DMLException ex)
{
ApexPages.addMessages(ex);
}
return null;
}

 

Hope this helps

Regards

-Henry

Praful GadgePraful Gadge

Hi Tapasvini,

 

I think the above mentionded code wont work because it will overrive the value in auditcount__c e.

 

The workarround can be:

 

public String getStore()

{

    try

    {

            List<auditcount__c> listOfAuditcount = new List<auditcount__c>();

            for(integer ac=0;ac<mapstr.size();ac++)
            {
              auditcount__c e=new auditcount__c(Name='hello',count__c=5);
              listOfAuditcount.add(e);
            }
            insert listOfAuditcount;

    }
    catch(System.DMLException ex)
    {
           ApexPages.addMessages(ex);
    }

}

 

 

Let me know your feedback.

 

Sincerely,

Praful G.