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
Mahantesh KenganalMahantesh Kenganal 

How to overcome with 150 DML limit for apex

* How to overcome of if i want to use more than 150 DML statements in apex . as per governer limit we cant use than 150 DML , to overcome this what we need to do?

 
Best Answer chosen by Mahantesh Kenganal
ClintLeeClintLee
Hi Mahantesh,

It's not possible to use more than 150 DML statements in a single apex transaction.

If you need to insert/update/delete many records you can do so by calling a DML statement on a list.

For example, to insert 150 new accounts you could do this.
 
// create 150 accounts
List<Account> accounts = new List<Account>();

for(Integer i = 0; i < 150; i++)
{
    Account account = new Account(Name = 'Test Account');
    accounts.add(account);
}

insert accounts;  // insert the entire list - this only consumes 1 DML statement.

Hope that helps,

Clint

All Answers

Abhishek BansalAbhishek Bansal
Hi Mahantesh,

The basic cause of hitting 150 DML limit in apex is execution of DML statemnets in for loop.
So we have to avoid DML statements in for loop.

Please find more help in link given below :
http://www.saaspie.com/2014/01/01/apex-governor-limits/

Let me know if you need more help.

Regards,
Abhishek
ClintLeeClintLee
Hi Mahantesh,

It's not possible to use more than 150 DML statements in a single apex transaction.

If you need to insert/update/delete many records you can do so by calling a DML statement on a list.

For example, to insert 150 new accounts you could do this.
 
// create 150 accounts
List<Account> accounts = new List<Account>();

for(Integer i = 0; i < 150; i++)
{
    Account account = new Account(Name = 'Test Account');
    accounts.add(account);
}

insert accounts;  // insert the entire list - this only consumes 1 DML statement.

Hope that helps,

Clint
This was selected as the best answer
Vishal_GuptaVishal_Gupta
Hi Mahantesh,

If your code is optimize and effecient as per the best practise to written Apex code and if still you are getting DML governor limits issue then you can implement some workaround to overcome with this issue but for that you need to first consider your business flow and requirement.

One of the woraround is to break your context in two part and then using actionfunction you can call first method and oncomplete of first actionfunction you can call second actionfunction in this way context will divide in two part and you will overcome with DML governor limit issue.

Please let me know if you require more information.

Thanks,
Vishal
Mohammad_DanishMohammad_Danish
Hi Mahantesh,

One of the workarounds could also be to have a count variable that keeps track of the list size (List.size works in VF page. List.size() is for apex class). Once the count variable reaches 150 ( DML records limit) use another list to hold the records. This way you will be able to overcome with DML governor limit issue. One thing you need to keep in mind is the overhead for the processing of the count variable. 

Thanks,
Danish