+ Start a Discussion
RadnipRadnip 

Bulkify best practice...

I'm trying to reduce the number of DML statements in some code. At the moment I'm looping over some logic to create a set of records to create in a custom object, then inserting the set of records. But then I'm looping over the inserted records to create some child records.

 

My question is, it is possible to create a set of the master object record and child object records and insert them using one DML?

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVa_VFTabulatorSeAlVa_VFTabulator

You can also reduce  the DML statements by inserting, updating, deleting more than one element at once (even if they have different types).

 

How?

If you have a group of independent objects, you can create a List of sObjects and insert all at the same time.

 

For example, imaging you have two objects, Dummy__c and Dummy2__c,

and you need to create an instance of each,  

you can proceed like the following code.

 

 

Dummy__c a = new Dummy__c(name='a');
Dummy2__c b = new Dummy2__c(name='b');

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

toInsert.add(a);
toInsert.add(b);

insert toInsert;

 

 This only consumes 1 DML statement.

 

 

Regards, Sergio

 

P.S. This might make your code a little bit less readable.

All Answers

bob_buzzardbob_buzzard

I don't think you'll be able to do this in a single DML unless you have an external id field on the account.

 

Presumably you only need to have 2 DML statements though - one that inserts the set of parent records, then another to insert all of the child records after you've iterated all the parents.

RadnipRadnip

It was wishful thinking more than anything. The code is being called from all over the place and looping this chunk. So by reducing it by one query could reduce the queries down quite a bit. Just going to have to rewrite it totally instead :(

SeAlVa_VFTabulatorSeAlVa_VFTabulator

You can also reduce  the DML statements by inserting, updating, deleting more than one element at once (even if they have different types).

 

How?

If you have a group of independent objects, you can create a List of sObjects and insert all at the same time.

 

For example, imaging you have two objects, Dummy__c and Dummy2__c,

and you need to create an instance of each,  

you can proceed like the following code.

 

 

Dummy__c a = new Dummy__c(name='a');
Dummy2__c b = new Dummy2__c(name='b');

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

toInsert.add(a);
toInsert.add(b);

insert toInsert;

 

 This only consumes 1 DML statement.

 

 

Regards, Sergio

 

P.S. This might make your code a little bit less readable.

This was selected as the best answer
RadnipRadnip

Of course an sObject list!! :) yes, you have to create the instance but DML is the bigger issue than the lines of executed code.

 

Thanks a lot!

 

Chamil MadusankaChamil Madusanka

You can refer following link too. It will be provided good understanding about bulkyfy best practice.

 

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.