6

Database.convertLead() will create an account, a contact and optionally an opportunity.

I ran the conversion for a lead using the following code.

Lead myLead = [SELECT ID,Status FROM Lead WHERE Id = '00Q9000000nuxhO'];
Database.LeadConvert lc = new Database.LeadConvert();
Leadstatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setLeadId(myLead.id);
Database.convertLead(lc);

It created an Account, an Opportunity and a Contact.

So ideally it should have been a 3 DML statements right ? But in the log it shows only 1 DML statement.

Can anyone explain the rationale behind this ?

4
  • Doesn't 1 dml statement seem more "ideal" than 3?
    – Adrian Larson
    Sep 1, 2016 at 0:25
  • I know...but I am just curious to know why it is considered a single statement :)
    – Varun
    Sep 1, 2016 at 0:31
  • Can you add a debug on Limits.getDmlStatements() after the convertLead call just to make it more explicit?
    – Adrian Larson
    Sep 1, 2016 at 0:34
  • 1
    Database.convertLead(...) runs insert List<sObject> maybe, the only reasonable explanation that I could imagine
    – Joca
    Sep 1, 2016 at 0:57

3 Answers 3

5

As stated on Execution Governors and Limits:

Calls to the following methods count against the number of DML queries issued in a request.

  • Approval.process
  • Database.convertLead
  • Database.emptyRecycleBin
  • Database.rollback
  • Database.setSavePoint
  • delete and Database.delete
  • insert and Database.insert
  • merge and Database.merge
  • undelete and Database.undelete
  • update and Database.update
  • upsert and Database.upsert
  • System.runAs

Every time you call any of them, the system increments the DML counter by 1, regardless of how many records were processed by the statement.

4
  • @AdrianLarson of course not :p
    – sfdcfox
    Sep 1, 2016 at 2:54
  • 1
    Done! Again, +1 for finding a reference for this one. Didn't even occur to me.
    – Adrian Larson
    Sep 1, 2016 at 2:57
  • 1
    @AdrianLarson Thanks! My arrow keys are not working quite right just now, so that would have taken me ages.
    – sfdcfox
    Sep 1, 2016 at 2:59
  • Python ftw. print('\n'.join('>- `%s`' % operation for operation in '''<bullets>'''.split('\n')))
    – Adrian Larson
    Sep 1, 2016 at 3:02
4

The reason is that 1 DML statement can insert multiple records. The system created an Account, an Opportunity and a Contact, all with a single Apex DML statement: Database.convertLead(lc);

The number of records created is irrelevant to the number of statements used.

4

I can't verify this is what happens with Database.convertLead, but you can insert up to 10 different types of SObject in one List<SObject> even without using that method, as astutely noted by @Joca in the comments.

insert new List<SObject>
{
    new Account(...),
    new Contact(...),
    new Opportunity(...)
};
system.assertEquals(1, Limits.getDmlStatements());

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .