Reduce number of DML Statements

One of the most common governor limit which every developer encounter is the number of DML Statements and there are many more as well like SOQL limit which I will be explaining in my next blog.

Identify the number of DML Statements allowed per transaction?


system.debug('DML Limit ' + Limits.getLimitDMLStatements());

SF Governor Limit

150 DML statements for a transaction is more than enough, but it may hit the limit if code is not properly written.


List<Account> acclist = new List<Account>();
acclist.add(new Account(Name = 'Account 1'));
acclist.add(new Account(Name = 'Account 2'));

List<CustomObject__c> customObjlist = new List<CustomObject__c>();
customObjlist.add(new CustomObject__c(Name = 'Custom Rec 1'));
customObjlist.add(new CustomObject__c(Name = 'Custom Rec 2'));

insert acclist;
insert customObjlist;

Here, if we see the above code consumes 2 DML statements – one for each object which is good but what if the number of different objects increases?

We can use sObject list which reduces it even further:

sObject is a generic base object where every object is an extension of sObject in APEX.


List<sObject> insertSObjectlist = new List<sObject>();

insertSObjectlist.add(new Account(Name = 'Account 1'));
insertSObjectlist.add(new Account(Name = 'Account 2'));

insertSObjectlist.add(new CustomObject__c(Name = 'Custom Rec 1'));
insertSObjectlist.add(new CustomObject__c(Name = 'Custom Rec 2'));

insert insertSObjectlist;

DML limit

Now, using sObject list the earlier 2 DML statements are now reduced to 1,
likewise we can use sObjectlist for all other types of DML.

Here is another limit, we cannot add’n’ number of objects to generic sObject list. At max, we can have a mix of 10 different objects

sample code for Update, Upsert and Delete using sObject list:

For Update:


List<sObject> updateSObjectlist = new List<sObject>();

updateSObjectlist.add(new Account(id= '0012800000pNrfL', Name = 'Account Rec 1'));
updateSObjectlist.add(new Account(id= '0012800000pNrfM', Name = 'Account Rec 2'));

updateSObjectlist.add(new CustomObject__c(id= 'a042800000RHfnU', Name = 'Custom Rec 1'));
updateSObjectlist.add(new CustomObject__c(id= 'a042800000RHfnV', Name = 'Custom Rec 2'));

update updateSObjectlist;

For Upsert:

Currently, Salesforce doesn’t support Upsert on generic sObject list.

Here is idea, you can vote and salesforce may support soon which saves us in writing extra code for spliting up the list into insert and update list.

For Delete:
List<sObject> deleteSObjectlist = new List<sObject>();

deleteSObjectlist.add(new Account(id= '0012800000pNrfL'));
deleteSObjectlist.add(new Account(id= '0012800000pNrfM'));

deleteSObjectlist.add(new CustomObject__c(id= 'a042800000RHfnU'));
deleteSObjectlist.add(new CustomObject__c(id= 'a042800000RHfnV'));

delete deleteSObjectlist;

**** Start using generic sObject list ***** and Happy coding ****

Advertisements

One thought on “Reduce number of DML Statements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s